-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforms_generator.py
208 lines (165 loc) · 7.09 KB
/
forms_generator.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# -*- coding: UTF-8 -*-
# Copyright (C) 2010 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 Email, Enumerate, Unicode, Boolean, String
from itools.gettext import MSG
from itools.uri import get_reference
from itools.web import get_context
from itools.xml import XMLParser
# Import from ikaaro
from ikaaro.forms import AutoForm, SelectWidget, TextWidget, BooleanCheckBox
from ikaaro.forms import RTEWidget, XHTMLBody, BooleanRadio
from ikaaro.table import OrderedTable, OrderedTableFile
from ikaaro.table_views import OrderedTable_View
# Import from itws
from itws.views import AutomaticEditView
# Import from shop
from cross_selling_views import AddProduct_View
from products.models import get_real_datatype
from products.enumerate import Datatypes
from registry import shop_widgets
from utils import get_shop
class Widgets(Enumerate):
@classmethod
def get_options(cls):
options = []
for name, title, widget in shop_widgets:
options.append({'name': name, 'value': title})
return options
@classmethod
def get_widget(cls, widget_name):
for name, title, widget in shop_widgets:
if name == widget_name:
return widget
raise ValueError
class ShopForm_Display(AutoForm):
access = 'is_allowed_to_view_for_authenticated'
def get_submit_value(self):
context = get_context()
return context.resource.get_property('submit_value')
submit_value = property(get_submit_value, None, None, '')
def get_title(self, context):
return context.resource.get_title()
def get_value(self, resource, context, name, datatype):
return context.query.get(name) or datatype.get_default()
def get_namespace(self, resource, context):
namespace = AutoForm.get_namespace(self, resource, context)
namespace['required_msg'] = (resource.get_property('introduction') +
list(XMLParser('<br/><br/>')) +
list(namespace['required_msg']))
return namespace
def get_schema(self, resource, context):
schema = {}
handler = resource.handler
get_value = handler.get_record_value
for record in handler.get_records():
name = get_value(record, 'name')
datatype = get_real_datatype(handler, record)
datatype.name = name
schema[name] = datatype
return schema
def get_query_schema(self):
context = get_context()
resource = context.resource
schema = {}
for key, datatype in self.get_schema(resource, context).items():
datatype.mandatory = False
schema[key] = datatype
return schema
def get_widgets(self, resource, context):
widgets = []
handler = resource.handler
get_value = handler.get_record_value
for record in handler.get_records_in_order():
name = get_value(record, 'name')
widget = Widgets.get_widget(get_value(record, 'widget'))
title = get_value(record, 'title')
widget = widget(name, title=title, has_empty_option=False)
widgets.append(widget)
return widgets
def action(self, resource, context, form):
root = context.root
to_addr = resource.get_property('to_addr')
subject = MSG(u'Message from form: "%s"' % resource.get_title()).gettext()
text = []
handler = resource.handler
get_value = handler.get_record_value
from_addr = to_addr
for record in handler.get_records_in_order():
name = get_value(record, 'name')
datatype = get_value(record, 'datatype')
value = form[name]
# XXX Render on datatype ?
if datatype == 'product':
shop = get_shop(resource)
site_root = resource.get_site_root()
product = context.root.get_resource(value)
base_uri = shop.get_property('shop_uri')
end_uri = site_root.get_pathto(product)
value = get_reference(base_uri).resolve(end_uri)
elif datatype == 'email':
# XXX Set from_addr
from_addr = value
title = get_value(record, 'title')
text.append('*%s* \n\n %s' % (title, value))
text = '\n\n\n'.join(text)
root.send_email(to_addr, subject, from_addr=from_addr, text=text,
subject_with_host=False)
return resource.get_property('final_message')
class ShopFormTable(OrderedTableFile):
record_properties = {
'name': String,
'title': Unicode(mandatory=True, multiple=True),
'mandatory': Boolean,
'multiple': Boolean,
'datatype': Datatypes(mandatory=True, index='keyword'),
'widget': Widgets(mandatory=True),
}
class ShopForm(OrderedTable):
class_id = 'shop-form'
class_title = MSG(u'Shop form')
class_version = '20090609'
class_handler = ShopFormTable
class_views = ['display', 'edit', 'view', 'add_record']
display = ShopForm_Display()
view = OrderedTable_View(search_template=None, access='is_admin')
edit = AutomaticEditView()
add_product = AddProduct_View()
form = [
TextWidget('name', title=MSG(u'Name')),
TextWidget('title', title=MSG(u'Title')),
BooleanCheckBox('mandatory', title=MSG(u'Mandatory')),
BooleanCheckBox('multiple', title=MSG(u'Multiple')),
SelectWidget('datatype', title=MSG(u'Data Type')),
SelectWidget('widget', title=MSG(u'Widget')),
]
edit_widgets = [
TextWidget('submit_value', title=MSG(u'Submit value')),
TextWidget('to_addr', title=MSG(u'To addr')),
RTEWidget('introduction', title=MSG(u'Introduction')),
RTEWidget('final_message', title=MSG(u'Final message')),
BooleanRadio('must_be_authentificated',
title=MSG(u'Must be authentificated to see form'))]
edit_schema = {'submit_value': Unicode(multilingual=True, mandatory=True),
'to_addr': Email(mandatory=True),
'introduction': XHTMLBody(multilingual=True),
'final_message': XHTMLBody(multilingual=True),
'must_be_authentificated': Boolean}
@classmethod
def get_metadata_schema(cls):
return merge_dicts(OrderedTable.get_metadata_schema(),
cls.edit_schema)