Projet

Général

Profil

0002-pricing-check-type-views-65459.patch

Lauréline Guérin, 23 mai 2022 17:03

Télécharger (22,4 ko)

Voir les différences:

Subject: [PATCH 2/3] pricing: check-type views (#65459)

 lingo/pricing/forms.py                        |  26 +++
 .../lingo/pricing/manager_agenda_list.html    |   1 +
 .../pricing/manager_check_type_form.html      |  31 +++
 .../manager_check_type_group_form.html        |  31 +++
 .../pricing/manager_check_type_list.html      |  59 ++++++
 lingo/pricing/urls.py                         |  38 ++++
 lingo/pricing/views.py                        | 127 +++++++++++-
 tests/pricing/manager/test_checktype.py       | 190 ++++++++++++++++++
 8 files changed, 502 insertions(+), 1 deletion(-)
 create mode 100644 lingo/pricing/templates/lingo/pricing/manager_check_type_form.html
 create mode 100644 lingo/pricing/templates/lingo/pricing/manager_check_type_group_form.html
 create mode 100644 lingo/pricing/templates/lingo/pricing/manager_check_type_list.html
 create mode 100644 tests/pricing/manager/test_checktype.py
lingo/pricing/forms.py
19 19
from django.template import Template, TemplateSyntaxError
20 20
from django.utils.translation import ugettext_lazy as _
21 21

  
22
from lingo.agendas.models import CheckType
22 23
from lingo.pricing.models import AgendaPricing, Criteria, CriteriaCategory
23 24

  
24 25

  
......
138 139
        super().__init__(*args, **kwargs)
139 140
        for i in range(len(matrix.rows[0].cells)):
140 141
            self.fields['crit_%i' % i] = forms.DecimalField(required=True, max_digits=5, decimal_places=2)
142

  
143

  
144
class NewCheckTypeForm(forms.ModelForm):
145
    class Meta:
146
        model = CheckType
147
        fields = ['label', 'kind', 'pricing', 'pricing_rate']
148

  
149
    def clean(self):
150
        super().clean()
151
        if self.cleaned_data.get('pricing') is not None and self.cleaned_data.get('pricing_rate') is not None:
152
            raise ValidationError(_('Please choose between pricing and pricing rate.'))
153

  
154

  
155
class CheckTypeForm(NewCheckTypeForm):
156
    class Meta:
157
        model = CheckType
158
        fields = ['label', 'slug', 'pricing', 'pricing_rate', 'disabled']
159

  
160
    def clean_slug(self):
161
        slug = self.cleaned_data['slug']
162

  
163
        if self.instance.group.check_types.filter(slug=slug).exclude(pk=self.instance.pk).exists():
164
            raise ValidationError(_('Another check type exists with the same identifier.'))
165

  
166
        return slug
lingo/pricing/templates/lingo/pricing/manager_agenda_list.html
10 10
<h2>{% trans 'Agendas' %}</h2>
11 11
<span class="actions">
12 12
  <a href="{% url 'lingo-manager-agenda-sync' %}">{% trans 'Refresh agendas' %}</a>
13
  <a href="{% url 'lingo-manager-check-type-list' %}">{% trans 'Check types' %}</a>
13 14
</span>
14 15
{% endblock %}
15 16

  
lingo/pricing/templates/lingo/pricing/manager_check_type_form.html
1
{% extends "lingo/pricing/manager_check_type_list.html" %}
2
{% load i18n %}
3

  
4
{% block breadcrumb %}
5
{{ block.super }}
6
{% if form.instance.pk %}
7
<a href="{% url 'lingo-manager-check-type-edit' form.instance.group_id form.instance.pk %}">{{ form.instance }}</a>
8
{% else %}
9
<a href="{% url 'lingo-manager-check-type-add' form.instance.group_id %}">{% trans "New check type" %}</a>
10
{% endif %}
11
{% endblock %}
12

  
13
{% block appbar %}
14
{% if form.instance.pk %}
15
<h2>{{ form.instance.group }} - {% trans "Edit check type" %}</h2>
16
{% else %}
17
<h2>{{ form.instance.group }} - {% trans "New check type" %}</h2>
18
{% endif %}
19
{% endblock %}
20

  
21
{% block content %}
22

  
23
<form method="post" enctype="multipart/form-data">
24
  {% csrf_token %}
25
  {{ form.as_p }}
26
  <div class="buttons">
27
    <button class="submit-button">{% trans "Save" %}</button>
28
    <a class="cancel" href="{% url 'lingo-manager-check-type-list' %}">{% trans 'Cancel' %}</a>
29
  </div>
30
</form>
31
{% endblock %}
lingo/pricing/templates/lingo/pricing/manager_check_type_group_form.html
1
{% extends "lingo/pricing/manager_check_type_list.html" %}
2
{% load i18n %}
3

  
4
{% block breadcrumb %}
5
{{ block.super }}
6
{% if object.pk %}
7
<a href="{% url 'lingo-manager-check-type-group-edit' object.pk %}">{{ object }}</a>
8
{% else %}
9
<a href="{% url 'lingo-manager-check-type-group-add' %}">{% trans "New check type group" %}</a>
10
{% endif %}
11
{% endblock %}
12

  
13
{% block appbar %}
14
{% if object.pk %}
15
<h2>{% trans "Edit check type group" %}</h2>
16
{% else %}
17
<h2>{% trans "New check type group" %}</h2>
18
{% endif %}
19
{% endblock %}
20

  
21
{% block content %}
22

  
23
<form method="post" enctype="multipart/form-data">
24
  {% csrf_token %}
25
  {{ form.as_p }}
26
  <div class="buttons">
27
    <button class="submit-button">{% trans "Save" %}</button>
28
    <a class="cancel" href="{% url 'lingo-manager-check-type-list' %}">{% trans 'Cancel' %}</a>
29
  </div>
30
</form>
31
{% endblock %}
lingo/pricing/templates/lingo/pricing/manager_check_type_list.html
1
{% extends "lingo/pricing/manager_agenda_list.html" %}
2
{% load i18n %}
3

  
4
{% block breadcrumb %}
5
{{ block.super }}
6
<a href="{% url 'lingo-manager-check-type-list' %}">{% trans "Check types" %}</a>
7
{% endblock %}
8

  
9
{% block appbar %}
10
<h2>{% trans 'Check types' %}</h2>
11
<span class="actions">
12
<a rel="popup" href="{% url 'lingo-manager-check-type-group-add' %}">{% trans 'New group' %}</a>
13
</span>
14
{% endblock %}
15

  
16
{% block content %}
17
<div class="pk-information">
18
<p>{% trans "Define here check types used in events agendas to check bookings." %}</p>
19
</div>
20
{% for object in object_list %}
21
<div class="section check-type-group">
22
    <h3>
23
    <a rel="popup" href="{% url 'lingo-manager-check-type-group-edit' object.pk %}">{{ object }}</a>
24
    <span>
25
    <a class="button" href="{% url 'lingo-manager-check-type-group-export' object.pk %}">{% trans "Export"%}</a>
26
    <a class="button" rel="popup" href="{% url 'lingo-manager-check-type-group-delete' object.pk %}">{% trans "Delete"%}</a>
27
    </span>
28
    </h3>
29
  <div>
30
  <ul class="objects-list single-links">
31
    {% for check_type in object.check_types.all %}
32
    {% if check_type.kind == 'absence' %}
33
    <li>
34
        <a rel="popup" href="{% url 'lingo-manager-check-type-edit' object.pk check_type.pk %}">{% trans "Absence" %} - {{ check_type }}{% if check_type.pricing %}{% endif %}{% if check_type.disabled %}<span class="extra-info"> ({% trans "disabled" %})</span>{% endif %}</a>
35
        <a class="delete" rel="popup" href="{% url 'lingo-manager-check-type-delete' object.pk check_type.pk %}">{% trans "delete"%}</a>
36
    </li>
37
    {% endif %}
38
    {% endfor %}
39
    {% for check_type in object.check_types.all %}
40
    {% if check_type.kind == 'presence' %}
41
    <li>
42
        <a rel="popup" href="{% url 'lingo-manager-check-type-edit' object.pk check_type.pk %}">{% trans "Presence" %} - {{ check_type }}{% if check_type.disabled %}<span class="extra-info"> ({% trans "disabled" %})</span>{% endif %}</a>
43
        <a class="delete" rel="popup" href="{% url 'lingo-manager-check-type-delete' object.pk check_type.pk %}">{% trans "delete"%}</a>
44
    </li>
45
    {% endif %}
46
    {% endfor %}
47
    <li><a class="add" rel="popup" href="{% url 'lingo-manager-check-type-add' object.pk %}">{% trans "Add a check type" %}</a></li>
48
  </ul>
49
  </div>
50
</div>
51
{% empty %}
52
<div class="big-msg-info">
53
  {% blocktrans %}
54
  This site doesn't have any check type group yet. Click on the "New group" button in the top
55
  right of the page to add a first one.
56
  {% endblocktrans %}
57
</div>
58
{% endfor %}
59
{% endblock %}
lingo/pricing/urls.py
173 173
        staff_member_required(views.agenda_pricing_matrix_edit),
174 174
        name='lingo-manager-agenda-pricing-matrix-slug-edit',
175 175
    ),
