From 7c8375f9ed991f1599c738d6c524cca2378fdd54 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 12 May 2020 14:22:52 +0200
Subject: [PATCH 1/2] agendas: add publication_date on event (#40728)

---
 .../migrations/0044_event_publication_date.py | 19 +++++++++++++++
 chrono/agendas/models.py                      |  2 ++
 chrono/manager/forms.py                       |  2 ++
 .../chrono/manager_event_detail_fragment.html |  3 ++-
 .../manager_events_agenda_month_view.html     |  3 +++
 .../manager_events_agenda_settings.html       |  3 +++
 tests/test_import_export.py                   |  2 ++
 tests/test_manager.py                         | 23 +++++++++++++++----
 8 files changed, 52 insertions(+), 5 deletions(-)
 create mode 100644 chrono/agendas/migrations/0044_event_publication_date.py

diff --git a/chrono/agendas/migrations/0044_event_publication_date.py b/chrono/agendas/migrations/0044_event_publication_date.py
new file mode 100644
index 0000000..e82dc82
--- /dev/null
+++ b/chrono/agendas/migrations/0044_event_publication_date.py
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('agendas', '0043_booking_user_external_id'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='event',
+            name='publication_date',
+            field=models.DateField(blank=True, null=True, verbose_name='Publication date'),
+        ),
+    ]
diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 4f051ef..4bc0798 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -510,6 +510,7 @@ class MeetingType(models.Model):
 class Event(models.Model):
     agenda = models.ForeignKey(Agenda, on_delete=models.CASCADE)
     start_datetime = models.DateTimeField(_('Date/time'))
+    publication_date = models.DateField(_('Publication date'), blank=True, null=True)
     places = models.PositiveIntegerField(_('Places'))
     waiting_list_places = models.PositiveIntegerField(_('Places in waiting list'), default=0)
     label = models.CharField(
@@ -636,6 +637,7 @@ class Event(models.Model):
     def export_json(self):
         return {
             'start_datetime': make_naive(self.start_datetime).strftime('%Y-%m-%d %H:%M:%S'),
+            'publication_date': self.publication_date.strftime('%Y-%m-%d') if self.publication_date else None,
             'places': self.places,
             'waiting_list_places': self.waiting_list_places,
             'label': self.label,
diff --git a/chrono/manager/forms.py b/chrono/manager/forms.py
index 594c40b..dde701b 100644
--- a/chrono/manager/forms.py
+++ b/chrono/manager/forms.py
@@ -73,6 +73,7 @@ class NewEventForm(forms.ModelForm):
         widgets = {
             'agenda': forms.HiddenInput(),
             'start_datetime': DateTimeWidget(),
+            'publication_date': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
         }
         exclude = ['full', 'meeting_type', 'desk', 'slug']
 
@@ -83,6 +84,7 @@ class EventForm(forms.ModelForm):
         widgets = {
             'agenda': forms.HiddenInput(),
             'start_datetime': DateTimeWidget(),
+            'publication_date': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
         }
         exclude = ['full', 'meeting_type', 'desk']
 
diff --git a/chrono/manager/templates/chrono/manager_event_detail_fragment.html b/chrono/manager/templates/chrono/manager_event_detail_fragment.html
index 98c7ab4..0e01908 100644
--- a/chrono/manager/templates/chrono/manager_event_detail_fragment.html
+++ b/chrono/manager/templates/chrono/manager_event_detail_fragment.html
@@ -4,7 +4,8 @@
 <div>
  {% if object.description %}<p>{{ object.description }}</p>{% endif %}
  {% if object.pricing %}<p>{% trans "Pricing:" %} {{ object.pricing }}</p>{% endif %}
- {% if object.url %}<p><a href="{{ object.url }}">{{ object.url|truncatechars:100 }}</a>{% endif %}
+ {% if object.url %}<p><a href="{{ object.url }}">{{ object.url|truncatechars:100 }}</a></p>{% endif %}
+ {% if object.publication_date %}<p>{% trans "Publication date:" %} {{ object.publication_date }}</p>{% endif %}
 </div>
 </div>
 {% endif %}
diff --git a/chrono/manager/templates/chrono/manager_events_agenda_month_view.html b/chrono/manager/templates/chrono/manager_events_agenda_month_view.html
index f419a93..42b898d 100644
--- a/chrono/manager/templates/chrono/manager_events_agenda_month_view.html
+++ b/chrono/manager/templates/chrono/manager_events_agenda_month_view.html
@@ -31,6 +31,9 @@
         {% endblocktrans %}
         {% endif %}
         )
+        {% if event.publication_date %}
+        ({% trans "publication date:" %} {{ event.publication_date }})
+        {% endif %}
         {% if not event.in_bookable_period %}
         ({% trans "out of bookable period" %})
         {% endif %}
diff --git a/chrono/manager/templates/chrono/manager_events_agenda_settings.html b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
index a541a3d..4efe5eb 100644
--- a/chrono/manager/templates/chrono/manager_events_agenda_settings.html
+++ b/chrono/manager/templates/chrono/manager_events_agenda_settings.html
@@ -37,6 +37,9 @@
         {% endblocktrans %}
         {% endif %}
         )
+        {% if event.publication_date %}
+        ({% trans "publication date:" %} {{ event.publication_date }})
+        {% endif %}
         {% if not event.in_bookable_period %}
         ({% trans "out of bookable period" %})
         {% endif %}
