From 4bb2a0e2f522eff81cddafd717368449b7ba5658 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Fri, 4 Feb 2022 08:25:38 +0100
Subject: [PATCH 3/3] rename "parameters" to "extra variables"

---
 ...e_parameters.py => 0053_page_variables.py} |  2 +-
 combo/data/models.py                          | 10 ++++-----
 combo/manager/forms.py                        |  6 ++---
 ...ameters.html => page_extra_variables.html} |  2 +-
 combo/manager/templates/combo/page_view.html  |  9 +++++---
 combo/manager/urls.py                         |  6 ++---
 combo/manager/views.py                        | 21 +++++++++---------
 combo/public/views.py                         |  2 +-
 tests/test_manager.py                         | 22 +++++++++----------
 tests/test_public.py                          |  6 ++---
 10 files changed, 44 insertions(+), 42 deletions(-)
 rename combo/data/migrations/{0053_page_parameters.py => 0053_page_variables.py} (91%)
 rename combo/manager/templates/combo/{page_parameters.html => page_extra_variables.html} (97%)

diff --git a/combo/data/migrations/0053_page_parameters.py b/combo/data/migrations/0053_page_variables.py
similarity index 91%
rename from combo/data/migrations/0053_page_parameters.py
rename to combo/data/migrations/0053_page_variables.py
index 1d56ed77..5359de39 100644
--- a/combo/data/migrations/0053_page_parameters.py
+++ b/combo/data/migrations/0053_page_variables.py
@@ -11,7 +11,7 @@ class Migration(migrations.Migration):
     operations = [
         migrations.AddField(
             model_name='page',
-            name='parameters',
+            name='extra_variables',
             field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
         ),
     ]
diff --git a/combo/data/models.py b/combo/data/models.py
index 6aaa6fd0..e6179484 100644
--- a/combo/data/models.py
+++ b/combo/data/models.py
@@ -194,7 +194,7 @@ class Page(models.Model):
     order = models.PositiveIntegerField()
     exclude_from_navigation = models.BooleanField(_('Exclude from navigation'), default=True)
     redirect_url = models.CharField(_('Redirect URL'), max_length=200, blank=True)
-    parameters = JSONField(blank=True, default=dict)
+    extra_variables = JSONField(blank=True, default=dict)
 
     public = models.BooleanField(_('Public'), default=True)
     groups = models.ManyToManyField(Group, verbose_name=_('Groups'), blank=True)
@@ -642,11 +642,11 @@ class Page(models.Model):
     def get_last_update_time(self):
         return self.last_update_timestamp
 
-    def get_parameters(self, request, original_context):
+    def get_extra_variables(self, request, original_context):
         result = {}
         context = RequestContext(request)
         context.push(original_context)
-        for key, tplt in (self.parameters or {}).items():
+        for key, tplt in (self.extra_variables or {}).items():
             try:
                 template = Template(tplt)
             except TemplateSyntaxError:
@@ -654,8 +654,8 @@ class Page(models.Model):
             result[key] = template.render(context)
         return result
 
-    def get_parameters_keys(self):
-        return sorted((self.parameters or {}).keys())
+    def get_extra_variables_keys(self):
+        return sorted((self.extra_variables or {}).keys())
 
     def is_new(self):
         return self.creation_timestamp > timezone.now() - datetime.timedelta(days=7)
diff --git a/combo/manager/forms.py b/combo/manager/forms.py
index 06aaf5d3..2556353b 100644
--- a/combo/manager/forms.py
+++ b/combo/manager/forms.py
@@ -169,14 +169,14 @@ class PageEditPictureForm(forms.ModelForm):
         field_classes = {'picture': ImageIncludingSvgField}
 
 
-class PageEditParameterForm(forms.Form):
-    key = forms.CharField(label=_('Property name'), required=False)
+class PageEditExtraVariableForm(forms.Form):
+    key = forms.CharField(label=_('Variable name'), required=False)
     value = forms.CharField(
         label=_('Value template'), widget=forms.TextInput(attrs={'size': 60}), required=False
     )
 
 
-PageEditParameterFormSet = formset_factory(PageEditParameterForm)
+PageEditExtraVariablesFormSet = formset_factory(PageEditExtraVariableForm)
 
 
 class PageVisibilityForm(forms.ModelForm):
diff --git a/combo/manager/templates/combo/page_parameters.html b/combo/manager/templates/combo/page_extra_variables.html
similarity index 97%
rename from combo/manager/templates/combo/page_parameters.html
rename to combo/manager/templates/combo/page_extra_variables.html
index 10df1e0d..7a0f79a8 100644
--- a/combo/manager/templates/combo/page_parameters.html
+++ b/combo/manager/templates/combo/page_extra_variables.html
@@ -26,7 +26,7 @@
       {% endfor %}
       </tbody>
   </table>
-  <button id="add-page-property-form" type="button">{% trans "Add another property" %}</button>
+  <button id="add-page-property-form" type="button">{% trans "Add another" %}</button>
   <div class="buttons">
     <button class="submit-button">{% trans "Save" %}</button>
     <a class="cancel" href="{% url 'combo-manager-page-view' pk=object.pk %}">{% trans 'Cancel' %}</a>
