wcs: uniformize forms links rendering (#65846)

This commit is contained in:
Serghei Mihai 2022-06-21 10:14:55 +02:00
parent 45d40596bc
commit 964461e27e
7 changed files with 46 additions and 27 deletions

View File

@ -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
@ -139,20 +138,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:

View File

@ -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 %}

View File

@ -1,7 +1,14 @@
{% firstof form.url url as form_url %}
{% firstof form.title title as form_title %}
{% autoescape off %}
{% firstof form.description description as form_description %}
{% endautoescape %}
{% 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 %}

View File

@ -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 %}

View File

@ -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">

View File

@ -75,7 +75,11 @@ def test_form_cell_save_cache(mock_send):
# check content provided to search engine
assert cell.render_for_search() == ''
assert cell.get_external_links_data() == [
{'title': 'form title', 'url': 'http://127.0.0.1:8999/form-title/', 'text': 'foo bar'}
{
'title': 'form title',
'url': 'http://127.0.0.1:8999/form-title/',
'text': '<p>a form description</p> foo bar',
}
]
# artificially change title
@ -240,6 +244,7 @@ def test_form_cell_render(mock_send):
result = cell.render({'request': RequestFactory().get('/')})
assert 'http://127.0.0.1:8999/form-title/tryauth' in result
assert 'form title' in result
assert '<p>a form description</p>' in result
@mock.patch('requests.Session.send', side_effect=mocked_requests_send)
@ -868,6 +873,7 @@ def test_forms_of_category_cell_render(mock_send, context):
assert 'http://127.0.0.1:8999/a-second-form-title/tryauth' in result
assert 'keyword-foo' in result
assert 'keyword-bar' in result
assert '<p>a form description</p>' in result
cell.ordering = 'popularity'
cell.save()
@ -929,7 +935,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/'
)

View File

@ -7,7 +7,13 @@ from unittest import mock
WCS_FORMDEFS_DATA = [
{'slug': 'a-private-form', 'title': 'a private form', 'url': '/a-private-form/'},
{'slug': 'a-second-form-title', 'title': 'a second form title', 'url': '/a-second-form-title/'},
{'slug': 'form-title', 'title': 'form title', 'url': '/form-title/', 'keywords': ['foo', 'bar']},
{
'slug': 'form-title',
'title': 'form title',
'url': '/form-title/',
'keywords': ['foo', 'bar'],
'description': '<p>a form description</p>',
},
{'slug': 'third-form-title', 'title': 'Third form title', 'url': '/third-form-title/'},
]
@ -22,6 +28,7 @@ WCS_CATEGORIES_FORMDEFS_DATA = [
'url': '/form-title/',
'keywords': ['foo', 'bar'],
'count': 42,
'description': '<p>a form description</p>',
},
{
'slug': 'a-second-form-title',