-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcountries.py
168 lines (131 loc) · 5.32 KB
/
countries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: UTF-8 -*-
# Copyright (C) 2009 Sylvain Taverne <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from standard library
from operator import itemgetter
# Import from itools
from itools.core import get_abspath
from itools.csv import Table as BaseTable, CSVFile, Property
from itools.datatypes import Enumerate, Boolean, Unicode
from itools.gettext import MSG
from itools.handlers import ro_database
from itools.web import get_context
# Import from ikaaro
from ikaaro.forms import BooleanRadio, SelectWidget, TextWidget
from ikaaro.registry import register_resource_class
from ikaaro.table import Table, OrderedTable, OrderedTableFile
from ikaaro.table_views import Table_AddRecord
# Import from shop
from countries_views import Countries_View, CountriesZones_View
from enumerates import CountriesZonesEnumerate
from utils import get_shop
###########################################################
# The list of countries is used in the user address book.
#
# Administrator of shop can activate/desactivate countries
# to allow or not the delivery in this countries.
#
# Shipping module allow to configure the price of delivery
# for each countries.
###########################################################
class BaseCountriesZones(OrderedTableFile):
record_properties = {
'title': Unicode(mandatory=True),
'has_tax': Boolean
}
class CountriesZones(OrderedTable):
class_id = 'countries-zones'
class_title = MSG(u'Countries Zones')
class_handler = BaseCountriesZones
class_views = ['view', 'add_record']
class_version = '20090923'
view = CountriesZones_View()
add_record = Table_AddRecord(title=MSG(u'Add a new zone'))
form = [
TextWidget('title', title=MSG(u'Country title')),
BooleanRadio('has_tax', title=MSG(u'Has TAX ?'))
]
@staticmethod
def _make_resource(cls, folder, name, *args, **kw):
OrderedTable._make_resource(cls, folder, name)
table = BaseCountriesZones()
zones = []
csv = ro_database.get_handler(get_abspath('data/countries.csv'), CSVFile)
for line in csv.get_rows():
zone = unicode(line[1], 'utf-8')
if zone not in zones:
zones.append(zone)
table.add_record({'title': Property(zone, language='fr')})
folder.set_handler(name, table)
class CountriesEnumerate(Enumerate):
@classmethod
def get_options(cls):
context = get_context()
# Search shop
shop = get_shop(context.resource)
# Get options
countries = shop.get_resource('countries').handler
if hasattr(cls, 'zone'):
records = countries.search(zone=str(cls.zone))
else:
records = countries.get_records()
options = []
for record in records:
enabled = countries.get_record_value(record, 'enabled')
if enabled is False:
continue
options.append({'name': str(record.id),
'value': countries.get_record_value(record, 'title')})
options.sort(key=itemgetter('value'))
return options
class BaseCountries(BaseTable):
record_properties = {
'title': Unicode(mandatory=True, multiple=True),
'zone': CountriesZonesEnumerate(is_indexed=True, mandatory=True),
'enabled': Boolean,
}
class Countries(Table):
class_id = 'countries'
class_title = MSG(u'Countries')
class_handler = BaseCountries
class_views = ['view', 'add_record']
view = Countries_View()
add_record = Table_AddRecord(title=MSG(u'Add a new country'))
form = [
TextWidget('title', title=MSG(u'Country title')),
SelectWidget('zone', title=MSG(u'Zone')),
BooleanRadio('enabled', title=MSG(u'Enabled')),
]
# XXX Enabled means its enabled for delivery not for registration
# People from south africa can register on website but not to be delivered
# This distinction must be explicit
@staticmethod
def _make_resource(cls, folder, name, *args, **kw):
Table._make_resource(cls, folder, name)
# Import CSV with list of countries
zones = []
table = BaseCountries()
csv = ro_database.get_handler(get_abspath('data/countries.csv'), CSVFile)
for line in csv.get_rows():
country = unicode(line[0], 'utf-8')
zone = unicode(line[1], 'utf-8')
if zone not in zones:
zones.append(zone)
table.add_record({'title': Property(country, language='fr'),
'zone': str(zones.index(zone)),
'enabled': True})
folder.set_handler(name, table)
register_resource_class(Countries)
register_resource_class(CountriesZones)