-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddresses.py
131 lines (105 loc) · 4.2 KB
/
addresses.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
# -*- 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.csv import Table as BaseTable
from itools.datatypes import Unicode, String, Enumerate
from itools.gettext import MSG
from itools.web import get_context
# Import from ikaaro
from ikaaro.forms import SelectRadio, TextWidget, SelectWidget
from ikaaro.registry import register_resource_class
from ikaaro.table import Table
from ikaaro.table_views import Table_View
# Import from shop
from countries import CountriesEnumerate
from datatypes import Civilite
from addresses_views import Addresses_Book, Addresses_Search
from addresses_views import Addresses_AddAddress, Addresses_EditAddress
from utils import get_shop
class Addresses_Enumerate(Enumerate):
@classmethod
def get_options(cls):
options = []
context = get_context()
shop = get_shop(context.resource)
addresses = shop.get_resource('addresses').handler
for address in addresses.search(user=context.user.name):
options.append(
{'name': str(address.id),
'value': addresses.get_record_value(address, 'title')})
return options
class BaseAddresses(BaseTable):
record_properties = {
'title': Unicode(is_indexed=True, mandatory=True),
'gender': Civilite(mandatory=True),
'firstname': Unicode(mandatory=True, is_indexed=True),
'lastname': Unicode(mandatory=True, is_indexed=True),
'user': String(is_indexed=True),
'address_1': Unicode(mandatory=True),
'address_2': Unicode,
'zipcode': String(mandatory=True, is_indexed=True),
'town': Unicode(mandatory=True, is_indexed=True),
'country': CountriesEnumerate(mandatory=True, is_indexed=True),
}
def get_record_kw(self, id):
kw = {}
record = self.get_record(id)
for key in self.record_properties.keys():
kw[key] = self.get_record_value(record, key)
return kw
def get_record_namespace(self, id):
namespace = self.get_record_kw(id)
namespace['country'] = CountriesEnumerate.get_value(
namespace['country'])
namespace['gender'] = Civilite.get_value(namespace['gender'])
return namespace
class Addresses(Table):
class_id = 'addresses'
class_title = MSG(u'Adresse')
class_handler = BaseAddresses
class_views = ['addresses_book', 'search']
@property
def class_views(self):
context = get_context()
# Back-Office
hostname = context.uri.authority
if hostname[:6] == 'admin.' :
return ['search']
return ['addresses_book']
# Views
addresses_book = Addresses_Book()
add_address = Addresses_AddAddress()
edit_address = Addresses_EditAddress()
search = Addresses_Search()
view = Table_View(access='is_admin')
last_changes = None
add_record = None
address_title = MSG(u"""
Please give a name to your address.
""")
address_tip = MSG(u"(Example: Home, Office)")
form = [
SelectRadio('gender', title=MSG(u'Genre')),
TextWidget('firstname', title=MSG(u'Firstname')),
TextWidget('lastname', title=MSG(u'Lastname')),
TextWidget('address_1', title=MSG(u'Address')),
TextWidget('address_2', title=MSG(u'Address (next)')),
TextWidget('zipcode', title=MSG(u'Zip Code')),
TextWidget('town', title=MSG(u'Town')),
SelectWidget('country', title=MSG(u'Country')),
TextWidget('title', title=address_title, tip=address_tip),
]
register_resource_class(Addresses)