From 67ccd94126a2b228b6b7369f1398603b12e11544 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 13 Apr 2020 16:12:50 +0200
Subject: [PATCH] misc: include form css classes when inserted in links list
 cells (#41465)

---
 combo/apps/wcs/models.py                      | 22 ++++++++++++++++-
 .../combo/wcs/forms_of_category.html          | 10 ++------
 .../templates/combo/link-list-cell.html       |  4 ++--
 tests/test_cells.py                           | 12 +++++-----
 tests/test_wcs.py                             | 24 ++++++++++++++++++-
 5 files changed, 54 insertions(+), 18 deletions(-)

diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index a49ce727..ef3431db 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -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,20 @@ 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 +133,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 +668,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:]
@@ -672,7 +691,8 @@ class WcsFormsOfCategoryCell(WcsCommonCategoryCell, WcsBlurpMixin):
                 yield {
                     'url': formdef['url'],
                     'title': formdef['title'],
-                    'text': text}
+                    'text': text,
+                }
 
 
 @register_cell_class
diff --git a/combo/apps/wcs/templates/combo/wcs/forms_of_category.html b/combo/apps/wcs/templates/combo/wcs/forms_of_category.html
index 30cfd8be..fc00ed4a 100644
--- a/combo/apps/wcs/templates/combo/wcs/forms_of_category.html
+++ b/combo/apps/wcs/templates/combo/wcs/forms_of_category.html
@@ -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="{% for css_class in form.css_classes %}{{ css_class }} {% endfor %}"
   ><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 {% for css_class in form.css_classes %}{{ css_class }} {% endfor %}"
   ><a href="{{ form.url }}tryauth">{{ form.title }}</a>
   {% if form.description %}<div class="description">{{ form.description|safe }}</div>{% endif %}
   </li>
diff --git a/combo/public/templates/combo/link-list-cell.html b/combo/public/templates/combo/link-list-cell.html
index 8b4a4eb3..1776a3a4 100644
--- a/combo/public/templates/combo/link-list-cell.html
+++ b/combo/public/templates/combo/link-list-cell.html
@@ -5,13 +5,13 @@
   {% include "combo/asset_picture_fragment.html" %}
   <ul>
   {% for link in links %}
-    <li><a href="{{ link.url }}">{{ link.title }}</a></li>
+  <li class="{% for css_class in link.css_classes %}{{ css_class }} {% endfor %}"><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 {% for css_class in link.css_classes %}{{ css_class }} {% endfor %}"><a href="{{ link.url }}">{{ link.title }}</a></li>
   {% endfor %}
   {% endif %}
   </ul>
diff --git a/tests/test_cells.py b/tests/test_cells.py
index acf9a922..1e379d3f 100644
--- a/tests/test_cells.py
+++ b/tests/test_cells.py
@@ -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,
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index c1bbe70d..cdd035cb 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -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)
-- 
2.26.0

