Passed
Pull Request — 2.x (#1960)
by Jordi
07:32
created

AddressWidget.getDistricts()   A

Complexity

Conditions 4

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 4
nop 3
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE.
4
#
5
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2021 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from AccessControl import ClassSecurityInfo
22
from Products.Archetypes.Registry import registerWidget
23
from Products.Archetypes.Widget import TypesWidget
24
from Products.CMFCore.utils import getToolByName
25
from senaite.core.locales import get_countries
26
from senaite.core.locales import get_states
27
from senaite.core.locales import get_districts
28
from senaite.core.p3compat import cmp
29
30
31
class AddressWidget(TypesWidget):
32
    _properties = TypesWidget._properties.copy()
33
    _properties.update({
34
        'macro': "bika_widgets/addresswidget",
35
        'helper_js': ("bika_widgets/addresswidget.js",),
36
        'helper_css': ("bika_widgets/addresswidget.css",),
37
        'showLegend': True,
38
        'showDistrict': True,
39
        'showCopyFrom': True,
40
        'showCity': True,
41
        'showPostalCode': True,
42
        'showAddress': True,
43
    })
44
45
    security = ClassSecurityInfo()
46
47
    # The values in the form/field are always
48
    # Country Name, State Name, District Name.
49
50
    def getCountries(self):
51
        countries = get_countries()
52
        items = map(lambda item: (item.alpha_2, item.name), countries)
53
        items.sort(lambda x, y: cmp(x[1], y[1]))
54
        return items
55
56
    def getDefaultCountry(self):
57
        portal = getToolByName(self, 'portal_url').getPortalObject()
58
        bs = portal._getOb('bika_setup')
59
        return bs.getDefaultCountry()
60
61
    def getStates(self, country):
62
        items = []
63
        if not country:
64
            return items
65
66
        # Get the states
67
        items = get_states(country, default=[])
68
        return map(lambda sub: [sub.country_code, sub.code, sub.name], items)
69
70
    def getDistricts(self, country, state):
71
        items = []
72
        if not country or not state:
73
            return items
74
        items = get_districts(country, state)
75
        return map(lambda sub: [sub.country_code, sub.code, sub.name],  items)
76
77
78
registerWidget(AddressWidget,
79
               title = 'Address Widget',
80
               description = ('Simple address widget with country/state lookups'),
81
               )
82