From 2f8b940cbde270954d8f39d9d6f88514cd64ec15 Mon Sep 17 00:00:00 2001
From: Serghei Mihai <smihai@entrouvert.com>
Date: Tue, 21 Jun 2022 10:14:55 +0200
Subject: [PATCH] wcs: uniformize forms links rendering (#65846)

---
 combo/apps/wcs/models.py                      | 23 +++++++++----------
 combo/apps/wcs/templates/combo/wcs/form.html  |  8 ++-----
 .../combo/wcs/form_link_fragment.html         | 10 +++++---
 .../combo/wcs/forms_of_category.html          |  4 ++--
 .../templates/combo/link-list-cell.html       |  6 ++++-
 tests/wcs/test_all.py                         |  2 +-
 6 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index 55852a76..42513ff2 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -18,7 +18,6 @@
 import collections
 import copy
 import logging
-import urllib.parse
 
 from django.conf import settings
 from django.contrib.postgres.fields import JSONField
@@ -138,20 +137,20 @@ class WcsFormCell(CellBase):
         populate_cache()
 
     def get_cell_extra_context(self, context):
-        request = context.get('request')
-        context = super().get_cell_extra_context(context)
-        context['slug'] = self.formdef_reference.split(':')[-1]
-        context['title'] = self.cached_title
-        context['url'] = self.cached_url
-        if not is_a_bot(request):
-            context['url'] += 'tryauth?cancelurl=%s' % urllib.parse.quote(request.build_absolute_uri())
+        extra_context = super().get_cell_extra_context(context)
+        extra_context['slug'] = self.formdef_reference.split(':')[-1]
+        extra_context['title'] = self.cached_title
+        extra_context['url'] = self.cached_url
+        extra_context['request_is_a_bot'] = is_a_bot(context.get('request'))
+        extra_context['is_form'] = True
+
         if self.cached_json:
-            context['description'] = mark_safe(self.cached_json.get('description', ''))
-            context['css_classes'] = get_formdef_css_classes(self.cached_json)
+            extra_context['description'] = mark_safe(self.cached_json.get('description', ''))
+            extra_context['css_classes'] = get_formdef_css_classes(self.cached_json)
             for attribute in self.cached_json:
                 if attribute not in context:
-                    context[attribute] = self.cached_json.get(attribute)
-        return context
+                    extra_context[attribute] = self.cached_json.get(attribute)
+        return extra_context
 
     def get_additional_label(self):
         if not self.cached_title:
diff --git a/combo/apps/wcs/templates/combo/wcs/form.html b/combo/apps/wcs/templates/combo/wcs/form.html
index a70c5f22..ccbc67cb 100644
--- a/combo/apps/wcs/templates/combo/wcs/form.html
+++ b/combo/apps/wcs/templates/combo/wcs/form.html
@@ -1,9 +1,5 @@
 {% block cell-content %}
-<div class="wcs-form-{{slug}}"><a href="{{ url }}">{{ title }}</a>
-{% if description %}
-<div class="description">
-{{ description }}
-</div>
-{% endif %}
+<div class="wcs-form-{{slug}}">
+{% include "combo/wcs/form_link_fragment.html" %}
 </div>
 {% endblock %}
diff --git a/combo/apps/wcs/templates/combo/wcs/form_link_fragment.html b/combo/apps/wcs/templates/combo/wcs/form_link_fragment.html
index b5bf08eb..22cd0ca8 100644
--- a/combo/apps/wcs/templates/combo/wcs/form_link_fragment.html
+++ b/combo/apps/wcs/templates/combo/wcs/form_link_fragment.html
@@ -1,7 +1,11 @@
+{% firstof form.url url as form_url %}
+{% firstof form.description description as form_description %}
+{% firstof form.title title as form_title %}
+
 {% block form-link-pre %}{% endblock %}
-<a href="{{ form.url }}{% if not request_is_a_bot %}tryauth?cancelurl={{ uri|iriencode }}{% endif %}">{% block form-link-title %}{{ form.title }}{% endblock %}</a>
-{% if form.description %}
-<div class="description">{% block form-link-description %}{{ form.description|safe }}{% endblock %}</div>
+<a href="{{ form_url }}{% if not request_is_a_bot %}tryauth?cancelurl={{ absolute_uri|urlencode|iriencode }}{% endif %}">{% block form-link-title %}{{ form_title }}{% endblock %}</a>
+{% if form_description %}
+<div class="description">{% block form-link-description %}{{ form_description|safe }}{% endblock %}</div>
 {% endif %}
 {% block form-link-post %}{% endblock %}
 
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 fd206cc1..26d9a7c9 100644
--- a/combo/apps/wcs/templates/combo/wcs/forms_of_category.html
+++ b/combo/apps/wcs/templates/combo/wcs/forms_of_category.html
@@ -15,7 +15,7 @@
 {% block cell-top-content %}{% endblock cell-top-content %}
 <ul>
 {% for form in forms %}
-<li class="{{ form.css_classes|join:" " }}">{% include "combo/wcs/form_link_fragment.html" with form=form uri=absolute_uri %}</li>
+<li class="{{ form.css_classes|join:" " }}">{% include "combo/wcs/form_link_fragment.html" %}</li>
 {% endfor %}
 {% if more_forms %}
   <li class="add-more-items">
@@ -28,7 +28,7 @@
 <ul style="display: none" class="more-items" id="more-items-{{ cell.get_reference }}" aria-labelledby="btn-wcs-more-items-{{ cell.get_reference }}">
 {% for form in more_forms %}
 <li class="more-items--item {{ form.css_classes|join:" " }}">
-  {% include "combo/wcs/form_link_fragment.html" with form=form uri=absolute_uri %}
+  {% include "combo/wcs/form_link_fragment.html" %}
 </li>
 {% endfor %}
 {% endif %}
diff --git a/combo/public/templates/combo/link-list-cell.html b/combo/public/templates/combo/link-list-cell.html
index 61012292..fb6657e2 100644
--- a/combo/public/templates/combo/link-list-cell.html
+++ b/combo/public/templates/combo/link-list-cell.html
@@ -9,7 +9,11 @@
   {% block cell-top-content %}{% endblock cell-top-content %}
   <ul>
   {% for link in links %}
-  <li class="{{ link.css_classes|default:""|join:" " }}{% if link.cell.extra_css_class %} {{ link.cell.extra_css_class }}{% endif %}"><a href="{{ link.url }}">{{ link.title }}</a></li>
+  <li class="{{ link.css_classes|default:""|join:" " }}{% if link.cell.extra_css_class %} {{ link.cell.extra_css_class }}{% endif %}">{% if link.is_form %}
+    {% include "combo/wcs/form_link_fragment.html" with form=link %}
+    {% else %}
+    <a href="{{ link.url }}">{{ link.title }}</a>
+    {% endif %}</li>
   {% endfor %}
   {% if more_links %}
   <li class="add-more-items">
diff --git a/tests/wcs/test_all.py b/tests/wcs/test_all.py
index 8bd79859..84037fcb 100644
--- a/tests/wcs/test_all.py
+++ b/tests/wcs/test_all.py
@@ -929,7 +929,7 @@ def test_forms_of_category_cancelurl(mock_send, app):
     cell_resp = app.get(ajax_cell_url + '?ctx=' + extra_ctx)
     assert (
         PyQuery(cell_resp.text).find('a').attr('href')
-        == 'http://127.0.0.1:8999/a-second-form-title/tryauth?cancelurl=http://testserver/test_forms_of_category_cell_render/'
+        == 'http://127.0.0.1:8999/a-second-form-title/tryauth?cancelurl=http%3A//testserver/test_forms_of_category_cell_render/'
     )
 
 
-- 
2.37.2

