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
|
|
|
import json |
22
|
|
|
from operator import itemgetter |
23
|
|
|
from senaite.core.api import geo |
24
|
|
|
from senaite.core.p3compat import cmp |
25
|
|
|
from bika.lims.browser import BrowserView |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class ajaxGetCountries(BrowserView): |
29
|
|
|
|
30
|
|
|
def __call__(self): |
31
|
|
|
searchTerm = self.request['searchTerm'].lower() |
32
|
|
|
page = self.request['page'] |
33
|
|
|
nr_rows = self.request['rows'] |
34
|
|
|
sord = self.request['sord'] |
35
|
|
|
sidx = self.request['sidx'] |
36
|
|
|
rows = [] |
37
|
|
|
|
38
|
|
|
# lookup objects from ISO code list |
39
|
|
|
for country in geo.get_countries(): |
40
|
|
|
iso = country.alpha_2 |
41
|
|
|
name = country.name |
42
|
|
|
country_info = {"Code": iso, "Country": name} |
43
|
|
|
if iso.lower().find(searchTerm) > -1: |
44
|
|
|
rows.append(country_info) |
45
|
|
|
elif name.lower().find(searchTerm) > -1: |
46
|
|
|
rows.append(country_info) |
47
|
|
|
|
48
|
|
|
rows = sorted(rows, cmp=lambda x,y: cmp(x.lower(), y.lower()), key=itemgetter(sidx and sidx or 'Country')) |
49
|
|
|
if sord == 'desc': |
50
|
|
|
rows.reverse() |
51
|
|
|
pages = len(rows) / int(nr_rows) |
52
|
|
|
pages += divmod(len(rows), int(nr_rows))[1] and 1 or 0 |
53
|
|
|
ret = {'page':page, |
54
|
|
|
'total':pages, |
55
|
|
|
'records':len(rows), |
56
|
|
|
'rows':rows[ (int(page) - 1) * int(nr_rows) : int(page) * int(nr_rows) ]} |
57
|
|
|
|
58
|
|
|
return json.dumps(ret) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
class ajaxGetStates(BrowserView): |
62
|
|
|
|
63
|
|
|
def __call__(self): |
64
|
|
|
items = [] |
65
|
|
|
country = self.request.get('country', '') |
66
|
|
|
if not country: |
67
|
|
|
return json.dumps(items) |
68
|
|
|
|
69
|
|
|
# Get the states |
70
|
|
|
items = geo.get_subdivisions(country, default=[]) |
71
|
|
|
items = map(lambda sub: [sub.country_code, sub.code, sub.name], items) |
72
|
|
|
return json.dumps(items) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
class ajaxGetDistricts(BrowserView): |
76
|
|
|
|
77
|
|
|
def __call__(self): |
78
|
|
|
country = self.request.get('country', '') |
79
|
|
|
state = self.request.get('state', '') |
80
|
|
|
items = [] |
81
|
|
|
if not all([country, state]): |
82
|
|
|
return json.dumps(items) |
83
|
|
|
|
84
|
|
|
# first-level subdivisions (districts?) of this subdivision (state?) |
85
|
|
|
state = geo.get_subdivision(state, parent=country, default=None) |
86
|
|
|
items = geo.get_subdivisions(state, default=[]) |
87
|
|
|
items = map(lambda sub: [sub.country_code, sub.code, sub.name], items) |
88
|
|
|
return json.dumps(items) |
89
|
|
|
|