Projet

Général

Profil

0004-pricing-agendas-not-linkable-if-subscription-not-req.patch

Lauréline Guérin, 29 juillet 2022 21:57

Télécharger (6,56 ko)

Voir les différences:

Subject: [PATCH 04/14] pricing: agendas not linkable if subscription not
 required (#67675)

 lingo/pricing/forms.py                        | 10 +++++++
 .../manager_agenda_pricing_detail.html        |  4 +++
 lingo/pricing/views.py                        |  4 +--
 tests/pricing/manager/test_agenda_pricing.py  | 26 +++++++++++++++++++
 4 files changed, 42 insertions(+), 2 deletions(-)
lingo/pricing/forms.py
174 174

  
175 175
        return slug
176 176

  
177
    def clean_subscription_required(self):
178
        subscription_required = self.cleaned_data['subscription_required']
179
        if subscription_required is False and self.instance.subscription_required is True:
180
            # value has changed, check linked agendas
181
            if self.instance.agendas.exists():
182
                raise forms.ValidationError(
183
                    _('Some agendas are linked to this pricing; please unlink them first.')
184
                )
185
        return subscription_required
186

  
177 187

  
178 188
class AgendaPricingAgendaAddForm(forms.Form):
179 189
    agenda = forms.ModelChoiceField(label=_('Agenda to add'), queryset=Agenda.objects.none(), required=True)
lingo/pricing/templates/lingo/pricing/manager_agenda_pricing_detail.html
27 27
<div class="pk-tabs">
28 28
  <div class="pk-tabs--tab-list" role="tablist">
29 29
    <button aria-controls="panel-options" aria-selected="true" id="tab-options" role="tab" tabindex="0">{% trans "Options" %}</button>
30
    {% if object.subscription_required %}
30 31
    <button aria-controls="panel-agendas" aria-selected="false" id="tab-agendas" role="tab" tabindex="0">{% trans "Agendas" %}</button>
32
    {% endif %}
31 33
    <button aria-controls="panel-debug" aria-selected="false" id="tab-debug" role="tab" tabindex="0">{% trans "Test tool" %}</button>
32 34
    {% for matrix in iter_matrix %}
33 35
    <button aria-controls="panel-matrix-{{ matrix.criteria.slug }}" aria-selected="false" id="tab-matrix-{{ matrix.criteria.slug }}" role="tab" tabindex="-1">{% trans "Pricings" context 'amount' %}{% if matrix.criteria %} - {{ matrix.criteria.label }}{% endif %}</button>
......
48 50
      </ul>
49 51
    </div>
50 52

  
53
    {% if object.subscription_required %}
51 54
    <div aria-labelledby="tab-agendas" hidden id="panel-agendas" role="tabpanel" tabindex="0">
52 55
      <ul class="objects-list single-links">
53 56
        {% for agenda in agendas %}
......
63 66
        <a class="pk-button" rel="popup" href="{% url 'lingo-manager-agenda-pricing-agenda-add' object.pk %}">{% trans "Add an agenda" %}</a>
64 67
      </div>
65 68
    </div>
69
    {% endif %}
66 70

  
67 71
    <div aria-labelledby="tab-debug" hidden id="panel-debug" role="tabpanel" tabindex="0">
68 72
      <form method="get" enctype="multipart/form-data" action="{% url 'lingo-manager-agenda-pricing-test-tool' object.pk %}">
lingo/pricing/views.py
834 834
    form_class = AgendaPricingAgendaAddForm
835 835

  
836 836
    def dispatch(self, request, *args, **kwargs):
837
        self.object = get_object_or_404(AgendaPricing, pk=kwargs['pk'])
837
        self.object = get_object_or_404(AgendaPricing, pk=kwargs['pk'], subscription_required=True)
838 838
        return super().dispatch(request, *args, **kwargs)
839 839

  
840 840
    def get_form_kwargs(self):
......
863 863
    pk_url_kwarg = 'agenda_pk'
864 864

  
865 865
    def dispatch(self, request, *args, **kwargs):
866
        self.agenda_pricing = get_object_or_404(AgendaPricing, pk=kwargs['pk'])
866
        self.agenda_pricing = get_object_or_404(AgendaPricing, pk=kwargs['pk'], subscription_required=True)
867 867
        return super().dispatch(request, *args, **kwargs)
868 868

  
869 869
    def get_queryset(self):
tests/pricing/manager/test_agenda_pricing.py
108 108
    assert agenda_pricing.flat_fee_schedule is True
109 109
    assert agenda_pricing.subscription_required is False
110 110

  
111
    agenda = Agenda.objects.create(label='Foo bar')
112
    agenda_pricing.agendas.add(agenda)
113
    agenda_pricing.subscription_required = True
114
    agenda_pricing.save()
115
    resp = app.get('/manage/pricing/agenda-pricing/%s/edit/' % agenda_pricing.pk)
116
    resp.form['subscription_required'] = False
117
    resp = resp.form.submit()
118
    assert resp.context['form'].errors['subscription_required'] == [
119
        'Some agendas are linked to this pricing; please unlink them first.'
120
    ]
121
    agenda_pricing.subscription_required = False
122
    agenda_pricing.save()
123
    resp = app.get('/manage/pricing/agenda-pricing/%s/edit/' % agenda_pricing.pk)
124
    resp = resp.form.submit()
125
    assert resp.location.endswith('/manage/pricing/agenda-pricing/%s/' % agenda_pricing.pk)
126

  
111 127

  
112 128
def test_detail_agenda_pricing(app, admin_user):
113 129
    pricing = Pricing.objects.create(label='Model')
......
196 212
    resp = resp.form.submit().follow()
197 213
    assert list(agenda_pricing2.agendas.all()) == [agenda3]
198 214

  
215
    agenda_pricing.subscription_required = False
216
    agenda_pricing.save()
217
    app.get('/manage/pricing/agenda-pricing/%s/agenda/add/' % agenda_pricing.pk, status=404)
218

  
199 219

  
200 220
def test_agenda_pricing_delete_agenda(app, admin_user):
201 221
    pricing = Pricing.objects.create(label='Model')
......
226 246
    # unknown
227 247
    app.get('/manage/pricing/agenda-pricing/%s/agenda/%s/delete/' % (agenda_pricing.pk, 0), status=404)
228 248

  
249
    agenda_pricing.subscription_required = False
250
    agenda_pricing.save()
251
    app.get(
252
        '/manage/pricing/agenda-pricing/%s/agenda/%s/delete/' % (agenda_pricing.pk, agenda2.pk), status=404
253
    )
254

  
229 255

  
230 256
def test_detail_agenda_pricing_3_categories(app, admin_user):
231 257
    category1 = CriteriaCategory.objects.create(label='Cat 1')
232
-