0001-cards-empty-digest-template-when-datasource-custom-v.patch
tests/test_cards_admin_pages.py | ||
---|---|---|
137 | 137 |
resp = resp.form.submit('submit').follow() |
138 | 138 | |
139 | 139 | |
140 |
def test_card_digest_template(pub, studio): |
|
141 |
create_superuser(pub) |
|
142 | ||
143 |
CardDef.wipe() |
|
144 |
carddef = CardDef() |
|
145 |
carddef.name = 'foo' |
|
146 |
carddef.fields = [ |
|
147 |
fields.StringField(id='1', label='Test', type='string', varname='foo'), |
|
148 |
] |
|
149 |
carddef.digest_template = 'X{{ form_var_foo }}Y' |
|
150 |
carddef.store() |
|
151 |
carddef.data_class().wipe() |
|
152 |
carddata = carddef.data_class()() |
|
153 |
carddata.data = {'1': 'bar'} |
|
154 |
carddata.store() |
|
155 | ||
156 |
# no custom view as datasource, it's ok to empty digest_template |
|
157 |
app = login(get_app(pub)) |
|
158 |
resp = app.get('/backoffice/cards/%s/options/templates' % carddef.id) |
|
159 |
resp.form['digest_template'] = '' |
|
160 |
resp = resp.form.submit().follow() |
|
161 |
carddef = CardDef.get(carddef.id) |
|
162 |
assert carddef.digest_template is None |
|
163 |
assert 'Existing cards will be updated in the background.' in resp.text |
|
164 | ||
165 |
carddef.digest_template = '{{ form_var_foo }}' |
|
166 |
carddef.store() |
|
167 | ||
168 |
custom_view = pub.custom_view_class() |
|
169 |
custom_view.title = 'card view' |
|
170 |
custom_view.formdef = carddef |
|
171 |
custom_view.columns = {'list': [{'id': 'id'}]} |
|
172 |
custom_view.filters = {'filter-1': True, 'filter-1-value': 'FOO'} |
|
173 |
custom_view.visibility = 'any' |
|
174 |
custom_view.store() |
|
175 | ||
176 |
# no custom view as datasource, it's ok to empty digest_template |
|
177 |
app = login(get_app(pub)) |
|
178 |
resp = app.get('/backoffice/cards/%s/options/templates' % carddef.id) |
|
179 |
resp.form['digest_template'] = '' |
|
180 |
resp = resp.form.submit().follow() |
|
181 |
carddef = CardDef.get(carddef.id) |
|
182 |
assert carddef.digest_template is None |
|
183 |
assert 'Existing cards will be updated in the background.' in resp.text |
|
184 | ||
185 |
carddef.digest_template = '{{ form_var_foo }}' |
|
186 |
carddef.store() |
|
187 | ||
188 |
# custom view as datasource, can not empty digest_template |
|
189 |
custom_view.visibility = 'datasource' |
|
190 |
custom_view.store() |
|
191 |
app = login(get_app(pub)) |
|
192 |
resp = app.get('/backoffice/cards/%s/options/templates' % carddef.id) |
|
193 |
resp.form['digest_template'] = '' |
|
194 |
resp = resp.form.submit() |
|
195 |
assert 'Can not empty digest template: there are data source custom views.' in resp.text |
|
196 |
carddef = CardDef.get(carddef.id) |
|
197 |
assert carddef.digest_template is not None |
|
198 | ||
199 |
# error: not stored, and no after jobs |
|
200 |
resp = app.get('/backoffice/cards/%s/' % carddef.id) |
|
201 |
assert 'Existing cards will be updated in the background.' not in resp.text |
|
202 | ||
203 | ||
140 | 204 |
def test_card_custom_view_data_source(pub, studio): |
141 | 205 |
user = create_superuser(pub) |
142 | 206 |
Role.wipe() |
wcs/admin/forms.py | ||
---|---|---|
40 | 40 |
from wcs.qommon.afterjobs import AfterJob |
41 | 41 | |
42 | 42 |
from wcs.formdef import FormDef, FormdefImportError, FormdefImportRecoverableError |
43 |
from wcs.carddef import CardDef |
|
43 | 44 |
from wcs.categories import Category |
44 | 45 |
from wcs.roles import Role, logged_users_role, get_user_roles |
45 | 46 |
from wcs.workflows import Workflow |
... | ... | |
279 | 280 |
for formdata in self.formdef.data_class().select(order_by='id'): |
280 | 281 |
formdata.store() |
281 | 282 |
job = get_response().add_after_job(N_('Updating digests'), update) |
282 |
get_session().message = ('info', |
|
283 |
_('Existing forms will be updated in the background.')) |
|
283 |
if isinstance(self.formdef, CardDef): |
|
284 |
get_session().message = ('info', |
|
285 |
_('Existing cards will be updated in the background.')) |
|
286 |
else: |
|
287 |
get_session().message = ('info', |
|
288 |
_('Existing forms will be updated in the background.')) |
|
284 | 289 |
return result |
285 | 290 | |
286 | 291 |
def handle(self, form, title): |
... | ... | |
302 | 307 |
for attr in attrs: |
303 | 308 |
widget = form.get_widget(attr) |
304 | 309 |
if widget: |
310 |
if hasattr(self, 'clean_%s' % attr): |
|
311 |
has_error = getattr(self, 'clean_%s' % attr)(form) |
|
312 |
if has_error: |
|
313 |
continue |
|
305 | 314 |
if attr == 'geoloc_label': |
306 | 315 |
if widget.parse(): |
307 | 316 |
self.formdef.geolocations = {'base': widget.parse()} |
... | ... | |
312 | 321 |
if getattr(self.formdef, attr, None) != new_value: |
313 | 322 |
self.changed = True |
314 | 323 |
setattr(self.formdef, attr, new_value) |
315 |
self.formdef.store(comment=_('Changed "%s" parameters') % title) |
|
316 |
return redirect('..') |
|
324 |
if not form.has_errors(): |
|
325 |
self.formdef.store(comment=_('Changed "%s" parameters') % title) |
|
326 |
return redirect('..') |
|
317 | 327 | |
318 | 328 |
html_top('forms', title=self.formdef.name) |
319 | 329 |
r = TemplateIO(html=True) |
... | ... | |
321 | 331 |
r += form.render() |
322 | 332 |
return r.getvalue() |
323 | 333 | |
334 |
def clean_digest_template(self, form): |
|
335 |
if not isinstance(self.formdef, CardDef): |
|
336 |
return False |
|
337 | ||
338 |
widget = form.get_widget('digest_template') |
|
339 |
new_value = widget.parse() |
|
340 |
if new_value: |
|
341 |
return False |
|
342 | ||
343 |
clauses = [Equal('formdef_type', 'carddef'), Equal('visibility', 'datasource')] |
|
344 |
custom_views = get_publisher().custom_view_class.count(clauses) |
|
345 |
if custom_views: |
|
346 |
widget.set_error(_('Can not empty digest template: there are data source custom views.')) |
|
347 |
return True |
|
348 | ||
349 |
return False |
|
350 | ||
324 | 351 | |
325 | 352 |
class WorkflowRoleDirectory(Directory): |
326 | 353 |
def __init__(self, formdef): |
327 |
- |