176
    url(
177
        r'^check-types/$', staff_member_required(views.check_type_list), name='lingo-manager-check-type-list'
178
    ),
179
    url(
180
        r'^check-type/group/add/$',
181
        staff_member_required(views.check_type_group_add),
182
        name='lingo-manager-check-type-group-add',
183
    ),
184
    url(
185
        r'^check-type/group/(?P<pk>\d+)/edit/$',
186
        staff_member_required(views.check_type_group_edit),
187
        name='lingo-manager-check-type-group-edit',
188
    ),
189
    url(
190
        r'^check-type/group/(?P<pk>\d+)/delete/$',
191
        staff_member_required(views.check_type_group_delete),
192
        name='lingo-manager-check-type-group-delete',
193
    ),
194
    url(
195
        r'^check-type/group/(?P<pk>\d+)/export/$',
196
        staff_member_required(views.check_type_group_export),
197
        name='lingo-manager-check-type-group-export',
198
    ),
199
    url(
200
        r'^check-type/group/(?P<group_pk>\d+)/add/$',
201
        staff_member_required(views.check_type_add),
202
        name='lingo-manager-check-type-add',
203
    ),
204
    url(
205
        r'^check-type/group/(?P<group_pk>\d+)/(?P<pk>\d+)/edit/$',
206
        staff_member_required(views.check_type_edit),
207
        name='lingo-manager-check-type-edit',
208
    ),
