From 70a4d537c4b633f2cdf4fecb96c749342f49e98e Mon Sep 17 00:00:00 2001
From: Serghei Mihai <smihai@entrouvert.com>
Date: Tue, 25 Feb 2020 09:31:45 +0100
Subject: [PATCH] templates: use with_template template tag to render forms
 (#40159)

---
 src/authentic2/templates/authentic2/change_email.html       | 6 +++---
 .../templates/authentic2/login_password_form.html           | 6 +++---
 .../authentic2/login_password_registration_form.html        | 6 +++---
 .../templates/registration/password_change_form.html        | 6 +++---
 .../templates/registration/password_reset_confirm.html      | 6 +++---
 .../templates/registration/password_reset_form.html         | 6 +++---
 .../registration/registration_completion_form.html          | 6 +++---
 .../templates/authentic2_auth_fc/unlink.html                | 4 ++--
 tests/test_ldap.py                                          | 4 ++--
 tests/test_login.py                                         | 5 ++---
 tests/test_registration.py                                  | 2 +-
 11 files changed, 28 insertions(+), 29 deletions(-)

diff --git a/src/authentic2/templates/authentic2/change_email.html b/src/authentic2/templates/authentic2/change_email.html
index ecdd09f4..baa84afe 100644
--- a/src/authentic2/templates/authentic2/change_email.html
+++ b/src/authentic2/templates/authentic2/change_email.html
@@ -1,5 +1,5 @@
 {% extends "authentic2/base-page.html" %}
-{% load i18n %}
+{% load i18n gadjo %}
 
 {% block page-title %}
   {{ block.super }} - {{ view.title }}
@@ -14,9 +14,9 @@
 {% block content %}
   <p>{% blocktrans with email=user.email %}Your current email is {{ email }}.
   An email will be sent to validate the new one.{% endblocktrans %}</p>
-  <form method="post">
+  <form method="post" class="pk-mark-optional-fields">
     {% csrf_token %}
-    {{ form.as_p }}
+    {{ form|with_template }}
     <button class="submit-button">{% trans "Validate" %}</button>
     <button class="cancel-button" name="cancel" formnovalidate>{% trans "Cancel" %}</button>
   </form>
diff --git a/src/authentic2/templates/authentic2/login_password_form.html b/src/authentic2/templates/authentic2/login_password_form.html
index 3606b129..97fcd89b 100644
--- a/src/authentic2/templates/authentic2/login_password_form.html
+++ b/src/authentic2/templates/authentic2/login_password_form.html
@@ -1,12 +1,12 @@
-{% load i18n staticfiles %}
+{% load i18n staticfiles gadjo %}
 
 {% block login %}
 
 {% block form %}
 <div>
-<form method="post">
+<form method="post" class="pk-mark-optional-fields">
 {% csrf_token %}
-{{ form.as_p }}
+{{ form|with_template }}
 {% block buttons %}
 <button class="submit-button" name="{{ submit_name }}">{% trans "Log in" %}</button>
 {% if cancel %}
diff --git a/src/authentic2/templates/authentic2/login_password_registration_form.html b/src/authentic2/templates/authentic2/login_password_registration_form.html
index d407896e..e7384af5 100644
--- a/src/authentic2/templates/authentic2/login_password_registration_form.html
+++ b/src/authentic2/templates/authentic2/login_password_registration_form.html
@@ -1,9 +1,9 @@
-{% load i18n %}
+{% load i18n gadjo %}
 
 {% block registration %}
-<form enctype="multipart/form-data" method="post">
+<form enctype="multipart/form-data" method="post" class="pk-mark-optional-fields">
   {% csrf_token %}
-  {{ form.as_p }}
+  {{ form|with_template }}
   <button class="submit-button">{% trans 'Submit' %}</button>
 </form>
 {% endblock %}
diff --git a/src/authentic2/templates/registration/password_change_form.html b/src/authentic2/templates/registration/password_change_form.html
index 6314a25c..a38dea38 100644
--- a/src/authentic2/templates/registration/password_change_form.html
+++ b/src/authentic2/templates/registration/password_change_form.html
@@ -1,5 +1,5 @@
 {% extends "authentic2/base-page.html" %}
-{% load i18n %}
+{% load i18n gadjo %}
 
 {% block page-title %}
   {{ block.super }} - {{ view.title }}
@@ -12,7 +12,7 @@
 {% endblock %}
 
 {% block content %}
-  <form method="post" action=".">
+  <form method="post" action="." class="pk-mark-optional-fields">
     {% if user.has_usable_password %}
     <p>{% blocktrans %}To change your password you must provide your current password, then provide
     the new password and confirm it.
@@ -21,7 +21,7 @@
     <p>{% blocktrans %}You are going to define your account password.{% endblocktrans %}</p>
     {% endif %}
     {% csrf_token %}
-    {{ form.as_p }}
+    {{ form|with_template }}
     <button class="submit-button">{% trans 'Submit' %}</button>
     <button class="cancel-button" name="cancel" formnovalidate>{% trans "Cancel" %}</button>
   </form>
diff --git a/src/authentic2/templates/registration/password_reset_confirm.html b/src/authentic2/templates/registration/password_reset_confirm.html
index b713d8fe..bcc1dff7 100644
--- a/src/authentic2/templates/registration/password_reset_confirm.html
+++ b/src/authentic2/templates/registration/password_reset_confirm.html
@@ -1,5 +1,5 @@
 {% extends "authentic2/base-page.html" %}
-{% load i18n %}
+{% load i18n gadjo %}
 
 {% block title %}
 {{ view.title }}
@@ -9,9 +9,9 @@
 
 {% if validlink %}
 
-<form method="post">
+<form method="post" class="pk-mark-optional-fields">
   {% csrf_token %}
-  {{ form.as_p }}
+  {{ form|with_template }}
 
   <button class="submit-button">{% trans 'Submit' %}</button>
 </form>
diff --git a/src/authentic2/templates/registration/password_reset_form.html b/src/authentic2/templates/registration/password_reset_form.html
index 7ddee74a..dd9d06da 100644
--- a/src/authentic2/templates/registration/password_reset_form.html
+++ b/src/authentic2/templates/registration/password_reset_form.html
@@ -1,5 +1,5 @@
 {% extends "authentic2/base-page.html" %}
-{% load i18n %}
+{% load i18n gadjo %}
 
 {% block title %}
 {% trans "Resetting password" %}
@@ -9,9 +9,9 @@
 
 <h2>{% trans "Resetting password" %}</h2>
 
-<form method="post">
+<form method="post" class="pk-mark-optional-fields">
   {% csrf_token %}
-  {{ form.as_p }}
+  {{ form|with_template }}
 
   <button class="submit-button">{% trans 'Submit' %}</button>
 </form>
diff --git a/src/authentic2/templates/registration/registration_completion_form.html b/src/authentic2/templates/registration/registration_completion_form.html
index c1596c97..ef916c97 100644
--- a/src/authentic2/templates/registration/registration_completion_form.html
+++ b/src/authentic2/templates/registration/registration_completion_form.html
@@ -1,5 +1,5 @@
 {% extends "authentic2/base-page.html" %}
-{% load i18n %}
+{% load i18n gadjo %}
 
 {% block title %}
     {% trans "Registration" %}
@@ -10,10 +10,10 @@
       <h2>{% trans "Registration" %}</h2>
       <p>{% trans "Please fill the form to complete your registration" %}</p>
       {% endblock %}
-      <form method="post" class="a2-registration-completion-form">
+      <form method="post" class="a2-registration-completion-form pk-mark-optional-fields">
         {% block registration-completion-form-top %}{% endblock %}
         {% csrf_token %}
-        {{ form.as_p }}
+        {{ form|with_template }}
         <button class="submit-button">{% trans 'Submit' %}</button>
         {% block registration-completion-form-bottom %}{% endblock %}
         {% csrf_token %}
diff --git a/src/authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html b/src/authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html
index fc46effc..e9f2ec1b 100644
--- a/src/authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html
+++ b/src/authentic2_auth_fc/templates/authentic2_auth_fc/unlink.html
@@ -1,6 +1,6 @@
 {% extends "authentic2_auth_fc/base.html"%}
 {% load staticfiles %}
-{% load i18n %}
+{% load i18n gadjo %}
 
 {% block content %}
     <p>
@@ -26,7 +26,7 @@
     {% endif %}
     <form method="post">
         {% csrf_token %}
-        {{ form.as_p }}
+        {{ form|with_template }}
         <button class="submit-button" name="unlink">{% trans "Unlink" %}</button>
         <button class="cancel-button" name="cancel" formnovalidate>{% trans "Cancel" %}</button>
     </form>
diff --git a/tests/test_ldap.py b/tests/test_ldap.py
index 1cf0a124..b8102632 100644
--- a/tests/test_ldap.py
+++ b/tests/test_ldap.py
@@ -847,7 +847,7 @@ def test_ou_selector(slapd, settings, app, ou1):
     response.form.set('password', PASS)
     response.form.set('ou', str(get_default_ou().pk))
     response = response.form.submit(name='login-password-submit')
-    assert response.pyquery('.errorlist.nonfield')
+    assert response.pyquery('.errornotice')
     assert '_auth_user_id' not in app.session
 
     # Check login to the proper ou works
@@ -875,7 +875,7 @@ def test_ou_selector_default_ou(slapd, settings, app, ou1):
     response.form.set('password', PASS)
     response.form.set('ou', str(ou1.pk))
     response = response.form.submit(name='login-password-submit')
-    assert response.pyquery('.errorlist.nonfield')
+    assert response.pyquery('.errornotice')
     assert '_auth_user_id' not in app.session
 
     # Check login to the proper ou works
diff --git a/tests/test_login.py b/tests/test_login.py
index 525b0c6c..d555b804 100644
--- a/tests/test_login.py
+++ b/tests/test_login.py
@@ -174,13 +174,12 @@ def test_ou_selector(app, settings, simple_user, ou1, ou2, user_ou1, role_ou1):
     response.form.set('username', simple_user.username)
     response.form.set('password', simple_user.username)
     response = response.form.submit(name='login-password-submit')
-    assert response.pyquery('.errorlist')
+    assert response.pyquery('.widget-with-error')
     # Check login to the wrong ou do not work
     response.form.set('password', simple_user.username)
     response.form.set('ou', str(ou1.pk))
     response = response.form.submit(name='login-password-submit')
-    assert not response.pyquery('.errorlist:not(.nonfield)')
-    assert response.pyquery('.errorlist.nonfield')
+    assert response.pyquery('.errornotice')
     assert '_auth_user_id' not in app.session
     # Check login to the proper ou works
     response.form.set('password', simple_user.username)
diff --git a/tests/test_registration.py b/tests/test_registration.py
index 766f846d..7c69d7ad 100644
--- a/tests/test_registration.py
+++ b/tests/test_registration.py
@@ -202,7 +202,7 @@ def test_username_settings(app, db, settings, mailoutbox):
         assert response.pyquery('[for=id_%s]' % key)
         assert response.pyquery('[for=id_%s]' % key).attr('class') == 'form-field-required'
 
-    assert response.pyquery('#id_username').next('.helptext').text() == 'Bien remplir'
+    assert response.pyquery('#id_username').next('.hint').text() == 'Bien remplir'
     assert not response.pyquery('.errorlist')
 
     # check username is validated using regexp
-- 
2.28.0