diff --git a/combo/manager/templates/combo/page_view.html b/combo/manager/templates/combo/page_view.html
index c923bbe2..bf805cd8 100644
--- a/combo/manager/templates/combo/page_view.html
+++ b/combo/manager/templates/combo/page_view.html
@@ -14,6 +14,7 @@
 {% if request.user.is_superuser %}
   <li><a class="action-add-child" rel="popup" href="{% url 'combo-manager-page-add-child' pk=object.id %}">{% trans 'Add a child page' %}</a></li>
   <li><a class="action-edit-roles" rel="popup" href="{% url 'combo-manager-page-edit-roles' pk=object.id %}">{% trans 'Manage edit roles' %}</a></li>
+  <li><a class="action-edit-page-variables" rel="popup" href="{% url 'combo-manager-page-edit-extra-variables' pk=object.id %}">{% trans "Edit extra page variables" %}</a></li>
   <li><a rel="popup" class="action-duplicate" href="{% url 'combo-manager-page-duplicate' pk=object.id %}">{% trans 'Duplicate' %}</a></li>
   <li><a class="action-delete" rel="popup" href="{% url 'combo-manager-page-delete' pk=object.id %}">{% trans 'Delete' %}</a></li>
 {% endif %}
@@ -90,11 +91,13 @@
 (<a rel="popup" href="{% url 'combo-manager-page-edit-picture' pk=object.id %}">{% trans 'change' %}</a>)
 </p>
 
+{% if object.extra_variables %}
 <p>
-<label>{% trans 'Parameters:' %}</label>
-{% for key in object.get_parameters_keys %}<i>{{ key }}</i>{% if not forloop.last %}, {% endif %}{% empty %}<i>{% trans 'none' context 'parameters' %}</i>{% endfor %}
-(<a rel="popup" href="{% url 'combo-manager-page-edit-parameters' pk=object.id %}">{% trans 'change' %}</a>)
+<label>{% trans 'Extra variables:' %}</label>
+{% for key in object.get_extra_variables_keys %}<i>{{ key }}</i>{% if not forloop.last %}, {% endif %}{% endfor %}
+(<a rel="popup" href="{% url 'combo-manager-page-edit-extra-variables' pk=object.id %}">{% trans 'change' %}</a>)
 </p>
+{% endif %}
 
 </div>
 
diff --git a/combo/manager/urls.py b/combo/manager/urls.py
index cad7a6e8..96d79e18 100644
--- a/combo/manager/urls.py
+++ b/combo/manager/urls.py
@@ -65,9 +65,9 @@ urlpatterns = [
         name='combo-manager-page-remove-picture',
     ),
     url(
-        r'^pages/(?P<pk>\d+)/parameters/$',
-        views.page_edit_parameters,
-        name='combo-manager-page-edit-parameters',
+        r'^pages/(?P<pk>\d+)/extra-variables/$',
+        views.page_edit_extra_variables,
+        name='combo-manager-page-edit-extra-variables',
     ),
     url(r'^pages/(?P<pk>\d+)/delete$', staff_required(views.page_delete), name='combo-manager-page-delete'),
     url(r'^pages/(?P<pk>\d+)/export$', views.page_export, name='combo-manager-page-export'),
