Projet

Général

Profil

0001-misc-include-form-css-classes-when-inserted-in-links.patch

Frédéric Péters, 13 avril 2020 19:50

Télécharger (6,91 ko)

Voir les différences:

Subject: [PATCH] misc: include form css classes when inserted in links list
 cells (#41465)

 combo/apps/wcs/models.py                      | 18 +++++++++++++++
 .../combo/wcs/forms_of_category.html          | 10 ++-------
 .../templates/combo/link-list-cell.html       |  4 ++--
 tests/test_wcs.py                             | 22 +++++++++++++++++++
 4 files changed, 44 insertions(+), 10 deletions(-)
combo/apps/wcs/models.py
23 23
from django.db import models
24 24
from django.forms import models as model_forms
25 25
from django.forms import Select
26
from django.utils.text import slugify
26 27
from django.utils.translation import ugettext_lazy as _
27 28

  
28 29
from jsonfield import JSONField
......
43 44
}
44 45

  
45 46

  
47
def get_formdef_css_classes(formdef):
48
    classes = []
49
    if formdef.get('redirection'):
50
        classes.append('is-redirection')
51
    if formdef.get('authentication_required'):
52
        classes.append('required-authentication')
53
    for authn_context in formdef.get('required_authentication_contexts') or []:
54
        classes.append('required-%s-authentication' % authn_context)
55
    for keyword in formdef.get('keywords') or []:
56
        classes.append('keyword-%s' % slugify(keyword))
57
    return classes
58

  
59

  
46 60
@register_cell_class
47 61
class WcsFormCell(CellBase):
48 62
    formdef_reference = models.CharField(_('Form'), max_length=150)
......
118 132
        context['title'] = self.cached_title
119 133
        context['url'] = self.cached_url + 'tryauth'
120 134
        if self.cached_json:
135
            context['css_classes'] = get_formdef_css_classes(self.cached_json)
121 136
            for attribute in self.cached_json:
122 137
                if not attribute in context:
123 138
                    context[attribute] = self.cached_json.get(attribute)
......
652 667
                        form['order'] = 9999
653 668
            extra_context['forms'] = sorted(extra_context['forms'], key=lambda x: x.get('order', 9999))
654 669

  
670
        for formdef in extra_context['forms']:
671
            formdef['css_classes'] = get_formdef_css_classes(formdef)
672

  
655 673
        if self.limit:
656 674
            if len(extra_context['forms']) > self.limit:
657 675
                extra_context['more_forms'] = extra_context['forms'][self.limit:]
combo/apps/wcs/templates/combo/wcs/forms_of_category.html
17 17
<div class="wcs-forms-of-category-{{slug}}">
18 18
<ul>
19 19
{% for form in forms %}
20
<li class="{% if form.authentication_required %}required-authentication{% endif %}
21
           {% for context in form.required_authentication_contexts %}required-{{context}}-authentication {% endfor %}
22
           {% for keyword in form.keywords %}keyword-{{keyword|slugify}} {% endfor %}
23
           {% if form.redirection %}is-redirection{% endif %}"
20
<li class="{{ form.css_classes|join:" " }}"
24 21
  ><a href="{{ form.url }}tryauth">{{ form.title }}</a>
25 22
  {% if form.description %}<div class="description">{{ form.description|safe }}</div>{% endif %}
26 23
  </li>
......
29 26
{% if more_forms %}
30 27
<li class="more-items"><a>+</a></li>
31 28
{% for form in more_forms %}
32
<li style="display: none" class="additional-form {% if form.authentication_required %}required-authentication{% endif %}
33
           {% for context in form.required_authentication_contexts %}required-{{context}}-authentication {% endfor %}
34
           {% for keyword in form.keywords %}keyword-{{keyword|slugify}} {% endfor %}
35
           {% if form.redirection %}is-redirection{% endif %}"
29
<li style="display: none" class="additional-form {{ form.css_classes|join:" " }}"
36 30
  ><a href="{{ form.url }}tryauth">{{ form.title }}</a>
37 31
  {% if form.description %}<div class="description">{{ form.description|safe }}</div>{% endif %}
38 32
  </li>
combo/public/templates/combo/link-list-cell.html
5 5
  {% include "combo/asset_picture_fragment.html" %}
6 6
  <ul>
7 7
  {% for link in links %}
8
    <li><a href="{{ link.url }}">{{ link.title }}</a></li>
8
  <li class="{{ link.css_classes|default:""|join:" " }}"><a href="{{ link.url }}">{{ link.title }}</a></li>
9 9
  {% endfor %}
10 10

  
11 11
  {% if more_links %}
12 12
    <li class="more-items"><a>+</a></li>
13 13
  {% for link in more_links %}
14
    <li style="display: none" class="additional-links"><a href="{{ link.url }}">{{ link.title }}</a></li>
14
    <li style="display: none" class="additional-links {{ link.css_classes|default:""|join:" " }}"><a href="{{ link.url }}">{{ link.title }}</a></li>
15 15
  {% endfor %}
16 16
  {% endif %}
17 17
  </ul>
tests/test_wcs.py
83 83
formdef = FormDef()
84 84
formdef.name = 'form title'
85 85
formdef.category_id = cat.id
86
formdef.keywords = 'foo, bar'
86 87
formdef.fields = [
87 88
    fields.StringField(id='1', label='1st field', type='string'),
88 89
    fields.ItemField(id='2', label='2nd field', type='item',
......
744 745
    assert result.index('form title') > result.index('a second form title')
745 746
    assert 'http://127.0.0.1:8999/form-title/tryauth' in result
746 747
    assert 'http://127.0.0.1:8999/a-second-form-title/tryauth' in result
748
    assert 'keyword-foo' in result
749
    assert 'keyword-bar' in result
747 750

  
748 751
    cell.ordering = 'popularity'
749 752
    cell.save()
......
1281 1284
    assert new_item.cached_json == item.cached_json
1282 1285

  
1283 1286

  
1287
def test_list_of_links_with_form_render(app):
1288
    page = Page(title='xxx', slug='test_list_of_links_with_form_render', template_name='standard')
1289
    page.save()
1290

  
1291
    cell = LinkListCell.objects.create(order=0, placeholder='content', page=page)
1292
    WcsFormCell.objects.create(
1293
        page=page,
1294
        placeholder=cell.link_placeholder,
1295
        cached_title='A title',
1296
        cached_url='http://example.com',
1297
        cached_json={'keywords': ['bar']},
1298
        order=0,
1299
    )
1300

  
1301
    resp = app.get('/test_list_of_links_with_form_render/')
1302
    assert 'A title' in resp
1303
    assert 'keyword-bar' in resp
1304

  
1305

  
1284 1306
@wcs_present
1285 1307
def test_view_page_with_wcs_cells_num_queries(app, admin_user):
1286 1308
    page = Page.objects.create(title=u'bar', slug='index', order=1)
1287
-