From 44bc0d71b88add01d297fa42732458d11c56925d Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Thu, 28 May 2020 16:37:01 +0200
Subject: [PATCH 1/2] manager: add duration on events (#37352)

---
 .../migrations/0044_auto_20200528_1603.py     | 24 +++++++++++++++++++
 chrono/agendas/models.py                      |  9 +++++--
 tests/test_manager.py                         |  3 +++
 3 files changed, 34 insertions(+), 2 deletions(-)
 create mode 100644 chrono/agendas/migrations/0044_auto_20200528_1603.py

diff --git a/chrono/agendas/migrations/0044_auto_20200528_1603.py b/chrono/agendas/migrations/0044_auto_20200528_1603.py
new file mode 100644
index 0000000..c882d55
--- /dev/null
+++ b/chrono/agendas/migrations/0044_auto_20200528_1603.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.18 on 2020-05-28 14:03
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('agendas', '0043_booking_user_external_id'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='event',
+            options={'ordering': ['agenda', 'start_datetime', 'duration', 'label']},
+        ),
+        migrations.AddField(
+            model_name='event',
+            name='duration',
+            field=models.IntegerField(default=30, verbose_name='Duration (in minutes)'),
+        ),
+    ]
diff --git a/chrono/agendas/models.py b/chrono/agendas/models.py
index 65c86d3..22ad3e9 100644
--- a/chrono/agendas/models.py
+++ b/chrono/agendas/models.py
@@ -633,16 +633,17 @@ class MeetingType(models.Model):
             'slug': self.slug,
             'duration': self.duration,
         }
 
 
 class Event(models.Model):
     agenda = models.ForeignKey(Agenda, on_delete=models.CASCADE)
     start_datetime = models.DateTimeField(_('Date/time'))
+    duration = models.IntegerField(_('Duration (in minutes)'), default=30)
     places = models.PositiveIntegerField(_('Places'))
     waiting_list_places = models.PositiveIntegerField(_('Places in waiting list'), default=0)
     label = models.CharField(
         _('Label'),
         max_length=150,
         null=True,
         blank=True,
         help_text=_('Optional label to identify this date.'),
@@ -653,17 +654,17 @@ class Event(models.Model):
     )
     pricing = models.CharField(_('Pricing'), max_length=150, null=True, blank=True)
     url = models.CharField(_('URL'), max_length=200, null=True, blank=True)
     full = models.BooleanField(default=False)
     meeting_type = models.ForeignKey(MeetingType, null=True, on_delete=models.CASCADE)
     desk = models.ForeignKey('Desk', null=True, on_delete=models.CASCADE)
 
     class Meta:
-        ordering = ['agenda', 'start_datetime', 'label']
+        ordering = ['agenda', 'start_datetime', 'duration', 'label']
         unique_together = ('agenda', 'slug')
 
     def __str__(self):
         if self.label:
             return self.label
         return date_format(localtime(self.start_datetime), format='DATETIME_FORMAT')
 
     def save(self, *args, **kwargs):
@@ -738,17 +739,21 @@ class Event(models.Model):
         return self.booking_set.filter(cancellation_datetime__isnull=True, in_waiting_list=False).count()
 
     @property
     def waiting_list(self):
         return self.booking_set.filter(cancellation_datetime__isnull=True, in_waiting_list=True).count()
 
     @property
     def end_datetime(self):
-        return self.start_datetime + datetime.timedelta(minutes=self.meeting_type.duration)
+        if self.agenda.accept_meetings():
+            minutes = self.meeting_type.duration
+        else:
+            minutes = self.duration
+        return self.start_datetime + datetime.timedelta(minutes=minutes)
 
     def get_absolute_url(self):
         return reverse('chrono-manager-event-edit', kwargs={'pk': self.agenda.id, 'event_pk': self.id})
 
     @classmethod
     def import_json(cls, data):
         data['start_datetime'] = make_aware(
             datetime.datetime.strptime(data['start_datetime'], '%Y-%m-%d %H:%M:%S')
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 0e9ff77..a2e00cc 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -443,24 +443,27 @@ def test_add_event_as_manager(app, manager_user):
     agenda.save()
 
     resp = app.get('/manage/agendas/%s/' % agenda.id).follow()
     resp = resp.click('Settings')
     assert '<h2>Settings' in resp.text
     resp = resp.click('New Event')
     resp.form['start_datetime$date'] = '2016-02-15'
     resp.form['start_datetime$time'] = '17:00'
+    resp.form['duration'] = 45
     resp.form['places'] = 10
     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 '/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
+    assert event.duration == 45
+    assert event.end_datetime == event.start_datetime + datetime.timedelta(minutes=45)
 
 
 def test_edit_event(app, admin_user):
     agenda = Agenda(label=u'Foo bar')
     agenda.save()
     event = Event(start_datetime=make_aware(datetime.datetime(2016, 2, 15, 17, 0)), places=20, agenda=agenda)
     event.save()
     app = login(app)
-- 
2.26.2

