misc: include form css classes when inserted in links list cells (#41465)
This commit is contained in:
parent
1eec3f6d6d
commit
9c45076ded
|
@ -23,6 +23,7 @@ from django.conf import settings
|
|||
from django.db import models
|
||||
from django.forms import models as model_forms
|
||||
from django.forms import Select
|
||||
from django.utils.text import slugify
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from jsonfield import JSONField
|
||||
|
@ -43,6 +44,19 @@ invalid_reason_codes = {
|
|||
}
|
||||
|
||||
|
||||
def get_formdef_css_classes(formdef):
|
||||
classes = []
|
||||
if formdef.get('redirection'):
|
||||
classes.append('is-redirection')
|
||||
if formdef.get('authentication_required'):
|
||||
classes.append('required-authentication')
|
||||
for authn_context in formdef.get('required_authentication_contexts') or []:
|
||||
classes.append('required-%s-authentication' % authn_context)
|
||||
for keyword in formdef.get('keywords') or []:
|
||||
classes.append('keyword-%s' % slugify(keyword))
|
||||
return classes
|
||||
|
||||
|
||||
@register_cell_class
|
||||
class WcsFormCell(CellBase):
|
||||
formdef_reference = models.CharField(_('Form'), max_length=150)
|
||||
|
@ -118,6 +132,7 @@ class WcsFormCell(CellBase):
|
|||
context['title'] = self.cached_title
|
||||
context['url'] = self.cached_url + 'tryauth'
|
||||
if self.cached_json:
|
||||
context['css_classes'] = get_formdef_css_classes(self.cached_json)
|
||||
for attribute in self.cached_json:
|
||||
if not attribute in context:
|
||||
context[attribute] = self.cached_json.get(attribute)
|
||||
|
@ -652,6 +667,9 @@ class WcsFormsOfCategoryCell(WcsCommonCategoryCell, WcsBlurpMixin):
|
|||
form['order'] = 9999
|
||||
extra_context['forms'] = sorted(extra_context['forms'], key=lambda x: x.get('order', 9999))
|
||||
|
||||
for formdef in extra_context['forms']:
|
||||
formdef['css_classes'] = get_formdef_css_classes(formdef)
|
||||
|
||||
if self.limit:
|
||||
if len(extra_context['forms']) > self.limit:
|
||||
extra_context['more_forms'] = extra_context['forms'][self.limit:]
|
||||
|
|
|
@ -17,10 +17,7 @@
|
|||
<div class="wcs-forms-of-category-{{slug}}">
|
||||
<ul>
|
||||
{% for form in forms %}
|
||||
<li class="{% if form.authentication_required %}required-authentication{% endif %}
|
||||
{% for context in form.required_authentication_contexts %}required-{{context}}-authentication {% endfor %}
|
||||
{% for keyword in form.keywords %}keyword-{{keyword|slugify}} {% endfor %}
|
||||
{% if form.redirection %}is-redirection{% endif %}"
|
||||
<li class="{{ form.css_classes|join:" " }}"
|
||||
><a href="{{ form.url }}tryauth">{{ form.title }}</a>
|
||||
{% if form.description %}<div class="description">{{ form.description|safe }}</div>{% endif %}
|
||||
</li>
|
||||
|
@ -29,10 +26,7 @@
|
|||
{% if more_forms %}
|
||||
<li class="more-items"><a>+</a></li>
|
||||
{% for form in more_forms %}
|
||||
<li style="display: none" class="additional-form {% if form.authentication_required %}required-authentication{% endif %}
|
||||
{% for context in form.required_authentication_contexts %}required-{{context}}-authentication {% endfor %}
|
||||
{% for keyword in form.keywords %}keyword-{{keyword|slugify}} {% endfor %}
|
||||
{% if form.redirection %}is-redirection{% endif %}"
|
||||
<li style="display: none" class="additional-form {{ form.css_classes|join:" " }}"
|
||||
><a href="{{ form.url }}tryauth">{{ form.title }}</a>
|
||||
{% if form.description %}<div class="description">{{ form.description|safe }}</div>{% endif %}
|
||||
</li>
|
||||
|
|
|
@ -5,13 +5,13 @@
|
|||
<div class="links-list">
|
||||
<ul>
|
||||
{% for link in links %}
|
||||
<li><a href="{{ link.url }}">{{ link.title }}</a></li>
|
||||
<li class="{{ link.css_classes|default:""|join:" " }}"><a href="{{ link.url }}">{{ link.title }}</a></li>
|
||||
{% endfor %}
|
||||
|
||||
{% if more_links %}
|
||||
<li class="more-items"><a>+</a></li>
|
||||
{% for link in more_links %}
|
||||
<li style="display: none" class="additional-links"><a href="{{ link.url }}">{{ link.title }}</a></li>
|
||||
<li style="display: none" class="additional-links {{ link.css_classes|default:""|join:" " }}"><a href="{{ link.url }}">{{ link.title }}</a></li>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</ul>
|
||||
|
|
|
@ -248,22 +248,22 @@ def test_link_list_cell():
|
|||
)
|
||||
|
||||
ctx = {'page_cells': [item]}
|
||||
assert '<ul><li><a href="http://example.net/">Example Site</a></li></ul>' in cell.render(ctx)
|
||||
assert '<ul><li class=""><a href="http://example.net/">Example Site</a></li></ul>' in cell.render(ctx)
|
||||
|
||||
item.title = ''
|
||||
assert '<ul><li><a href="http://example.net/">http://example.net/</a></li></ul>' in cell.render(ctx)
|
||||
assert '<ul><li class=""><a href="http://example.net/">http://example.net/</a></li></ul>' in cell.render(ctx)
|
||||
|
||||
item.link_page = page
|
||||
assert '<ul><li><a href="/example-page/">example page</a></li></ul>' in cell.render(ctx)
|
||||
assert '<ul><li class=""><a href="/example-page/">example page</a></li></ul>' in cell.render(ctx)
|
||||
|
||||
item.title = 'altertitle'
|
||||
assert '<ul><li><a href="/example-page/">altertitle</a></li></ul>' in cell.render(ctx)
|
||||
assert '<ul><li class=""><a href="/example-page/">altertitle</a></li></ul>' in cell.render(ctx)
|
||||
|
||||
item.anchor = 'anchor'
|
||||
assert '<ul><li><a href="/example-page/#anchor">altertitle</a></li></ul>' in cell.render(ctx)
|
||||
assert '<ul><li class=""><a href="/example-page/#anchor">altertitle</a></li></ul>' in cell.render(ctx)
|
||||
|
||||
item.link_page = None
|
||||
assert '<ul><li><a href="http://example.net/#anchor">altertitle</a></li></ul>' in cell.render(ctx)
|
||||
assert '<ul><li class=""><a href="http://example.net/#anchor">altertitle</a></li></ul>' in cell.render(ctx)
|
||||
|
||||
item2 = LinkCell.objects.create(
|
||||
page=page,
|
||||
|
|
|
@ -83,6 +83,7 @@ for i in range(1, 10):
|
|||
formdef = FormDef()
|
||||
formdef.name = 'form title'
|
||||
formdef.category_id = cat.id
|
||||
formdef.keywords = 'foo, bar'
|
||||
formdef.fields = [
|
||||
fields.StringField(id='1', label='1st field', type='string'),
|
||||
fields.ItemField(id='2', label='2nd field', type='item',
|
||||
|
@ -292,7 +293,7 @@ def test_form_cell_save_cache():
|
|||
assert cell.get_external_links_data() == [
|
||||
{'title': 'form title',
|
||||
'url': 'http://127.0.0.1:8999/form-title/',
|
||||
'text': ''}]
|
||||
'text': 'foo bar'}]
|
||||
|
||||
# artificially change title
|
||||
WcsFormCell.objects.filter(id=cell.id).update(cached_title='XXX')
|
||||
|
@ -744,6 +745,8 @@ def test_forms_of_category_cell_render(context):
|
|||
assert result.index('form title') > result.index('a second form title')
|
||||
assert 'http://127.0.0.1:8999/form-title/tryauth' in result
|
||||
assert 'http://127.0.0.1:8999/a-second-form-title/tryauth' in result
|
||||
assert 'keyword-foo' in result
|
||||
assert 'keyword-bar' in result
|
||||
|
||||
cell.ordering = 'popularity'
|
||||
cell.save()
|
||||
|
@ -1281,6 +1284,25 @@ def test_import_export_pages_with_links():
|
|||
assert new_item.cached_json == item.cached_json
|
||||
|
||||
|
||||
def test_list_of_links_with_form_render(app):
|
||||
page = Page(title='xxx', slug='test_list_of_links_with_form_render', template_name='standard')
|
||||
page.save()
|
||||
|
||||
cell = LinkListCell.objects.create(order=0, placeholder='content', page=page)
|
||||
WcsFormCell.objects.create(
|
||||
page=page,
|
||||
placeholder=cell.link_placeholder,
|
||||
cached_title='A title',
|
||||
cached_url='http://example.com',
|
||||
cached_json={'keywords': ['bar']},
|
||||
order=0,
|
||||
)
|
||||
|
||||
resp = app.get('/test_list_of_links_with_form_render/')
|
||||
assert 'A title' in resp
|
||||
assert 'keyword-bar' in resp
|
||||
|
||||
|
||||
@wcs_present
|
||||
def test_view_page_with_wcs_cells_num_queries(app, admin_user):
|
||||
page = Page.objects.create(title=u'bar', slug='index', order=1)
|
||||
|
|
Loading…
Reference in New Issue