1 | ## $Id: browser.py 13066 2015-06-16 14:48:55Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """UI components for products offered by the company. |
---|
19 | """ |
---|
20 | |
---|
21 | import sys |
---|
22 | import grok |
---|
23 | import pytz |
---|
24 | from urllib import urlencode |
---|
25 | from datetime import datetime |
---|
26 | from zope.event import notify |
---|
27 | from zope.i18n import translate |
---|
28 | from zope.catalog.interfaces import ICatalog |
---|
29 | from zope.component import queryUtility, getUtility, createObject |
---|
30 | from zope.schema.interfaces import ConstraintNotSatisfied, RequiredMissing |
---|
31 | from zope.formlib.textwidgets import BytesDisplayWidget |
---|
32 | from zope.security import checkPermission |
---|
33 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
34 | from waeup.ikoba.interfaces import ( |
---|
35 | IContactForm, IObjectHistory, IIkobaObject, IIkobaUtils) |
---|
36 | from waeup.ikoba.browser.layout import ( |
---|
37 | IkobaPage, IkobaEditFormPage, IkobaAddFormPage, IkobaDisplayFormPage, |
---|
38 | NullValidator, jsaction, action, UtilityView) |
---|
39 | from waeup.ikoba.widgets.datewidget import ( |
---|
40 | FriendlyDateWidget, FriendlyDateDisplayWidget, |
---|
41 | FriendlyDatetimeDisplayWidget) |
---|
42 | from waeup.ikoba.browser.breadcrumbs import Breadcrumb |
---|
43 | from waeup.ikoba.utils.helpers import html2dict |
---|
44 | from waeup.ikoba.browser.pages import ( |
---|
45 | delSubobjects, add_local_role, del_local_roles, msave, |
---|
46 | LocalRoleAssignmentUtilityView) |
---|
47 | |
---|
48 | from waeup.ikoba.products.interfaces import ( |
---|
49 | IProductsContainer, IProduct) |
---|
50 | |
---|
51 | grok.context(IIkobaObject) # Make IKofaObject the default context |
---|
52 | grok.templatedir('browser_templates') |
---|
53 | |
---|
54 | |
---|
55 | class ProductsBreadcrumb(Breadcrumb): |
---|
56 | """A breadcrumb for the customers container. |
---|
57 | """ |
---|
58 | grok.context(IProductsContainer) |
---|
59 | title = _('Products') |
---|
60 | |
---|
61 | |
---|
62 | class ProductBreadcrumb(Breadcrumb): |
---|
63 | """A breadcrumb for the customer container. |
---|
64 | """ |
---|
65 | grok.context(IProduct) |
---|
66 | |
---|
67 | def title(self): |
---|
68 | return self.context.title |
---|
69 | |
---|
70 | |
---|
71 | class ProductsContainerPage(IkobaDisplayFormPage): |
---|
72 | """The standard view for product containers. |
---|
73 | """ |
---|
74 | grok.context(IProductsContainer) |
---|
75 | grok.name('index') |
---|
76 | grok.require('waeup.Public') |
---|
77 | grok.template('containerpage') |
---|
78 | pnav = 1 |
---|
79 | label = _('Products') |
---|
80 | |
---|
81 | @property |
---|
82 | def description(self): |
---|
83 | lang = self.request.cookies.get('ikoba.language') |
---|
84 | html = self.context.description_dict.get(lang,'') |
---|
85 | if html =='': |
---|
86 | portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE |
---|
87 | html = self.context.description_dict.get(portal_language,'') |
---|
88 | return html |
---|
89 | |
---|
90 | |
---|
91 | class ProductsContainerManageFormPage(IkobaEditFormPage, |
---|
92 | LocalRoleAssignmentUtilityView): |
---|
93 | """The manage page for customer containers. |
---|
94 | """ |
---|
95 | grok.context(IProductsContainer) |
---|
96 | grok.name('manage') |
---|
97 | grok.require('waeup.manageProducts') |
---|
98 | grok.template('containermanagepage') |
---|
99 | pnav = 1 |
---|
100 | label = _('Manage product section') |
---|
101 | |
---|
102 | taboneactions = [_('Save'), _('Cancel')] |
---|
103 | tabtwoactions = [_('Add product'), _('Remove selected'), _('Cancel')] |
---|
104 | |
---|
105 | @action(_('Add product'), style='primary', validator=NullValidator) |
---|
106 | def addSubunit(self, **data): |
---|
107 | self.redirect(self.url(self.context, 'addproduct')) |
---|
108 | return |
---|
109 | |
---|
110 | @jsaction(_('Remove selected')) |
---|
111 | def delProducts(self, **data): |
---|
112 | delSubobjects(self, redirect='manage', tab='2') |
---|
113 | return |
---|
114 | |
---|
115 | @action(_('Save'), style='primary') |
---|
116 | def save(self, **data): |
---|
117 | msave(self, **data) |
---|
118 | # Save multilingual dict |
---|
119 | portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE |
---|
120 | desc = getattr(self.context, 'description', None) |
---|
121 | self.context.description_dict = html2dict(desc, portal_language) |
---|
122 | return |
---|
123 | |
---|
124 | @action(_('Cancel'), validator=NullValidator) |
---|
125 | def cancel(self, **data): |
---|
126 | self.redirect(self.url(self.context)) |
---|
127 | return |
---|
128 | |
---|
129 | |
---|
130 | class ProductAddFormPage(IkobaAddFormPage): |
---|
131 | """Add-form to add a customer. |
---|
132 | """ |
---|
133 | grok.context(IProductsContainer) |
---|
134 | grok.require('waeup.manageProducts') |
---|
135 | grok.name('addproduct') |
---|
136 | form_fields = grok.AutoFields(IProduct) |
---|
137 | label = _('Add product') |
---|
138 | pnav = 1 |
---|
139 | |
---|
140 | @action(_('Create product'), style='primary') |
---|
141 | def addProduct(self, **data): |
---|
142 | product = createObject(u'waeup.Product') |
---|
143 | self.applyData(product, **data) |
---|
144 | try: |
---|
145 | self.context.addProduct(product) |
---|
146 | except KeyError: |
---|
147 | self.flash(_('The id chosen already exists.'), |
---|
148 | type='danger') |
---|
149 | return |
---|
150 | self.flash( |
---|
151 | _("Product ${a} added.", mapping = {'a':data['product_id']})) |
---|
152 | ob_class = self.__implemented__.__name__.replace('waeup.ikoba.','') |
---|
153 | self.context.__parent__.logger.info( |
---|
154 | '%s - added: %s' % (ob_class, data['product_id'])) |
---|
155 | self.redirect(self.url(self.context, u'manage')+'#tab2') |
---|
156 | return |
---|
157 | |
---|
158 | |
---|
159 | class ProductDisplayFormPage(IkobaDisplayFormPage): |
---|
160 | """ Page to display product data |
---|
161 | """ |
---|
162 | grok.context(IProduct) |
---|
163 | grok.name('index') |
---|
164 | grok.require('waeup.Public') |
---|
165 | grok.template('productpage') |
---|
166 | #grok.template('basepage') |
---|
167 | pnav = 1 |
---|
168 | |
---|
169 | @property |
---|
170 | def form_fields(self): |
---|
171 | return grok.AutoFields(self.context.form_fields_interface).omit( |
---|
172 | 'terms_and_conditions', 'description') |
---|
173 | |
---|
174 | @property |
---|
175 | def label(self): |
---|
176 | return self.context.title |
---|
177 | |
---|
178 | @property |
---|
179 | def description(self): |
---|
180 | lang = self.request.cookies.get('ikoba.language') |
---|
181 | html = self.context.description_dict.get(lang,'') |
---|
182 | if html =='': |
---|
183 | portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE |
---|
184 | html = self.context.description_dict.get(portal_language,'') |
---|
185 | return html |
---|
186 | |
---|
187 | @property |
---|
188 | def terms_and_conditions(self): |
---|
189 | lang = self.request.cookies.get('ikoba.language') |
---|
190 | html = self.context.tc_dict.get(lang,'') |
---|
191 | if html =='': |
---|
192 | portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE |
---|
193 | html = self.context.tc_dict.get(portal_language,'') |
---|
194 | return html |
---|
195 | |
---|
196 | |
---|
197 | class ProductManageFormPage(IkobaEditFormPage, |
---|
198 | LocalRoleAssignmentUtilityView): |
---|
199 | """ View to manage product data |
---|
200 | """ |
---|
201 | grok.context(IProduct) |
---|
202 | grok.name('manage') |
---|
203 | grok.require('waeup.manageProducts') |
---|
204 | grok.template('productmanagepage') |
---|
205 | pnav = 1 |
---|
206 | |
---|
207 | taboneactions = [_('Save'), _('Cancel')] |
---|
208 | tabtwoactions1 = [_('Remove selected local roles')] |
---|
209 | tabtwoactions2 = [_('Add local role')] |
---|
210 | |
---|
211 | |
---|
212 | @property |
---|
213 | def form_fields(self): |
---|
214 | return grok.AutoFields( |
---|
215 | self.context.form_fields_interface).omit('product_id') |
---|
216 | |
---|
217 | def label(self): |
---|
218 | return _("Manage product ${a}", mapping = {'a':self.context.product_id}) |
---|
219 | |
---|
220 | @action(_('Save'), style='primary') |
---|
221 | def save(self, **data): |
---|
222 | msave(self, **data) |
---|
223 | # Save multilingual dicts |
---|
224 | portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE |
---|
225 | desc = getattr(self.context, 'description', None) |
---|
226 | self.context.description_dict = html2dict(desc, portal_language) |
---|
227 | tc = getattr(self.context, 'terms_and_conditions', None) |
---|
228 | self.context.tc_dict = html2dict(tc, portal_language) |
---|
229 | return |
---|
230 | |
---|
231 | @action(_('Cancel'), validator=NullValidator) |
---|
232 | def cancel(self, **data): |
---|
233 | self.redirect(self.url(self.context)) |
---|
234 | return |
---|
235 | |
---|
236 | @action(_('Add local role'), validator=NullValidator) |
---|
237 | def addLocalRole(self, **data): |
---|
238 | return add_local_role(self,2,**data) |
---|
239 | |
---|
240 | @action(_('Remove selected local roles')) |
---|
241 | def delLocalRoles(self, **data): |
---|
242 | return del_local_roles(self,2,**data) |
---|