• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / zc.form / 16248976557

21 Oct 2024 07:16AM UTC coverage: 82.759%. Remained the same
16248976557

push

github

icemac
Back to development: 2.2

126 of 180 branches covered (70.0%)

Branch coverage included in aggregate %.

786 of 922 relevant lines covered (85.25%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

47.46
/src/zc/form/browser/tzwidget.py
1
##############################################################################
2
#
3
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
"""timezone widget"""
15

16
import pytz
1✔
17
# XXX argh.
18
import pytz.tzinfo
1✔
19

20
import zope.browserpage
1✔
21
import zope.formlib.interfaces
1✔
22
import zope.interface.common.idatetime
1✔
23
import zope.publisher.interfaces.browser
1✔
24
from zope import component
1✔
25
from zope import interface
1✔
26
from zope.interface.common.idatetime import ITZInfo
1✔
27
from zope.security.checker import NamesChecker
1✔
28

29
import zc.form.browser.mruwidget
1✔
30
import zc.form.interfaces
1✔
31

32

33
ALL_TIMEZONES = frozenset(pytz.all_timezones)
1✔
34

35
names = set(ITZInfo.names(all=True))
1✔
36
names.add('zone')
1✔
37
names.add('localize')
1✔
38
checker = NamesChecker(names)
1✔
39
pytz.UTC.__Security_checker__ = checker
1✔
40
pytz.tzinfo.BaseTzInfo.__Security_checker__ = checker
1✔
41
# end argh.
42

43

44
class TimeZoneWidget(zc.form.browser.mruwidget.MruSourceInputWidget):
1✔
45

46
    def getMostRecentlyUsedTerms(self):
1✔
47
        mru = super().getMostRecentlyUsedTerms()
×
48
        # add ones from locale
49
        territory = self.request.locale.id.territory
×
50
        if territory:
×
51
            try:
×
52
                choices = pytz.country_timezones(territory)
×
53
            except KeyError:
×
54
                pass
×
55
            else:
56
                already = {term.token for term in mru}
×
57
                additional = sorted(t for t in choices if t not in already)
×
58
                mru.extend(zc.form.interfaces.Term(t.replace('_', ' '), t)
×
59
                           for t in additional)
60
        return mru
×
61

62

63
@component.adapter(zc.form.interfaces.AvailableTimeZones,
1✔
64
                   zope.publisher.interfaces.browser.IBrowserRequest)
65
@interface.implementer(zope.formlib.interfaces.ISourceQueryView)
1✔
66
class TimeZoneQueryView:
1✔
67

68
    def __init__(self, source, request):
1✔
69
        self.context = source
×
70
        self.request = request
×
71

72
    _render = zope.browserpage.ViewPageTemplateFile('timezone_queryview.pt')
1✔
73

74
    def render(self, name):
1✔
75
        return self._render(field_name=name + '.searchstring',
×
76
                            button_name=name + '.search')
77

78
    def results(self, name):
1✔
79
        if not (name + '.search' in self.request):
×
80
            return None
×
81

82
        searchstring = self.request[name + '.searchstring'].lower()
×
83
        timezones = []
×
84
        searchstring = searchstring.strip().lower().replace(' ', '_')  # regex
×
85
        for tz in ALL_TIMEZONES:
×
86
            if searchstring in tz.lower():
×
87
                timezones.append(pytz.timezone(tz))
×
88
        return timezones
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc