Passed
Pull Request — 2.x (#1961)
by Ramon
05:23
created

bika.lims.browser.widgets.addresswidget   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 51
dl 0
loc 89
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A AddressWidget.getDistricts() 0 10 4
A AddressWidget.getDefaultCountry() 0 4 1
A AddressWidget.to_utf8() 0 6 3
A AddressWidget.getCountries() 0 4 2
A AddressWidget.getStates() 0 9 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.api import geo
26
27
28
class AddressWidget(TypesWidget):
29
    _properties = TypesWidget._properties.copy()
30
    _properties.update({
31
        'macro': "bika_widgets/addresswidget",
32
        'helper_js': ("bika_widgets/addresswidget.js",),
33
        'helper_css': ("bika_widgets/addresswidget.css",),
34
        'showLegend': True,
35
        'showDistrict': True,
36
        'showCopyFrom': True,
37
        'showCity': True,
38
        'showPostalCode': True,
39
        'showAddress': True,
40
    })
41
42
    security = ClassSecurityInfo()
43
44
    # The values in the form/field are always
45
    # Country Name, State Name, District Name.
46
47
    def getCountries(self):
48
        countries = geo.get_countries()
49
        items = map(lambda item: (item.alpha_2, item.name), countries)
50
        return self.to_utf8(items)
51
52
    def getDefaultCountry(self):
53
        portal = getToolByName(self, 'portal_url').getPortalObject()
54
        bs = portal._getOb('bika_setup')
55
        return bs.getDefaultCountry()
56
57
    def getStates(self, country):
58
        items = []
59
        if not country:
60
            return items
61
62
        # first-level subdivisions of the country (states??)
63
        items = geo.get_subdivisions(country, default=[])
64
        items = map(lambda sub: [sub.country_code, sub.code, sub.name], items)
65
        return self.to_utf8(items)
66
67
    def getDistricts(self, country, state):
68
        items = []
69
        if not country or not state:
70
            return items
71
72
        # first-level subdivisions (districts?) of this subdivision (state?)
73
        state_obj = geo.get_subdivision(state, parent=country, default=None)
74
        items = geo.get_subdivisions(state_obj, default=[])
75
        items = map(lambda sub: [sub.country_code, sub.code, sub.name], items)
76
        return self.to_utf8(items)
77
78
    def to_utf8(self, value):
79
        if isinstance(value, unicode):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable unicode does not seem to be defined.
Loading history...
80
            return value.encode("utf-8")
81
        elif isinstance(value, list):
82
            return map(self.to_utf8, value)
83
        return value
84
85
86
registerWidget(AddressWidget,
87
               title = 'Address Widget',
88
               description = ('Simple address widget with country/state lookups'),
89
               )
90