From ccf230e38ab24ef65667e72d8fd4e05e4cc38bab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 14 Apr 2019 10:00:18 +0200
Subject: [PATCH] forms: let user recall draft on first page (#32263)

---
 tests/test_form_pages.py                | 34 +++++++++++++++++++++++++
 wcs/forms/root.py                       |  4 +++
 wcs/templates/wcs/formdata_filling.html | 31 ++++++++++++++++++++++
 3 files changed, 69 insertions(+)

diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py
index fadd855f6..2f452afe0 100644
--- a/tests/test_form_pages.py
+++ b/tests/test_form_pages.py
@@ -6115,3 +6115,37 @@ def test_field_live_condition_unknown_page_id(pub):
     params = resp.form.submit_fields()
     params = [(key, value if key != 'page_id' else 'eiuiu') for key, value in params]
     app.post('/foo/live', params=params)
+
+
+def test_form_recall_draft(pub):
+    user = create_user(pub)
+
+    formdef = create_formdef()
+    formdef.data_class().wipe()
+
+    app = login(get_app(pub), username='foo', password='foo')
+    resp = app.get('/test/')
+    assert not 'You already started to fill this form.' in resp.body
+
+    draft = formdef.data_class()()
+    draft.user_id = user.id
+    draft.status = 'draft'
+    draft.data = {}
+    draft.store()
+
+    app = login(get_app(pub), username='foo', password='foo')
+    resp = app.get('/test/')
+    assert 'You already started to fill this form.' in resp.body
+    assert 'href="%s"' % draft.id in resp.body
+
+    draft2 = formdef.data_class()()
+    draft2.user_id = user.id
+    draft2.status = 'draft'
+    draft2.data = {}
+    draft2.store()
+
+    app = login(get_app(pub), username='foo', password='foo')
+    resp = app.get('/test/')
+    assert 'You already started to fill this form.' in resp.body
+    assert 'href="%s"' % draft.id in resp.body
+    assert 'href="%s"' % draft2.id in resp.body
diff --git a/wcs/forms/root.py b/wcs/forms/root.py
index 313a94d7d..2cd44f0b7 100644
--- a/wcs/forms/root.py
+++ b/wcs/forms/root.py
@@ -50,6 +50,7 @@ from wcs.categories import Category
 from wcs.formdef import FormDef
 from wcs.formdata import FormData
 from wcs.forms.common import FormTemplateMixin
+from wcs.variables import LazyFormDef
 from wcs.roles import logged_users_role
 from wcs.workflows import Workflow, WorkflowBackofficeFieldsFormDef
 
@@ -426,6 +427,7 @@ class FormPage(Directory, FormTemplateMixin):
         context = {
             'view': self,
             'form': form,
+            'formdef': LazyFormDef(self.formdef),
             'form_side': lambda: self.form_side(0, page, data=data, magictoken=magictoken),
             'steps': lambda: self.step(0, page),
         }
@@ -623,6 +625,8 @@ 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 not get_request().form.has_key('mt'):
+                self.initial_hit = True
             # 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 cd613acd0..3b243eab0 100644
--- a/wcs/templates/wcs/formdata_filling.html
+++ b/wcs/templates/wcs/formdata_filling.html
@@ -1,7 +1,38 @@
 {% extends template_base %}
+{% load i18n %}
 
 {% block body %}
 {{ form_side|default:"" }}
 {{ publisher.get_request.session.display_message|safe }}
+
+{% 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 as drafts %}
+  {% with drafts|length as drafts_length %}
+   {% if drafts_length > 0 %}
+    <div class="drafts-recall">
+     <p>
+       {% blocktrans %}
+       You already started to fill this form. You can continue it or submit a new
+       one.
+       {% endblocktrans %}
+     </p>
+     {% if drafts_length == 1 %}
+       <p><a class="pk-button" href="{{drafts.0.internal_id}}">{% trans "Continue" %}</a></p>
+     {% elif drafts_length > 1 %}
+     <ul>
+     {% for draft in 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 %}
+     </ul>
+     {% endif %}
+    </div>
+   {% endif %}
+  {% endwith %}
+{% endwith %}
+{% endif %}
+{% endblock %}
+
 {{ form.render|safe }}
 {% endblock %}
-- 
2.20.1

