forms: evaluate existing drafts before creating a new one (#48904)

This commit is contained in:
Frédéric Péters 2020-11-27 10:28:40 +01:00
parent e10eca5c0d
commit 5e34e1743d
3 changed files with 32 additions and 10 deletions

View File

@ -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')]

View File

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

View File

@ -10,10 +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 drafts_length > 0 %}
{% if view.initial_drafts %}
{% with view.initial_drafts|length as drafts_length %}
<div class="drafts-recall">
<p>
{% blocktrans %}
@ -22,19 +20,17 @@
{% endblocktrans %}
</p>
{% if drafts_length == 1 %}
<p><a class="pk-button" href="{{drafts.0.internal_id}}/">{% trans "Continue with draft" %}</a></p>
<p><a class="pk-button" href="{{view.initial_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 %}
</ul>
{% endif %}
</div>
{% endif %}
{% endwith %}
{% endwith %}
{% endif %}
{% endblock %}