-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcountries_views.py
137 lines (109 loc) · 4.69 KB
/
countries_views.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
# -*- 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 itools
from itools.core import merge_dicts
from itools.datatypes import Boolean, Integer, String
from itools.gettext import MSG
from itools.web import INFO, ERROR
from itools.xml import XMLParser
# Import from ikaaro
from ikaaro.forms import SelectWidget
from ikaaro.table_views import OrderedTable_View
# Import from shop
from enumerates import CountriesZonesEnumerate
from utils import bool_to_img
from utils_views import SearchTable_View
class Countries_View(SearchTable_View):
columns = [
('checkbox', None),
('title', MSG(u'Country title')),
('zone', MSG(u'Zone title')),
('enabled', MSG(u'Is enabled ?')),
]
# XXX Can't delete country
# We have to check -> User addresses ...
table_actions = []
search_schema = {'zone': CountriesZonesEnumerate}
search_widgets = [SelectWidget('zone', title=MSG(u'Zone'))]
def get_table_columns(self, resource, context):
return self.columns
def get_query_schema(self):
return merge_dicts(SearchTable_View.get_query_schema(self),
batch_size=Integer(default=500),
reverse=Boolean(default=False),
sort_by=String(default='title'))
def get_item_value(self, resource, context, item, column):
handler = resource.handler
if column == 'checkbox':
return item.id, False
elif column == 'title':
id = item.id
title = handler.get_record_value(item, 'title')
link = context.get_link(resource)
return title, '%s/;edit_record?id=%s' % (link, id)
elif column == 'zone':
zone = handler.get_record_value(item, 'zone')
return CountriesZonesEnumerate.get_value(zone)
elif column == 'enabled':
enabled = handler.get_record_value(item, 'enabled')
return bool_to_img(enabled)
class CountriesZones_View(OrderedTable_View):
columns = [
('checkbox', None),
('title', MSG(u'Zone title')),
('countries', MSG(u'Associated countries')),
('has_tax', MSG(u'Has TAX ?')),
]
link_template = "<a href='%s'>(Edit this list)</a>"
def get_table_columns(self, resource, context):
return self.columns
def get_item_value(self, resource, context, item, column):
handler = resource.handler
if column == 'checkbox':
return item.id, False
elif column == 'title':
id = item.id
title = handler.get_record_value(item, 'title')
link = context.get_link(resource)
return title, '%s/;edit_record?id=%s' % (link, id)
elif column == 'countries':
from countries import CountriesEnumerate
datatype = CountriesEnumerate(zone=item.id)
widget = SelectWidget('l_%s' % item.id, has_empty_option=False)
h_widget = widget.to_html(datatype, None)
link = '/shop/countries?zone=%i&search=' % item.id
link = self.link_template % link
h_link = XMLParser(link)
return list(h_widget) + list(h_link)
elif column == 'has_tax':
has_tax = handler.get_record_value(item, 'has_tax')
return bool_to_img(has_tax)
del_msg = u"You can't delete zone %s. It's associated to %s countries."
def action_remove(self, resource, context, form):
from countries import CountriesEnumerate
ids = form['ids']
for id in ids:
datatype = CountriesEnumerate(zone=id)
options = datatype.get_options()
if len(options) != 0:
record = resource.handler.get_record(id)
zone = resource.handler.get_record_value(record, 'title')
context.message = ERROR(self.del_msg % (zone, len(options)))
return
resource.handler.del_record(id)
# Reindex the resource
context.server.change_resource(resource)
context.message = INFO(u'Zone(s) deleted')