From 64e62dba98517b2d4b97c9ae34e804f30842064e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Fri, 3 Jun 2022 08:39:21 +0200
Subject: [PATCH] manager: initial value for date_start field in agenda pricing
 form (#65326)

---
 lingo/pricing/forms.py               | 12 ++++++++++++
 tests/pricing/manager/test_agenda.py | 17 +++++++++++++----
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/lingo/pricing/forms.py b/lingo/pricing/forms.py
index 646ed75..c56d044 100644
--- a/lingo/pricing/forms.py
+++ b/lingo/pricing/forms.py
@@ -17,6 +17,7 @@
 from django import forms
 from django.forms import ValidationError
 from django.template import Template, TemplateSyntaxError
+from django.utils.timezone import now
 from django.utils.translation import ugettext_lazy as _
 
 from lingo.agendas.models import CheckType
@@ -115,6 +116,17 @@ class AgendaPricingForm(forms.ModelForm):
             'date_end': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
         }
 
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        if not self.instance.pk:
+            last_date_end = (
+                AgendaPricing.objects.filter(agenda=self.instance.agenda)
+                .order_by('date_end')
+                .values_list('date_end', flat=True)
+                .last()
+            )
+            self.initial['date_start'] = last_date_end or now().date()
+
     def clean(self):
         cleaned_data = super().clean()
 
diff --git a/tests/pricing/manager/test_agenda.py b/tests/pricing/manager/test_agenda.py
index 54b5578..c56d44b 100644
--- a/tests/pricing/manager/test_agenda.py
+++ b/tests/pricing/manager/test_agenda.py
@@ -29,6 +29,8 @@ def test_add_agenda_pricing(app, admin_user):
     resp = app.get('/manage/pricing/agendas/')
     resp = resp.click(href='/manage/pricing/agenda/%s/' % agenda.pk)
     resp = resp.click('New pricing')
+    # first pricing, starts on today
+    assert resp.form['date_start'].value == datetime.date.today().strftime('%Y-%m-%d')
     resp.form['pricing'] = pricing.pk
     resp.form['date_start'] = '2021-09-01'
     resp.form['date_end'] = '2021-09-01'
@@ -44,24 +46,30 @@ def test_add_agenda_pricing(app, admin_user):
     assert agenda_pricing.date_end == datetime.date(2022, 9, 1)
 
     resp = app.get('/manage/pricing/agenda/%s/pricing/add/' % agenda.pk)
+    # starts on last date_end
+    assert resp.form['date_start'].value == '2022-09-01'
     resp.form['pricing'] = pricing.pk
     resp.form['date_start'] = '2021-11-01'
     resp.form['date_end'] = '2022-11-01'
     resp = resp.form.submit()
     assert resp.context['form'].errors['__all__'] == ['Pricing overlaps existing pricings.']
-    resp.form['date_start'] = '2022-09-01'
-    resp.form['date_end'] = '2023-09-01'
+    resp.form['date_start'] = '2020-09-01'
+    resp.form['date_end'] = '2021-09-01'
     resp = resp.form.submit()
     agenda_pricing = AgendaPricing.objects.latest('pk')
     assert agenda_pricing.pricing == pricing
     assert agenda_pricing.agenda == agenda
-    assert agenda_pricing.date_start == datetime.date(2022, 9, 1)
-    assert agenda_pricing.date_end == datetime.date(2023, 9, 1)
+    assert agenda_pricing.date_start == datetime.date(2020, 9, 1)
+    assert agenda_pricing.date_end == datetime.date(2021, 9, 1)
 
     resp = app.get('/manage/pricing/agenda/%s/' % agenda.pk)
     assert AgendaPricing.objects.filter(agenda=agenda, pricing=pricing).count() == 2
     assert resp.text.count('"/manage/pricing/%s/"' % pricing.pk) == 2
 
+    resp = app.get('/manage/pricing/agenda/%s/pricing/add/' % agenda.pk)
+    # starts on last date_end
+    assert resp.form['date_start'].value == '2022-09-01'
+
 
 def test_edit_agenda_pricing(app, admin_user):
     agenda = Agenda.objects.create(label='Foo Bar')
@@ -90,6 +98,7 @@ def test_edit_agenda_pricing(app, admin_user):
     app = login(app)
     resp = app.get('/manage/pricing/agenda/%s/pricing/%s/' % (agenda.pk, agenda_pricing.pk))
     resp = resp.click(href='/manage/pricing/agenda/%s/pricing/%s/edit/' % (agenda.pk, agenda_pricing.pk))
+    assert resp.form['date_start'].value == '2021-09-01'
     resp.form['pricing'] = pricing2.pk
     resp.form['date_start'] = '2021-09-01'
     resp.form['date_end'] = '2021-09-01'
-- 
2.35.1

