admin: lateral template edition for Forms and Cards (#49661)

This commit is contained in:
Lauréline Guérin 2021-01-11 09:47:12 +01:00
parent ac8c2a1acc
commit f2f0feeceb
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
5 changed files with 75 additions and 14 deletions

View File

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import pytest
import re
from wcs import fields
from wcs.admin.settings import UserFieldsFormDef
@ -179,6 +180,51 @@ def test_card_workflow_change(pub):
resp = resp.form.submit('submit').follow()
def assert_option_display(resp, label, value):
option_line = re.findall('%s.*%s' % (label, value), resp.text, re.DOTALL)
assert option_line
assert '</li>' not in option_line
def test_card_templates(pub):
create_superuser(pub)
CardDef.wipe()
carddef = CardDef()
carddef.name = 'foo'
carddef.fields = [
fields.StringField(id='1', label='Test', type='string', varname='foo'),
]
carddef.store()
carddata = carddef.data_class()()
carddata.data = {'1': 'bar'}
carddata.store()
app = login(get_app(pub))
resp = app.get('/backoffice/cards/1/')
assert_option_display(resp, 'Templates', 'None')
resp = resp.click('Templates')
resp.form['digest_template'] = 'X{{form_var_test}}Y'
resp = resp.form.submit()
assert resp.location == 'http://example.net/backoffice/cards/1/'
resp = resp.follow()
assert_option_display(resp, 'Templates', 'Custom')
carddef = CardDef.get(carddef.id)
assert carddef.digest_template == 'X{{form_var_test}}Y'
assert carddef.lateral_template is None
assert 'Existing cards will be updated in the background.' in resp.text
resp = app.get('/backoffice/cards/1/options/templates')
resp.form['lateral_template'] = 'X{{form_var_test}}Y'
resp = resp.form.submit().follow()
assert_option_display(resp, 'Templates', 'Custom')
carddef = CardDef.get(carddef.id)
assert carddef.digest_template == 'X{{form_var_test}}Y'
assert carddef.lateral_template == 'X{{form_var_test}}Y'
assert 'Existing cards will be updated in the background.' not in resp.text
def test_card_digest_template(pub):
create_superuser(pub)
@ -190,7 +236,6 @@ def test_card_digest_template(pub):
]
carddef.digest_template = 'X{{ form_var_foo }}Y'
carddef.store()
carddef.data_class().wipe()
carddata = carddef.data_class()()
carddata.data = {'1': 'bar'}
carddata.store()
@ -214,6 +259,10 @@ def test_card_digest_template(pub):
assert carddef.digest_template is None
assert 'Existing cards will be updated in the background.' in resp.text
# afterjobs are actually run synchronously during tests; we don't have
# to wait to check the digest has been updated:
assert carddef.data_class().get(carddata.id).digest == 'XbarY'
carddef.digest_template = '{{ form_var_foo }}'
carddef.store()

View File

@ -816,9 +816,8 @@ def test_form_always_advertise(pub):
assert FormDef.get(1).always_advertise is True
def test_form_digest_template(pub):
def test_form_templates(pub):
create_superuser(pub)
role = create_role()
FormDef.wipe()
formdef = FormDef()
@ -834,22 +833,31 @@ def test_form_digest_template(pub):
app = login(get_app(pub))
resp = app.get('/backoffice/forms/1/')
# Display to unlogged users
assert_option_display(resp, 'Digest Template', 'None')
resp = resp.click('Digest Template')
assert_option_display(resp, 'Templates', 'None')
resp = resp.click('Templates')
resp.form['digest_template'] = 'X{{form_var_test}}Y'
resp = resp.form.submit()
assert resp.location == 'http://example.net/backoffice/forms/1/'
resp = resp.follow()
assert_option_display(resp, 'Digest Template', 'Custom')
assert_option_display(resp, 'Templates', 'Custom')
formdef = FormDef.get(formdef.id)
assert formdef.digest_template == 'X{{form_var_test}}Y'
assert formdef.lateral_template is None
assert 'Existing forms will be updated in the background.' in resp.text
# afterjobs are actually run synchronously during tests; we don't have
# to wait to check the digest has been updated:
assert formdef.data_class().get(formdata.id).digest == 'XhelloY'
resp = app.get('/backoffice/forms/1/options/templates')
resp.form['lateral_template'] = 'X{{form_var_test}}Y'
resp = resp.form.submit().follow()
assert_option_display(resp, 'Templates', 'Custom')
formdef = FormDef.get(formdef.id)
assert formdef.digest_template == 'X{{form_var_test}}Y'
assert formdef.lateral_template == 'X{{form_var_test}}Y'
assert 'Existing forms will be updated in the background.' not in resp.text
def test_form_delete(pub):
create_superuser(pub)

