From db0c5781328af09f8954ed26769f895020ccb060 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 12 Nov 2019 11:28:13 +0100
Subject: [PATCH] wcs: include drafts or not in current forms cell (#20231)

---
 combo/apps/wcs/forms.py                       |  2 +-
 ...0018_wcscurrentformscell_include_drafts.py | 19 +++++++++++++++++++
 combo/apps/wcs/models.py                      | 11 +++++++++--
 .../templates/combo/wcs/list_of_forms.html    |  5 +++++
 tests/test_wcs.py                             | 13 +++++++++++++
 5 files changed, 47 insertions(+), 3 deletions(-)
 create mode 100644 combo/apps/wcs/migrations/0018_wcscurrentformscell_include_drafts.py

diff --git a/combo/apps/wcs/forms.py b/combo/apps/wcs/forms.py
index 1aee714..0640137 100644
--- a/combo/apps/wcs/forms.py
+++ b/combo/apps/wcs/forms.py
@@ -86,7 +86,7 @@ class CategoriesSelectMultiple(forms.SelectMultiple):
 class WcsCurrentFormsCellForm(forms.ModelForm):
     class Meta:
         model = WcsCurrentFormsCell
-        fields = ['wcs_site', 'categories', 'current_forms', 'done_forms']
+        fields = ['wcs_site', 'categories', 'current_forms', 'done_forms', 'include_drafts']
 
     def __init__(self, *args, **kwargs):
         super(WcsCurrentFormsCellForm, self).__init__(*args, **kwargs)
diff --git a/combo/apps/wcs/migrations/0018_wcscurrentformscell_include_drafts.py b/combo/apps/wcs/migrations/0018_wcscurrentformscell_include_drafts.py
new file mode 100644
index 0000000..c7e0c7d
--- /dev/null
+++ b/combo/apps/wcs/migrations/0018_wcscurrentformscell_include_drafts.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('wcs', '0017_wcscareformscell'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='wcscurrentformscell',
+            name='include_drafts',
+            field=models.BooleanField(default=False, verbose_name='Include drafts'),
+        ),
+    ]
diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index 092f34b..d14bc52 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -307,6 +307,7 @@ class WcsCurrentFormsCell(WcsUserDataBaseCell):
     categories = JSONField(_('Categories'), blank=True)
     current_forms = models.BooleanField(_('Current Forms'), default=True)
     done_forms = models.BooleanField(_('Done Forms'), default=False)
+    include_drafts = models.BooleanField(_('Include drafts'), default=False)
 
     class Meta:
         verbose_name = _('User Forms')
@@ -317,11 +318,15 @@ class WcsCurrentFormsCell(WcsUserDataBaseCell):
 
     def get_api_url(self, context):
         user = self.get_concerned_user(context)
+        base_url = '/api/user/forms'
         if user:
             user_name_id = user.get_name_id()
             if user_name_id:
-                return '/api/users/%s/forms?limit=100&sort=desc' % user_name_id
-        return '/api/user/forms?limit=100&sort=desc'
+                base_url = '/api/users/%s/forms' % user_name_id
+        url = base_url + '?limit=100&sort=desc'
+        if self.include_drafts:
+            url += '&include-drafts=on'
+        return url
 
     @property
     def template_name(self):
@@ -339,6 +344,8 @@ class WcsCurrentFormsCell(WcsUserDataBaseCell):
             label = _('Done Forms')
         else:
             label = _('Current Forms')
+        if self.include_drafts:
+            label = '%s %s' % (label, _('and drafts'))
         if initial_label:
             return '%s - %s' % (initial_label, label)
         return label
diff --git a/combo/apps/wcs/templates/combo/wcs/list_of_forms.html b/combo/apps/wcs/templates/combo/wcs/list_of_forms.html
index 5d1099c..752860b 100644
--- a/combo/apps/wcs/templates/combo/wcs/list_of_forms.html
+++ b/combo/apps/wcs/templates/combo/wcs/list_of_forms.html
@@ -1,3 +1,4 @@
+{% load combo %}
 {% block cell-content %}
 {% if forms %}
 <ul>
@@ -5,9 +6,13 @@
   {% if data.url and data.title %}
   <li class="{{data.status_css_class}} {% if data.form_status_is_endpoint %}done{% endif %}"><a
       {% if data.readable %}href="{{ data.url }}"{% endif %}><span class="form-title">{{data.name}}</span>
+    {% if data.draft %}
+    <span class="form-number">{{data.datetime|strptime:"%Y-%m-%d %H:%M:%S"}}</span>
+    {% else %}
     <span class="form-number">{{data.form_number}}
     {% if data.form_digest %}<span class="form-digest">({{data.form_digest}})</span>{% endif %}
     </span>
+    {% endif %}
     <span class="form-status">{{data.status}}</span></a></li>
   {% endif %}
   {% endfor %}
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index ec2d31d..b9cd1bb 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -387,6 +387,7 @@ def test_current_forms_cell_render(context):
     # done forms
     cell.current_forms = False
     cell.done_forms = True
+    cell.include_drafts = False
     cell.save()
     result = cell.render(context)
     assert not 'http://127.0.0.1:8999/form-title/1/' in result
@@ -422,6 +423,18 @@ def test_current_forms_cell_render(context):
     extra_context = cell.get_cell_extra_context(context)
     assert len(extra_context['forms']) == 0
 
+    # check include drafts
+    cell.include_drafts = False
+    with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
+        cell.get_cell_extra_context(context)
+    assert requests_get.call_args_list[0][0][0] == '/api/user/forms?limit=100&sort=desc'
+
+    cell.include_drafts = True
+    with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
+        cell.get_cell_extra_context(context)
+    assert requests_get.call_args_list[0][0][0] == '/api/user/forms?limit=100&sort=desc&include-drafts=on'
+
+
 @wcs_present
 def test_current_forms_cell_render_single_site(context):
     page = Page(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
-- 
2.20.1

