Projet

Général

Profil

0010-pricing-add-an-agenda-selector-in-test-tool-67675.patch

Lauréline Guérin, 29 juillet 2022 22:03

Télécharger (8,91 ko)

Voir les différences:

Subject: [PATCH 10/14] pricing: add an agenda selector in test tool (#67675)

 lingo/pricing/forms.py                        | 18 +++++++--------
 .../manager_agenda_pricing_detail.html        | 20 ++++++++--------
 tests/pricing/manager/test_agenda_pricing.py  | 23 ++++++++-----------
 3 files changed, 27 insertions(+), 34 deletions(-)
lingo/pricing/forms.py
287 287

  
288 288

  
289 289
class PricingTestToolForm(forms.Form):
290
    agenda = forms.ModelChoiceField(label=_('Agenda'), empty_label=None, queryset=Agenda.objects.none())
290 291
    event_slug = forms.CharField(label=_('Event identifier'))
291 292
    user_external_id = forms.CharField(label=_('User external identifier'))
292 293
    adult_external_id = forms.CharField(label=_('Adult external identifier'))
......
296 297
        self.request = kwargs.pop('request')
297 298
        self.agenda_pricing = kwargs.pop('agenda_pricing')
298 299
        self.agenda = None
299
        if kwargs['data'] and kwargs['data'].get('event_slug'):
300
            self.init_agenda(kwargs['data']['event_slug'])
300
        if kwargs['data'] and kwargs['data'].get('agenda'):
301
            self.init_agenda(kwargs['data']['agenda'])
301 302
        self.serialized_event = None
302 303
        self.serialized_subscription = None
303 304
        self.check_type_slug = None
304 305
        self.booking_status = None
305 306
        super().__init__(*args, **kwargs)
307
        self.fields['agenda'].queryset = self.agenda_pricing.agendas.all()
306 308
        presence_check_types = (
307 309
            self.agenda.check_type_group.check_types.presences()
308 310
            if self.agenda and self.agenda.check_type_group
......
325 327
        ]
326 328
        self.fields['booking_status'].choices = status_choices
327 329

  
328
    def init_agenda(self, event_slug):
329
        agenda_slug = event_slug.split('@')[0]
330
    def init_agenda(self, agenda_id):
330 331
        try:
331
            self.agenda = self.agenda_pricing.agendas.get(slug=agenda_slug)
332
            self.agenda = self.agenda_pricing.agendas.get(pk=agenda_id)
332 333
        except Agenda.DoesNotExist:
333 334
            pass
334 335

  
335 336
    def clean_event_slug(self):
336 337
        event_slug = self.cleaned_data['event_slug']
337
        if '@' not in event_slug:
338
            raise ValidationError(_('Missing agenda identifier'))
339
        agenda_slug = event_slug.split('@')[0]
340 338
        if not self.agenda:
341
            raise ValidationError(_('The agenda identifier is wrong (%s)') % agenda_slug)
339
            return event_slug
342 340
        try:
343
            self.serialized_event = get_event(event_slug)
341
            self.serialized_event = get_event('%s@%s' % (self.agenda.slug, event_slug))
344 342
        except ChronoError as e:
345 343
            raise forms.ValidationError(e)
346 344

  
lingo/pricing/templates/lingo/pricing/manager_agenda_pricing_detail.html
79 79
            var presences = {};
80 80
            var absences = {};
81 81
            {% for agenda in agendas %}
82
              presences['{{ agenda.slug }}'] = [];
83
              absences['{{ agenda.slug }}'] = [];
82
              presences['{{ agenda.pk }}'] = [];
83
              absences['{{ agenda.pk }}'] = [];
84 84
              {% for check_type in agenda.check_type_group.check_types.all %}
85 85
                {% if check_type.kind == "presence" %}
86
                  presences['{{ agenda.slug }}'].push({slug: '{{ check_type.slug }}', label: '{{ check_type.label }}'});
86
                  presences['{{ agenda.pk }}'].push({slug: '{{ check_type.slug }}', label: '{{ check_type.label }}'});
87 87
                {% else %}
88
                  absences['{{ agenda.slug }}'].push({slug: '{{ check_type.slug }}', label: '{{ check_type.label }}'});
88
                  absences['{{ agenda.pk }}'].push({slug: '{{ check_type.slug }}', label: '{{ check_type.label }}'});
89 89
                {% endif %}
90 90
              {% endfor %}
91 91
            {% endfor %}
92
            $('#id_event_slug').on('change', function() {
93
              var agenda_slug = $(this).val().split('@')[0];
92
            $('#id_agenda').on('change', function() {
93
              var agenda_id = $(this).val();
94 94
              var $select = $('#id_booking_status');
95 95
              var current_value = $select.val();
96 96
              $select.find('option').remove().end().append('<option value="presence">' + '{% trans "Presence" %}' + '</option>');
97
              if (presences[agenda_slug]) {
98
                $.each(presences[agenda_slug], function(index, value) {
97
              if (presences[agenda_id]) {
98
                $.each(presences[agenda_id], function(index, value) {
99 99
                  $select.append('<option value="presence::' + value.slug + '">' + '{% trans "Presence" %} (' + value.label + ')</option>');
100 100
                });
101 101
              }
102 102
              $select.append('<option value="absence">' + '{% trans "Absence" %}' + '</option>');
103
              if (absences[agenda_slug]) {
104
                $.each(absences[agenda_slug], function(index, value) {
103
              if (absences[agenda_id]) {
104
                $.each(absences[agenda_id], function(index, value) {
105 105
                  $select.append('<option value="absence::' + value.slug + '">' + '{% trans "Absence" %} (' + value.label + ')</option>');
106 106
                });
107 107
              }
tests/pricing/manager/test_agenda_pricing.py
840 840

  
841 841
    # check event date
842 842
    mock_event.return_value = {'start_datetime': '2021-08-31T12:00:00+02:00'}
843
    resp.form['event_slug'] = 'foo-bar@foo'
843
    resp.form['agenda'] = agenda.pk
844
    resp.form['event_slug'] = 'foo'
844 845
    resp.form['user_external_id'] = 'user:1'
845 846
    resp.form['adult_external_id'] = 'adult:1'
846 847
    resp.form['booking_status'] = 'presence'
......
859 860

  
860 861
    mock_event.return_value = {'start_datetime': '2021-09-01T12:00:00+02:00'}
861 862

  
862
    # check event_slug & agenda
863
    resp.form['event_slug'] = 'foo'
864
    resp = resp.form.submit().follow()
865
    assert resp.context['test_tool_form'].errors['event_slug'] == ['Missing agenda identifier']
866
    resp.form['event_slug'] = 'foo@foo'
867
    resp = resp.form.submit().follow()
868
    assert resp.context['test_tool_form'].errors['event_slug'] == ['The agenda identifier is wrong (foo)']
869

  
870 863
    # check subscriptions dates
871 864
    mock_subscriptions.return_value = []
872 865
    mock_event.reset_mock()
873
    resp.form['event_slug'] = 'foo-bar@foo'
874 866
    resp = resp.form.submit().follow()
875 867
    assert mock_pricing_data_event.call_args_list == []
876 868
    assert 'Computed pricing data' not in resp
......
944 936
    )
945 937
    agenda_pricing.agendas.add(agenda)
946 938

  
947
    mock_event.side_effect = ChronoError('foo bar foo-bar@foo-event')
939
    mock_event.side_effect = ChronoError('foo bar foo-event')
948 940

  
949 941
    app = login(app)
950 942
    resp = app.get('/manage/pricing/agenda-pricing/%s/' % agenda_pricing.pk)
951
    resp.form['event_slug'] = 'foo-bar@foo-event'
943
    resp.form['agenda'] = agenda.pk
944
    resp.form['event_slug'] = 'foo-event'
952 945
    resp.form['user_external_id'] = 'user:1'
953 946
    resp.form['adult_external_id'] = 'adult:1'
954 947
    resp.form['booking_status'] = 'presence'
955 948
    resp = resp.form.submit().follow()
956
    assert resp.context['test_tool_form'].errors['event_slug'] == ['foo bar foo-bar@foo-event']
949
    assert resp.context['test_tool_form'].errors['event_slug'] == ['foo bar foo-event']
957 950

  
958 951

  
959 952
@mock.patch('lingo.pricing.forms.get_event')
......
973 966

  
974 967
    app = login(app)
975 968
    resp = app.get('/manage/pricing/agenda-pricing/%s/' % agenda_pricing.pk)
969
    resp.form['agenda'] = agenda.pk
976 970
    resp.form['event_slug'] = 'foo-bar@foo-event'
977 971
    resp.form['user_external_id'] = 'user:1'
978 972
    resp.form['adult_external_id'] = 'adult:1'
......
1022 1016
        ('presence', False, 'Presence'),
1023 1017
        ('absence', False, 'Absence'),
1024 1018
    ]
1025
    resp.form['event_slug'] = 'foo-bar@foo'
1019
    resp.form['agenda'] = agenda.pk
1020
    resp.form['event_slug'] = 'foo'
1026 1021
    resp.form['user_external_id'] = 'user:1'
1027 1022
    resp.form['adult_external_id'] = 'adult:1'
1028 1023
    resp.form['booking_status'] = 'presence'
1029
-