diff --git a/tests/test_import_export.py b/tests/test_import_export.py
index 1ba4202..092116d 100644
--- a/tests/test_import_export.py
+++ b/tests/test_import_export.py
@@ -131,6 +131,7 @@ def test_import_export_event_details(app, some_data, meetings_agenda):
     first_event.description = 'description'
     first_event.pricing = '100'
     first_event.url = 'https://example.net/'
+    first_event.publication_date = datetime.date(2020, 5, 11)
     first_event.save()
 
     output = get_output_of_command('export_site')
@@ -147,6 +148,7 @@ def test_import_export_event_details(app, some_data, meetings_agenda):
     assert first_imported_event.description == 'description'
     assert first_imported_event.pricing == '100'
     assert first_imported_event.url == 'https://example.net/'
+    assert first_imported_event.publication_date == datetime.date(2020, 5, 11)
 
 
 def test_import_export_permissions(app, some_data, meetings_agenda):
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 5cd0dbd..4f82981 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -388,7 +388,8 @@ def test_add_event(app, admin_user):
     resp = resp.follow()
     event = Event.objects.get(places=10)
     assert event.slug is None
-    assert not "This agenda doesn't have any event yet." in resp.text
+    assert event.publication_date is None
+    assert "This agenda doesn't have any event yet." not in resp.text
     assert '/manage/agendas/%s/events/%s/' % (agenda.id, event.id) in resp.text
     assert ('Feb. 15, %s, 5 p.m.' % year) in resp.text
     assert '10 places' in resp.text
@@ -402,12 +403,14 @@ def test_add_event(app, admin_user):
     resp = resp.click('New Event')
     resp.form['start_datetime$date'] = '%s-02-15' % year
     resp.form['start_datetime$time'] = '18:00'
+    resp.form['publication_date'] = '2020-05-11'
     resp.form['places'] = 11
     resp.form['description'] = 'A description'
     resp = resp.form.submit()
     resp = resp.follow()
     event = Event.objects.get(places=11)
     assert event.description == 'A description'
+    assert event.publication_date == datetime.date(2020, 5, 11)
 
     # add with errors in datetime parts
     for parts in (
@@ -452,7 +455,7 @@ def test_add_event_as_manager(app, manager_user):
     resp = resp.form.submit()
     resp = resp.follow()
     event = Event.objects.get(places=10)
-    assert not "This agenda doesn't have any event yet." in resp.text
+    assert "This agenda doesn't have any event yet." not in resp.text
     assert '/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id) in resp.text
     assert 'Feb. 15, 2016, 5 p.m.' in resp.text
     assert '10 places' in resp.text
@@ -468,14 +471,18 @@ def test_edit_event(app, admin_user):
     resp = resp.click('Feb. 15, 2016, 5 p.m.')
     assert resp.form['start_datetime$date'].value == '2016-02-15'
     assert resp.form['start_datetime$time'].value == '17:00'
+    assert resp.form['publication_date'].value == ''
     resp.form['start_datetime$date'] = '2016-02-16'
     resp.form['start_datetime$time'] = '17:00'
+    resp.form['publication_date'] = '2020-05-11'
     resp.form['places'] = 20
     resp = resp.form.submit()
     resp = resp.follow()
     assert '/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id) in resp.text
     assert 'Feb. 16, 2016, 5 p.m.' in resp.text
     assert '20 places' in resp.text
+    event.refresh_from_db()
+    assert event.publication_date == datetime.date(2020, 5, 11)
 
 
 def test_edit_missing_event(app, admin_user):
@@ -487,8 +494,12 @@ def test_edit_event_as_manager(app, manager_user):
     agenda = Agenda(label=u'Foo bar')
     agenda.view_role = manager_user.groups.all()[0]
     agenda.save()
-    event = Event(start_datetime=make_aware(datetime.datetime(2016, 2, 15, 17, 0)), places=20, agenda=agenda)
-    event.save()
+    event = Event.objects.create(
+        start_datetime=make_aware(datetime.datetime(2016, 2, 15, 17, 0)),
+        places=20,
+        agenda=agenda,
+        publication_date=datetime.date(2020, 5, 11),
+    )
     app = login(app, username='manager', password='manager')
     resp = app.get('/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id), status=403)
 
@@ -498,14 +509,18 @@ def test_edit_event_as_manager(app, manager_user):
     resp = resp.click('Feb. 15, 2016, 5 p.m.')
     assert resp.form['start_datetime$date'].value == '2016-02-15'
     assert resp.form['start_datetime$time'].value == '17:00'
+    assert resp.form['publication_date'].value == '2020-05-11'
     resp.form['start_datetime$date'] = '2016-02-16'
     resp.form['start_datetime$time'] = '17:00'
+    resp.form['publication_date'] = ''
     resp.form['places'] = 20
     resp = resp.form.submit()
     resp = resp.follow()
     assert '/manage/agendas/%s/events/%s/edit' % (agenda.id, event.id) in resp.text
     assert 'Feb. 16, 2016, 5 p.m.' in resp.text
     assert '20 places' in resp.text
+    event.refresh_from_db()
+    assert event.publication_date is None
 
 
 def test_booked_places(app, admin_user):
-- 
2.20.1

