From 9df6c1e16514cfe7d6fb3cc5a14a16067b8af4de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Fri, 27 Nov 2020 10:28:40 +0100
Subject: [PATCH] forms: evaluate existing drafts before creating a new one
 (#48904)

---
 tests/form_pages/test_all.py            | 22 ++++++++++++++++++++++
 wcs/forms/root.py                       |  8 ++++++--
 wcs/templates/wcs/formdata_filling.html |  8 +++-----
 3 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/tests/form_pages/test_all.py b/tests/form_pages/test_all.py
index be3386918..c05b58026 100644
--- a/tests/form_pages/test_all.py
+++ b/tests/form_pages/test_all.py
@@ -1723,6 +1723,28 @@ def test_form_tracking_code_as_user(pub, nocache):
             headers={'User-agent': 'Googlebot'}, status=403)
 
 
+def test_form_tracking_code_prefill(pub, nocache):
+    create_user(pub)
+    formdef = create_formdef()
+    formdef.data_class().wipe()
+    formdef.fields = [fields.StringField(id='0', label='string',
+        prefill={'type': 'user', 'value': 'email'})]
+    formdef.enable_tracking_codes = True
+    formdef.store()
+
+    # first time
+    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
+    assert '<h3>Tracking code</h3>' in resp.text
+    assert 'You already started to fill this form.' not in resp.text
+    resp.forms[0]['f0'] = 'foobar'
+    resp = resp.forms[0].submit('submit')
+
+    # second time, invitation to load an existing draft
+    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
+    assert '<h3>Tracking code</h3>' in resp.text
+    assert 'You already started to fill this form.' in resp.text
+
+
 def test_form_empty_tracking_code(pub, nocache):
     formdef = create_formdef()
     formdef.fields = [fields.StringField(id='0', label='string')]
diff --git a/wcs/forms/root.py b/wcs/forms/root.py
index fb8c02dbc..e102ec2e4 100644
--- a/wcs/forms/root.py
+++ b/wcs/forms/root.py
@@ -753,8 +753,12 @@ class FormPage(Directory, FormTemplateMixin):
             self.feed_current_data(get_request().form.get('magictoken'))
         else:
             self.feed_current_data(None)
-            if not self.edit_mode and get_request().get_method() == 'GET' and 'mt' not in get_request().form:
-                self.initial_hit = True
+            if not self.edit_mode and (
+                    get_request().get_method() == 'GET' and
+                    'mt' not in get_request().form and
+                    get_request().user):
+                self.initial_drafts = list(
+                        LazyFormDef(self.formdef).objects.current_user().drafts().order_by('receipt_time'))
             # first hit on first page, if tracking code are enabled and we
             # are not editing an existing formdata, generate a new tracking
             # code.
diff --git a/wcs/templates/wcs/formdata_filling.html b/wcs/templates/wcs/formdata_filling.html
index 679e6e965..8a849113d 100644
--- a/wcs/templates/wcs/formdata_filling.html
+++ b/wcs/templates/wcs/formdata_filling.html
@@ -10,9 +10,8 @@
 
 {% block form-main %}
 {% block drafts-recall %}
-{% if view.initial_hit and user.is_authenticated and not request.quixote_request.is_in_backoffice %}
-{% with formdef.objects.current_user.drafts|order_by:"receipt_time" as drafts %}
-  {% with drafts|length as drafts_length %}
+{% if view.initial_drafts %}
+  {% with view.initial_drafts|length as drafts_length %}
    {% if drafts_length > 0 %}
     <div class="drafts-recall">
      <p>
@@ -25,7 +24,7 @@
        <p><a class="pk-button" href="{{drafts.0.internal_id}}/">{% trans "Continue with draft" %}</a></p>
      {% elif drafts_length > 1 %}
      <ul>
-     {% for draft in drafts %}
+     {% for draft in view.initial_drafts %}
      <li><a href="{{draft.internal_id}}/">{% trans "continue with draft from " %} {{draft.receipt_date}}
                      {{draft.receipt_time}}</a>, {% blocktrans with page_no=draft.page_no|add:1 %}on page {{page_no}}{% endblocktrans %}</li>
      {% endfor %}
@@ -34,7 +33,6 @@
     </div>
    {% endif %}
   {% endwith %}
-{% endwith %}
 {% endif %}
 {% endblock %}
 
-- 
2.29.2

