Projet

Général

Profil

0001-manager-initial-value-for-date_start-field-in-agenda.patch

Lauréline Guérin, 03 juin 2022 08:42

Télécharger (4,25 ko)

Voir les différences:

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(-)
lingo/pricing/forms.py
17 17
from django import forms
18 18
from django.forms import ValidationError
19 19
from django.template import Template, TemplateSyntaxError
20
from django.utils.timezone import now
20 21
from django.utils.translation import ugettext_lazy as _
21 22

  
22 23
from lingo.agendas.models import CheckType
......
115 116
            'date_end': forms.DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
116 117
        }
117 118

  
119
    def __init__(self, *args, **kwargs):
120
        super().__init__(*args, **kwargs)
121
        if not self.instance.pk:
122
            last_date_end = (
123
                AgendaPricing.objects.filter(agenda=self.instance.agenda)
124
                .order_by('date_end')
125
                .values_list('date_end', flat=True)
126
                .last()
127
            )
128
            self.initial['date_start'] = last_date_end or now().date()
129

  
118 130
    def clean(self):
119 131
        cleaned_data = super().clean()
120 132

  
tests/pricing/manager/test_agenda.py
29 29
    resp = app.get('/manage/pricing/agendas/')
30 30
    resp = resp.click(href='/manage/pricing/agenda/%s/' % agenda.pk)
31 31
    resp = resp.click('New pricing')
32
    # first pricing, starts on today
33
    assert resp.form['date_start'].value == datetime.date.today().strftime('%Y-%m-%d')
32 34
    resp.form['pricing'] = pricing.pk
33 35
    resp.form['date_start'] = '2021-09-01'
34 36
    resp.form['date_end'] = '2021-09-01'
......
44 46
    assert agenda_pricing.date_end == datetime.date(2022, 9, 1)
45 47

  
46 48
    resp = app.get('/manage/pricing/agenda/%s/pricing/add/' % agenda.pk)
49
    # starts on last date_end
50
    assert resp.form['date_start'].value == '2022-09-01'
47 51
    resp.form['pricing'] = pricing.pk
48 52
    resp.form['date_start'] = '2021-11-01'
49 53
    resp.form['date_end'] = '2022-11-01'
50 54
    resp = resp.form.submit()
51 55
    assert resp.context['form'].errors['__all__'] == ['Pricing overlaps existing pricings.']
52
    resp.form['date_start'] = '2022-09-01'
53
    resp.form['date_end'] = '2023-09-01'
56
    resp.form['date_start'] = '2020-09-01'
57
    resp.form['date_end'] = '2021-09-01'
54 58
    resp = resp.form.submit()
55 59
    agenda_pricing = AgendaPricing.objects.latest('pk')
56 60
    assert agenda_pricing.pricing == pricing
57 61
    assert agenda_pricing.agenda == agenda
58
    assert agenda_pricing.date_start == datetime.date(2022, 9, 1)
59
    assert agenda_pricing.date_end == datetime.date(2023, 9, 1)
62
    assert agenda_pricing.date_start == datetime.date(2020, 9, 1)
63
    assert agenda_pricing.date_end == datetime.date(2021, 9, 1)
60 64

  
61 65
    resp = app.get('/manage/pricing/agenda/%s/' % agenda.pk)
62 66
    assert AgendaPricing.objects.filter(agenda=agenda, pricing=pricing).count() == 2
63 67
    assert resp.text.count('"/manage/pricing/%s/"' % pricing.pk) == 2
64 68

  
69
    resp = app.get('/manage/pricing/agenda/%s/pricing/add/' % agenda.pk)
70
    # starts on last date_end
71
    assert resp.form['date_start'].value == '2022-09-01'
72

  
65 73

  
66 74
def test_edit_agenda_pricing(app, admin_user):
67 75
    agenda = Agenda.objects.create(label='Foo Bar')
......
90 98
    app = login(app)
91 99
    resp = app.get('/manage/pricing/agenda/%s/pricing/%s/' % (agenda.pk, agenda_pricing.pk))
92 100
    resp = resp.click(href='/manage/pricing/agenda/%s/pricing/%s/edit/' % (agenda.pk, agenda_pricing.pk))
101
    assert resp.form['date_start'].value == '2021-09-01'
93 102
    resp.form['pricing'] = pricing2.pk
94 103
    resp.form['date_start'] = '2021-09-01'
95 104
    resp.form['date_end'] = '2021-09-01'
96
-