From 18ce6713b4c2447b3f37fe965e2089ae93e78566 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 18 Jun 2020 10:39:04 +0200
Subject: [PATCH] manager: publication_date in csv event import (#44177)

---
 chrono/manager/forms.py                       | 10 ++++++
 .../chrono/manager_sample_events.txt          |  4 +--
 tests/test_manager.py                         | 32 +++++++++++++++----
 3 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/chrono/manager/forms.py b/chrono/manager/forms.py
index 95c6ab0..e28da75 100644
--- a/chrono/manager/forms.py
+++ b/chrono/manager/forms.py
@@ -348,6 +348,16 @@ class ImportEventsForm(forms.Form):
                     setattr(event, more_attr, csvline[column_index - 1])
                 column_index += 1
 
+            if len(csvline) >= 10 and csvline[9]:  # publication date is optional
+                for date_fmt in ('%Y-%m-%d', '%d/%m/%Y'):
+                    try:
+                        event.publication_date = datetime.datetime.strptime(csvline[9], date_fmt).date()
+                        break
+                    except ValueError:
+                        continue
+                else:
+                    raise ValidationError(_('Invalid file format. (date format, line %d)') % (i + 1))
+
             try:
                 event.full_clean(exclude=exclude)
             except ValidationError as e:
diff --git a/chrono/manager/templates/chrono/manager_sample_events.txt b/chrono/manager/templates/chrono/manager_sample_events.txt
index 139bb0c..20eaf69 100644
--- a/chrono/manager/templates/chrono/manager_sample_events.txt
+++ b/chrono/manager/templates/chrono/manager_sample_events.txt
@@ -1,2 +1,2 @@
-{% load i18n %}{% trans 'date' %},{% trans 'time' %},{% trans 'number of places' %},{% trans 'number of places in waiting list' %},{% trans 'label' %},{% trans 'identifier' %},{% trans 'description' %},{% trans 'pricing' %},{% trans 'URL' %}
-{{ some_future_date|date:"Y-m-d" }},{{ some_future_date|date:"H:i" }},15,0,{% trans "example event" as label %}{{ label }},{{ label|slugify }},,,https://www.example.net
+{% load i18n %}{% trans 'date' %},{% trans 'time' %},{% trans 'number of places' %},{% trans 'number of places in waiting list' %},{% trans 'label' %},{% trans 'identifier' %},{% trans 'description' %},{% trans 'pricing' %},{% trans 'URL' %},{% trans 'publication date' %}
+{{ some_future_date|date:"Y-m-d" }},{{ some_future_date|date:"H:i" }},15,0,{% trans "example event" as label %}{{ label }},{{ label|slugify }},,,https://www.example.net,{{ some_future_date|date:"Y-m-d" }}
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 6647c1f..0bfa57b 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -1206,7 +1206,7 @@ def test_import_events(app, admin_user):
     resp = resp.click('Import Events')
     sample_csv_resp = resp.click('Download sample file')
     assert sample_csv_resp.content_type == 'text/csv'
-    assert sample_csv_resp.text.startswith('date,time,')
+    assert sample_csv_resp.text.startswith('date,time')
 
     resp.form['events_csv_file'] = Upload('t.csv', sample_csv_resp.content, 'text/csv')
     resp = resp.form.submit(status=302)
@@ -1344,19 +1344,39 @@ def test_import_events(app, admin_user):
     resp.form['events_csv_file'] = Upload('t.csv', b'2016-09-16,18:00,10,5,label,slug', 'text/csv')
     resp = resp.form.submit(status=200)
     assert 'Event with this Agenda and Identifier already exists.' in resp.text
-    assert not '__all__' in resp.text
+    assert '__all__' not in resp.text
 
     # additional optional attributes
     Event.objects.all().delete()
     resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
+    resp.form['events_csv_file'] = Upload('t.csv', b'2016-09-16,18:00,10,5,label,slug,,,,', 'text/csv')
+    resp = resp.form.submit(status=302)
+    assert Event.objects.count() == 1
+    event = Event.objects.get()
+    assert event.description == ''
+    assert event.pricing == ''
+    assert event.url == ''
+    assert event.publication_date is None
+    Event.objects.all().delete()
+    resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
     resp.form['events_csv_file'] = Upload(
-        't.csv', b'2016-09-16,18:00,10,5,label,slug,description,pricing,url', 'text/csv'
+        't.csv', b'2016-09-16,18:00,10,5,label,slug,description,pricing,url,2016-10-16', 'text/csv'
     )
     resp = resp.form.submit(status=302)
     assert Event.objects.count() == 1
-    assert Event.objects.all()[0].description == 'description'
-    assert Event.objects.all()[0].pricing == 'pricing'
-    assert Event.objects.all()[0].url == 'url'
+    event = Event.objects.get()
+    assert event.description == 'description'
+    assert event.pricing == 'pricing'
+    assert event.url == 'url'
+    assert event.publication_date == datetime.date(2016, 10, 16)
+
+    # publication date bad format
+    resp = app.get('/manage/agendas/%s/import-events' % agenda.id, status=200)
+    resp.form['events_csv_file'] = Upload(
+        't.csv', b'2016-09-16,18:00,10,5,label,slug,description,pricing,url,foobar', 'text/csv'
+    )
+    resp = resp.form.submit(status=200)
+    assert 'Invalid file format. (date format' in resp.text
 
     # import events with empty slugs
     Event.objects.all().delete()
-- 
2.20.1