View File

@ -297,6 +297,8 @@ class OptionsDirectory(Directory):
form = Form(enctype='multipart/form-data')
form.add(StringWidget, 'digest_template', title=_('Digest'),
value=self.formdef.digest_template, size=50)
form.add(WysiwygTextWidget, 'lateral_template', title=_('Lateral Block'),
value=self.formdef.lateral_template)
result = self.handle(form, _('Templates'))
if self.changed and self.formdef.data_class().count():
get_response().add_after_job(UpdateDigestAfterJob(formdef=self.formdef))
@ -332,7 +334,7 @@ class OptionsDirectory(Directory):
'description', 'keywords', 'category_id',
'skip_from_360_view', 'geoloc_label', 'appearance_keywords',
'include_download_all_button',
'digest_template', 'drafts_lifespan', 'user_support']
'digest_template', 'lateral_template', 'drafts_lifespan', 'user_support']
for attr in attrs:
widget = form.get_widget(attr)
if widget:
@ -348,7 +350,8 @@ class OptionsDirectory(Directory):
else:
new_value = widget.parse()
if getattr(self.formdef, attr, None) != new_value:
self.changed = True
if attr == 'digest_template':
self.changed = True
setattr(self.formdef, attr, new_value)
if not form.has_errors():
self.formdef.store(comment=_('Changed "%s" parameters') % title)
@ -613,12 +616,12 @@ class FormDefPage(Directory):
self.formdef.appearance_keywords and
self.formdef.appearance_keywords or C_('appearance|Standard'))
if self.formdef.digest_template:
if self.formdef.digest_template or self.formdef.lateral_template:
digest_template_status = C_('template|Custom')
else:
digest_template_status = C_('template|None')
r += add_option_line('options/templates',
_('Digest Template'), digest_template_status)
_('Templates'), digest_template_status)
online_status = C_('online status|Active')
if self.formdef.disabled:

View File

@ -165,12 +165,12 @@ class CardDefPage(FormDefPage):
self.formdef.geolocations and
C_('geolocation|Enabled') or C_('geolocation|Disabled'))
if self.formdef.digest_template:
if self.formdef.digest_template or self.formdef.lateral_template:
digest_template_status = C_('template|Custom')
else:
digest_template_status = C_('template|None')
r += add_option_line('options/templates',
_('Digest Template'), digest_template_status)
_('Templates'), digest_template_status)
if self.formdef.user_support == 'optional':
user_support_status = C_('user_support|Optional')
else:

View File

@ -117,6 +117,7 @@ class FormDef(StorableObject):
include_download_all_button = False
appearance_keywords = None
digest_template = None
lateral_template = None
drafts_lifespan = None
user_support = None
@ -134,7 +135,7 @@ class FormDef(StorableObject):
TEXT_ATTRIBUTES = ['name', 'url_name', 'description', 'keywords',
'publication_date', 'expiration_date', 'internal_identifier',
'disabled_redirection', 'appearance_keywords',
'digest_template', 'drafts_lifespan', 'user_support']
'digest_template', 'lateral_template', 'drafts_lifespan', 'user_support']
BOOLEAN_ATTRIBUTES = ['discussion', 'detailed_emails', 'disabled',
'only_allow_one', 'enable_tracking_codes', 'confirmation',
'always_advertise', 'include_download_all_button',