| 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): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 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 |  |  |  |