209
    url(
210
        r'^check-type/group/(?P<group_pk>\d+)/(?P<pk>\d+)/delete/$',
211
        staff_member_required(views.check_type_delete),
212
        name='lingo-manager-check-type-delete',
213
    ),
176 214
]
lingo/pricing/views.py
40 40
from django.views.generic.detail import SingleObjectMixin
41 41

  
42 42
from lingo.agendas.chrono import refresh_agendas
43
from lingo.agendas.models import Agenda
43
from lingo.agendas.models import Agenda, CheckType, CheckTypeGroup
44 44
from lingo.agendas.views import AgendaMixin
45 45
from lingo.pricing.forms import (
46 46
    AgendaPricingForm,
47
    CheckTypeForm,
47 48
    CriteriaForm,
48 49
    ExportForm,
49 50
    ImportForm,
51
    NewCheckTypeForm,
50 52
    NewCriteriaForm,
51 53
    PricingCriteriaCategoryAddForm,
52 54
    PricingCriteriaCategoryEditForm,
......
786 788

  
787 789

  
788 790
agenda_pricing_matrix_edit = AgendaPricingMatrixEdit.as_view()
791

  
792

  
793
class CheckTypeListView(ListView):
794
    template_name = 'lingo/pricing/manager_check_type_list.html'
795
    model = CheckTypeGroup
796

  
797
    def get_queryset(self):
798
        return CheckTypeGroup.objects.prefetch_related('check_types')
799

  
800

  
801
check_type_list = CheckTypeListView.as_view()
802

  
803

  
804
class CheckTypeGroupAddView(CreateView):
805
    template_name = 'lingo/pricing/manager_check_type_group_form.html'
806
    model = CheckTypeGroup
807
    fields = ['label']
808

  
809
    def get_success_url(self):
810
        return reverse('lingo-manager-check-type-list')
811

  
812

  
813
check_type_group_add = CheckTypeGroupAddView.as_view()
814

  
815

  
816
class CheckTypeGroupEditView(UpdateView):
817
    template_name = 'lingo/pricing/manager_check_type_group_form.html'
818
    model = CheckTypeGroup
819
    fields = ['label', 'slug']
820

  
821
    def get_success_url(self):
822
        return reverse('lingo-manager-check-type-list')
823

  
824

  
825
check_type_group_edit = CheckTypeGroupEditView.as_view()
826

  
827

  
828
class CheckTypeGroupDeleteView(DeleteView):
829
    template_name = 'lingo/manager_confirm_delete.html'
830
    model = CheckTypeGroup
831

  
832
    def get_success_url(self):
833
        return reverse('lingo-manager-check-type-list')
834

  
835

  
836
check_type_group_delete = CheckTypeGroupDeleteView.as_view()
837

  
838

  
839
class CheckTypeGroupExport(DetailView):
840
    model = CheckTypeGroup
841

  
842
    def get(self, request, *args, **kwargs):
843
        response = HttpResponse(content_type='application/json')
844
        today = datetime.date.today()
845
        attachment = 'attachment; filename="export_check_type_group_{}_{}.json"'.format(
846
            self.get_object().slug, today.strftime('%Y%m%d')
847
        )
848
        response['Content-Disposition'] = attachment
849
        json.dump({'check_type_groups': [self.get_object().export_json()]}, response, indent=2)
850
        return response
851

  
852

  
853
check_type_group_export = CheckTypeGroupExport.as_view()
854

  
855

  
856
class CheckTypeAddView(CreateView):
857
    template_name = 'lingo/pricing/manager_check_type_form.html'
858
    model = CheckType
859
    form_class = NewCheckTypeForm
860

  
861
    def dispatch(self, request, *args, **kwargs):
862
        self.group_pk = kwargs.pop('group_pk')
863
        return super().dispatch(request, *args, **kwargs)
864

  
865
    def get_form_kwargs(self):
866
        kwargs = super().get_form_kwargs()
867
        if not kwargs.get('instance'):
868
            kwargs['instance'] = self.model()
869
        kwargs['instance'].group_id = self.group_pk
870
        return kwargs
871

  
872
    def get_success_url(self):
873
        return reverse('lingo-manager-check-type-list')
874

  
875

  
876
check_type_add = CheckTypeAddView.as_view()
877

  
878

  
879
class CheckTypeEditView(UpdateView):
880
    template_name = 'lingo/pricing/manager_check_type_form.html'
881
    model = CheckType
882
    form_class = CheckTypeForm
883

  
884
    def dispatch(self, request, *args, **kwargs):
885
        self.group_pk = kwargs.pop('group_pk')
886
        return super().dispatch(request, *args, **kwargs)
887

  
888
    def get_queryset(self):
889
        return CheckType.objects.filter(group=self.group_pk)
890

  
891
    def get_success_url(self):
892
        return reverse('lingo-manager-check-type-list')
893

  
894

  
895
check_type_edit = CheckTypeEditView.as_view()
896

  
897

  
898
class CheckTypeDeleteView(DeleteView):
899
    template_name = 'lingo/manager_confirm_delete.html'
900
    model = CheckType
901

  
902
    def dispatch(self, request, *args, **kwargs):
903
        self.group_pk = kwargs.pop('group_pk')
904
        return super().dispatch(request, *args, **kwargs)
905

  
906
    def get_queryset(self):
907
        return CheckType.objects.filter(group=self.group_pk)
908

  
909
    def get_success_url(self):
910
        return reverse('lingo-manager-check-type-list')
911

  
912

  
913
check_type_delete = CheckTypeDeleteView.as_view()
tests/pricing/manager/test_checktype.py
1
import pytest
2

  
3
from lingo.agendas.models import CheckType, CheckTypeGroup
4
from tests.utils import login
5

  
6
pytestmark = pytest.mark.django_db
7

  
8

  
9
def test_add_group(app, admin_user):
10
    app = login(app)
11
    resp = app.get('/manage/pricing/agendas/')
12
    resp = resp.click('Check types')
13
    resp = resp.click('New group')
14
    resp.form['label'] = 'Foo bar'
15
    resp = resp.form.submit()
16
    group = CheckTypeGroup.objects.latest('pk')
17
    assert resp.location.endswith('/manage/pricing/check-types/')
18
    assert group.label == 'Foo bar'
19
    assert group.slug == 'foo-bar'
20

  
21

  
22
def test_edit_group(app, admin_user):
23
    group = CheckTypeGroup.objects.create(label='Foo bar')
24
    group2 = CheckTypeGroup.objects.create(label='baz')
25

  
26
    app = login(app)
27
    resp = app.get('/manage/pricing/check-types/')
28
    resp = resp.click(href='/manage/pricing/check-type/group/%s/edit/' % group.pk)
29
    resp.form['label'] = 'Foo bar baz'
30
    resp.form['slug'] = group2.slug
31
    resp = resp.form.submit()
32
    assert resp.context['form'].errors['slug'] == ['Check type group with this Identifier already exists.']
33

  
34
    resp.form['slug'] = 'baz2'
35
    resp = resp.form.submit()
36
    assert resp.location.endswith('/manage/pricing/check-types/')
37
    group.refresh_from_db()
38
    assert group.label == 'Foo bar baz'
39
    assert group.slug == 'baz2'
40

  
41

  
42
def test_delete_group(app, admin_user):
43
    group = CheckTypeGroup.objects.create(label='Foo bar')
44
    CheckType.objects.create(label='Foo reason', group=group)
45

  
46
    app = login(app)
47
    resp = app.get('/manage/pricing/check-types/')
48
    resp = resp.click(href='/manage/pricing/check-type/group/%s/delete/' % group.pk)
49
    resp = resp.form.submit()
50
    assert resp.location.endswith('/manage/pricing/check-types/')
51
    assert CheckTypeGroup.objects.exists() is False
52
    assert CheckType.objects.exists() is False
53

  
54

  
55
def test_add_check_type(app, admin_user):
56
    group = CheckTypeGroup.objects.create(label='Foo bar')
57

  
58
    app = login(app)
59
    resp = app.get('/manage/pricing/check-types/')
60
    resp = resp.click('Add a check type')
61
    resp.form['label'] = 'Foo reason'
62
    assert 'slug' not in resp.context['form'].fields
63
    assert 'disabled' not in resp.context['form'].fields
64
    resp = resp.form.submit()
65
    check_type = CheckType.objects.latest('pk')
66
    assert resp.location.endswith('/manage/pricing/check-types/')
67
    assert check_type.label == 'Foo reason'
68
    assert check_type.group == group
69
    assert check_type.slug == 'foo-reason'
70
    assert check_type.kind == 'absence'
71
    assert check_type.pricing is None
72
    assert check_type.pricing_rate is None
73

  
74
    resp = app.get('/manage/pricing/check-type/group/%s/add/' % group.pk)
75
    resp.form['label'] = 'Foo reason'
76
    resp.form['kind'] = 'presence'
77
    resp = resp.form.submit()
78
    assert resp.location.endswith('/manage/pricing/check-types/')
79
    check_type = CheckType.objects.latest('pk')
80
    assert check_type.label == 'Foo reason'
81
    assert check_type.slug == 'foo-reason-1'
82
    assert check_type.kind == 'presence'
83

  
84

  
85
def test_add_check_type_pricing(app, admin_user):
86
    group = CheckTypeGroup.objects.create(label='Foo bar')
87

  
88
    app = login(app)
89
    resp = app.get('/manage/pricing/check-type/group/%s/add/' % group.pk)
90
    assert 'pricing' in resp.context['form'].fields
91
    assert 'pricing_rate' in resp.context['form'].fields
92
    resp.form['label'] = 'Foo reason'
93
    resp.form['pricing'] = 42
94
    resp.form['pricing_rate'] = 150
95
    resp = resp.form.submit()
96
    assert resp.context['form'].errors['__all__'] == ['Please choose between pricing and pricing rate.']
97
    resp.form['pricing'] = 0
98
    resp.form['pricing_rate'] = 0
99
    resp = resp.form.submit()
100
    assert resp.context['form'].errors['__all__'] == ['Please choose between pricing and pricing rate.']
101
    resp.form['pricing'] = 42
102
    resp.form['pricing_rate'] = None
103
    resp = resp.form.submit()
104
    check_type = CheckType.objects.latest('pk')
105
    assert check_type.pricing == 42
106
    assert check_type.pricing_rate is None
107

  
108
    resp = app.get('/manage/pricing/check-type/group/%s/add/' % group.pk)
109
    resp.form['label'] = 'Foo reason'
110
    resp.form['pricing_rate'] = 150
111
    resp = resp.form.submit()
112
    check_type = CheckType.objects.latest('pk')
113
    assert check_type.pricing is None
114
    assert check_type.pricing_rate == 150
115

  
116

  
117
def test_edit_check_type(app, admin_user):
118
    group = CheckTypeGroup.objects.create(label='Foo bar')
119
    check_type = CheckType.objects.create(label='Foo reason', group=group, kind='presence')
120
    check_type2 = CheckType.objects.create(label='Baz', group=group)
121
    group2 = CheckTypeGroup.objects.create(label='Foo bar')
122
    check_type3 = CheckType.objects.create(label='Foo bar reason', group=group2)
123

  
124
    app = login(app)
125
    resp = app.get('/manage/pricing/check-types/')
126
    resp = resp.click(href='/manage/pricing/check-type/group/%s/%s/edit/' % (group.pk, check_type.pk))
127
    assert 'This check type is set on some existing bookings, modify it with caution.' not in resp
128
    resp.form['label'] = 'Foo bar reason'
129
    resp.form['slug'] = check_type2.slug
130
    resp.form['disabled'] = True
131
    assert 'kind' not in resp.context['form'].fields
132
    resp = resp.form.submit()
133
    assert resp.context['form'].errors['slug'] == ['Another check type exists with the same identifier.']
134

  
135
    resp.form['slug'] = check_type3.slug
136
    resp = resp.form.submit()
137
    assert resp.location.endswith('/manage/pricing/check-types/')
138
    check_type.refresh_from_db()
139
    assert check_type.label == 'Foo bar reason'
140
    assert check_type.slug == 'foo-bar-reason'
141
    assert check_type.kind == 'presence'
142
    assert check_type.pricing is None
143
    assert check_type.pricing_rate is None
144
    assert check_type.disabled is True
145

  
146
    app.get('/manage/pricing/check-type/group/%s/%s/edit/' % (group2.pk, check_type.pk), status=404)
147

  
148

  
149
def test_edit_check_type_pricing(app, admin_user):
150
    group = CheckTypeGroup.objects.create(label='Foo bar')
151
    check_type = CheckType.objects.create(label='Foo reason', group=group)
152

  
153
    app = login(app)
154
    resp = app.get('/manage/pricing/check-type/group/%s/%s/edit/' % (group.pk, check_type.pk))
155
    assert 'pricing' in resp.context['form'].fields
156
    assert 'pricing_rate' in resp.context['form'].fields
157
    resp.form['pricing'] = 42
158
    resp.form['pricing_rate'] = 150
159
    resp = resp.form.submit()
160
    assert resp.context['form'].errors['__all__'] == ['Please choose between pricing and pricing rate.']
161
    resp.form['pricing'] = 42
162
    resp.form['pricing_rate'] = None
163
    resp = resp.form.submit()
164
    check_type.refresh_from_db()
165
    assert check_type.pricing == 42
166
    assert check_type.pricing_rate is None
167

  
168
    resp = app.get('/manage/pricing/check-type/group/%s/%s/edit/' % (group.pk, check_type.pk))
169
    resp.form['pricing'] = None
170
    resp.form['pricing_rate'] = 150
171
    resp = resp.form.submit()
172
    check_type.refresh_from_db()
173
    assert check_type.pricing is None
174
    assert check_type.pricing_rate == 150
175

  
176

  
177
def test_delete_check_type(app, admin_user):
178
    group = CheckTypeGroup.objects.create(label='Foo bar')
179
    check_type = CheckType.objects.create(label='Foo reason', group=group)
180

  
181
    app = login(app)
182
    resp = app.get('/manage/pricing/check-types/')
183
    resp = resp.click(href='/manage/pricing/check-type/group/%s/%s/delete/' % (group.pk, check_type.pk))
184
    resp = resp.form.submit()
185
    assert resp.location.endswith('/manage/pricing/check-types/')
186
    assert CheckTypeGroup.objects.exists() is True
187
    assert CheckType.objects.exists() is False
188

  
189
    group2 = CheckTypeGroup.objects.create(label='Foo bar baz')
190
    app.get('/manage/pricing/check-type/group/%s/%s/delete/' % (group2.pk, check_type.pk), status=404)
0
-