-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddresses_views.py
148 lines (111 loc) · 5.05 KB
/
addresses_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
138
139
140
141
142
143
144
145
146
147
148
# -*- 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.datatypes import String, Unicode
from itools.gettext import MSG
from itools.web import FormError, STLView
# Import from ikaaro
from ikaaro.forms import SelectWidget, TextWidget
from ikaaro.table_views import Table_EditRecord, Table_AddRecord
from ikaaro.table_views import Table_AddEditRecord
# Import from shop
from countries import CountriesEnumerate
from utils import get_shop
from utils_views import SearchTable_View
class Addresses_Book(STLView):
access = 'is_authenticated'
title = MSG(u'My address book')
template = '/ui/shop/addresses_book.xml'
def get_namespace(self, resource, context):
namespace = {'addresses': []}
shop = resource.get_site_root().get_resource('shop')
addresses = shop.get_resource('addresses').handler
for record in addresses.search(user=context.user.name):
ns = {'id': record.id}
ns.update(shop.get_user_address_namespace(record.id))
namespace['addresses'].append(ns)
return namespace
class Addresses_AddAddress(Table_AddRecord):
access = 'is_authenticated'
title = MSG(u'Fill a new address')
def action_add_or_edit(self, resource, context, record):
# We add current user
record['user'] = context.user.name
# Normal action
Table_AddRecord.action_add_or_edit(self, resource, context, record)
def action_on_success(self, resource, context):
return context.come_back(MSG(u'New address added.'),
goto=';addresses_book')
class Addresses_Search(SearchTable_View):
access = 'is_admin'
search_schema = {'user': String,
'firstname': Unicode,
'lastname': Unicode,
'zipcode': String,
'town': Unicode,
'country': CountriesEnumerate}
search_widgets = [TextWidget('user', title=MSG(u'Id user')),
TextWidget('firstname', title=MSG(u'Firstname')),
TextWidget('lastname', title=MSG(u'Lastname')),
TextWidget('zipcode', title=MSG(u'Zipcode')),
TextWidget('town', title=MSG(u'Town')),
SelectWidget('country', title=MSG(u'Country'))]
def get_table_columns(self, resource, context):
return (SearchTable_View.get_table_columns(self, resource, context) +
[('user', MSG(u'Id user'))])
def get_item_value(self, resource, context, item, column):
if column == 'user':
handler = resource.handler
value = handler.get_record_value(item, column)
return value, '/users/%s' % value
return SearchTable_View.get_item_value(self, resource, context,
item, column)
class Addresses_EditAddress(Table_EditRecord):
access = 'is_authenticated'
title = MSG(u'Edit address')
def is_allowed_to_edit_record(self, handler, context):
""" We check that user is allowed to edit record """
query = Table_AddEditRecord.get_query(self, context)
id_record = query['id']
record = handler.get_record(id_record)
if handler.get_record_value(record, 'user') != context.user.name:
raise FormError, MSG(u'You are not authorized to do that !')
return True
def get_query(self, context):
shop = get_shop(context.resource)
query = Table_AddEditRecord.get_query(self, context)
# Test the id is valid
id = query['id']
resource = shop.get_resource('addresses')
handler = resource.get_handler()
record = handler.get_record(id)
if record is None:
context.query = query
raise FormError, MSG(u'The {id} record is missing.', id=id)
# Is authorized ?
self.is_allowed_to_edit_record(resource.handler, context)
# Ok
return query
def action_add_or_edit(self, resource, context, record):
# Is authorized ?
self.is_allowed_to_edit_record(resource.handler, context)
# We add current user
record['user'] = context.user.name
# Normal action
Table_EditRecord.action_add_or_edit(self, resource, context, record)
def action_on_success(self, resource, context):
return context.come_back(MSG(u'Address modified'),
goto=';addresses_book')