diff --git a/combo/manager/views.py b/combo/manager/views.py
index 04763ef7..218243b9 100644
--- a/combo/manager/views.py
+++ b/combo/manager/views.py
@@ -63,8 +63,8 @@ from .forms import (
     PageAddForm,
     PageDuplicateForm,
     PageEditDescriptionForm,
+    PageEditExtraVariablesFormSet,
     PageEditIncludeInNavigationForm,
-    PageEditParameterFormSet,
     PageEditPictureForm,
     PageEditRedirectionForm,
     PageEditRolesForm,
@@ -367,15 +367,16 @@ class PageRemovePictureView(ManagedPageMixin, DetailView):
 page_remove_picture = PageRemovePictureView.as_view()
 
 
-class PageEditParametersView(PageEditView):
-    form_class = PageEditParameterFormSet
-    comment = _('changed parameters')
-    template_name = 'combo/page_parameters.html'
-    page_title = _('Page properties')
+class PageEditExtraVariablesView(PageEditView):
+    form_class = PageEditExtraVariablesFormSet
+    comment = _('changed extra variables')
+    template_name = 'combo/page_extra_variables.html'
+    page_title = _('Extra page variables')
 
     def get_initial(self):
         return sorted(
-            ({'key': k, 'value': v} for k, v in self.get_object().parameters.items()), key=itemgetter('key')
+            ({'key': k, 'value': v} for k, v in self.get_object().extra_variables.items()),
+            key=itemgetter('key'),
         )
 
     def get_form_kwargs(self):
@@ -385,17 +386,17 @@ class PageEditParametersView(PageEditView):
 
     def form_valid(self, form):
         self.object = self.get_object()
-        self.object.parameters = {}
+        self.object.extra_variables = {}
         for sub_data in form.cleaned_data:
             if not sub_data.get('key'):
                 continue
-            self.object.parameters[sub_data['key']] = sub_data['value']
+            self.object.extra_variables[sub_data['key']] = sub_data['value']
         self.object.save()
         PageSnapshot.take(self.object, request=self.request, comment=self.comment)
         return HttpResponseRedirect(self.get_success_url())
 
 
-page_edit_parameters = PageEditParametersView.as_view()
+page_edit_extra_variables = PageEditExtraVariablesView.as_view()
 
 
 class PageView(ManagedPageMixin, DetailView):
diff --git a/combo/public/views.py b/combo/public/views.py
index a9d968d9..fbd26776 100644
--- a/combo/public/views.py
+++ b/combo/public/views.py
@@ -104,7 +104,7 @@ def modify_global_context(request, ctx):
         ctx['selected_user'] = get_user_from_name_id(ctx['name_id'])
     if 'page' in ctx:
         page = ctx['page']
-        ctx.update(page.get_parameters(request, ctx))
+        ctx.update(page.get_extra_variables(request, ctx))
 
 
 @csrf_exempt
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 303b5310..523610e7 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -571,22 +571,22 @@ def test_page_edit_picture(app, admin_user):
     assert Page.objects.get(id=page.id).picture.url in resp.text
 
 
-def test_page_edit_parameters(app, admin_user):
+def test_page_edit_extra_variables(app, admin_user):
     app = login(app)
     page = Page.objects.create(title='One', slug='one', template_name='standard')
-    assert page.parameters == {}
+    assert page.extra_variables == {}
     resp = app.get('/manage/pages/%s/' % page.id)
-    assert resp.text.count('<i>none</i>') == 4
-    resp = resp.click(href='.*/parameters/')
+    assert '<label>Extra variables:</label>' not in resp.text
+    resp = resp.click(href='.*/extra-variables/')
     resp.form['form-0-key'] = 'foo'
     resp.form['form-0-value'] = 'bar'
     resp = resp.form.submit().follow()
     page.refresh_from_db()
-    assert page.parameters == {'foo': 'bar'}
-    assert resp.text.count('<i>none</i>') == 3
+    assert page.extra_variables == {'foo': 'bar'}
+    assert '<label>Extra variables:</label>' in resp.text
     assert '<i>foo</i>' in resp
 
-    resp = resp.click(href='.*/parameters/')
+    resp = resp.click(href='.*/extra-variables/', index=0)
     assert resp.form['form-TOTAL_FORMS'].value == '2'
     assert resp.form['form-0-key'].value == 'foo'
     assert resp.form['form-0-value'].value == 'bar'
@@ -597,14 +597,13 @@ def test_page_edit_parameters(app, admin_user):
     resp.form['form-1-value'] = 'baz'
     resp = resp.form.submit().follow()
     page.refresh_from_db()
-    assert page.parameters == {
+    assert page.extra_variables == {
         'foo': 'bar-bis',
         'blah': 'baz',
     }
-    assert resp.text.count('<i>none</i>') == 3
     assert '<i>blah</i>, <i>foo</i>' in resp
 
-    resp = resp.click(href='.*/parameters/')
+    resp = resp.click(href='.*/extra-variables/', index=0)
     assert resp.form['form-TOTAL_FORMS'].value == '3'
     assert resp.form['form-0-key'].value == 'blah'
     assert resp.form['form-0-value'].value == 'baz'
@@ -617,10 +616,9 @@ def test_page_edit_parameters(app, admin_user):
     resp.form['form-0-key'] = ''
     resp = resp.form.submit().follow()
     page.refresh_from_db()
-    assert page.parameters == {
+    assert page.extra_variables == {
         'foo': 'bar',
     }
-    assert resp.text.count('<i>none</i>') == 3
     assert '<i>foo</i>' in resp
 
 
diff --git a/tests/test_public.py b/tests/test_public.py
index 6954cb33..81da3b95 100644
--- a/tests/test_public.py
+++ b/tests/test_public.py
@@ -1093,12 +1093,12 @@ def test_sub_slug(app, john_doe, jane_doe):
         assert resp.context['card_foo_bar_id'] == '42'
 
 
-def test_page_parameters(app):
+def test_page_extra_variables(app):
     page = Page.objects.create(
         title='Home',
         slug='page',
         template_name='standard',
-        parameters={'foo': 'bar', 'bar_id': '{{ 40|add:2 }}'},
+        extra_variables={'foo': 'bar', 'bar_id': '{{ 40|add:2 }}'},
     )
     cell = JsonCell.objects.create(
         page=page,
@@ -1123,7 +1123,7 @@ def test_page_parameters(app):
         )
     assert resp.text.strip() == 'XXbarYY42ZZ'
 
-    # check sub_slug/parameters override
+    # check sub_slug/extra_variables override
     page.sub_slug = '(?P<fooo>[a-z]+)'
     page.save()
     cell.template_string = 'XX{{ foo }}YY{{ bar_id }}ZZ{{ fooo }}AA'
-- 
2.34.1

