Project

General

Profile

0004-api-add-category-parameter-on-add-agenda-endpoint-57.patch

Nicolas Roche, 08 Oct 2021 10:43 AM

Download (6.09 KB)

View differences:

Subject: [PATCH 4/5] api: add category parameter on add agenda endpoint
 (#57670)

 chrono/api/serializers.py | 10 +++++++++-
 tests/api/test_all.py     |  7 +++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
chrono/api/serializers.py
1 1
from django.contrib.auth.models import Group
2 2
from django.utils.translation import ugettext_lazy as _
3 3
from rest_framework import serializers
4 4
from rest_framework.exceptions import ValidationError
5 5

  
6
from chrono.agendas.models import AbsenceReason, Agenda, Booking, Event
6
from chrono.agendas.models import AbsenceReason, Agenda, Booking, Category, Event
7 7

  
8 8

  
9 9
class StringOrListField(serializers.ListField):
10 10
    def to_internal_value(self, data):
11 11
        if isinstance(data, str):
12 12
            data = [s.strip() for s in data.split(',') if s.strip()]
13 13
        return super().to_internal_value(data)
14 14

  
......
158 158
            'pricing',
159 159
            'url',
160 160
        ]
161 161

  
162 162

  
163 163
class AgendaSerializer(serializers.ModelSerializer):
164 164
    edit_role = serializers.CharField(required=False, max_length=150)
165 165
    view_role = serializers.CharField(required=False, max_length=150)
166
    category = serializers.SlugField(required=False, max_length=160)
166 167

  
167 168
    class Meta:
168 169
        model = Agenda
169 170
        fields = [
170 171
            'slug',
171 172
            'label',
172 173
            'kind',
173 174
            'minimal_booking_delay',
174 175
            'minimal_booking_delay_in_working_days',
175 176
            'maximal_booking_delay',
176 177
            'anonymize_delay',
177 178
            'edit_role',
178 179
            'view_role',
180
            'category',
179 181
        ]
180 182

  
181 183
    def get_role(self, value):
182 184
        try:
183 185
            return Group.objects.get(name=value)
184 186
        except Group.DoesNotExist:
185 187
            raise serializers.ValidationError(_('unknown role: %s' % value))
186 188

  
187 189
    def validate_edit_role(self, value):
188 190
        return self.get_role(value)
189 191

  
190 192
    def validate_view_role(self, value):
191 193
        return self.get_role(value)
192 194

  
195
    def validate_category(self, value):
196
        try:
197
            return Category.objects.get(slug=value)
198
        except Category.DoesNotExist:
199
            raise serializers.ValidationError(_('unknown category: %s' % value))
200

  
193 201
    def validate(self, attrs):
194 202
        super().validate(attrs)
195 203
        if attrs['minimal_booking_delay_in_working_days'] and attrs.get('kind', 'events') != 'events':
196 204
            raise ValidationError(
197 205
                {
198 206
                    'minimal_booking_delay_in_working_days': _('Option not available on %s agenda')
199 207
                    % attrs['kind']
200 208
                }
tests/api/test_all.py
598 598
                },
599 599
            },
600 600
        ]
601 601
    }
602 602

  
603 603

  
604 604
@pytest.mark.freeze_time('2021-07-09')
605 605
def test_add_agenda(app, user, settings):
606
    category_a = Category.objects.create(label='Category A')
606 607
    api_url = '/api/agenda/'
607 608

  
608 609
    # no authentication
609 610
    resp = app.post(api_url, status=401)
610 611
    assert resp.json['detail'] == 'Authentication credentials were not provided.'
611 612

  
612 613
    # wrong password
613 614
    app.authorization = ('Basic', ('john.doe', 'wrong'))
......
626 627
        'label': 'foo',
627 628
        'slug': 'foo',
628 629
        'kind': 'oups',
629 630
        'minimal_booking_delay': 'oups',
630 631
        'minimal_booking_delay_in_working_days': 'oups',
631 632
        'anonymize_delay': 'oups',
632 633
        'edit_role': 'oups',
633 634
        'view_role': 'plop',
635
        'category': 'oups',
634 636
    }
635 637
    resp = app.post(api_url, params=params, status=400)
636 638
    assert resp.json['err']
637 639
    assert resp.json['errors'] == {
638 640
        'kind': ['"oups" is not a valid choice.'],
639 641
        'minimal_booking_delay': ['A valid integer is required.'],
640 642
        'minimal_booking_delay_in_working_days': ['Must be a valid boolean.'],
641 643
        'anonymize_delay': ['A valid integer is required.'],
642 644
        'edit_role': ['unknown role: oups'],
643 645
        'view_role': ['unknown role: plop'],
646
        'category': ['unknown category: oups'],
644 647
    }
645 648

  
646 649
    # slug already used
647 650
    meeting_agenda = Agenda(label='Foo bar Meeting', kind='meetings')
648 651
    meeting_agenda.save()
649 652
    params = {
650 653
        'label': 'foo',
651 654
        'slug': meeting_agenda.slug,
......
687 690
        'label': 'foo Meetings',
688 691
        'slug': 'foo-meetings',
689 692
        'kind': 'meetings',
690 693
        'minimal_booking_delay': 1,
691 694
        'maximal_booking_delay': 3,
692 695
        'anonymize_delay': 30,
693 696
        'edit_role': 'Edit',
694 697
        'view_role': 'View',
698
        'category': 'category-a',
695 699
    }
696 700
    resp = app.post(api_url, params=params)
697 701
    assert not resp.json['err']
698 702
    assert len(resp.json['data']) == 1
699 703
    agenda = Agenda.objects.get(slug='foo-meetings')
700 704
    assert agenda.min_booking_datetime.date() == datetime.date(2021, 7, 10)
701 705
    assert agenda.edit_role == edit_group
702 706
    assert agenda.view_role == view_group
707
    assert agenda.category == category_a
703 708

  
704 709
    # add an events agenda
705 710
    params = {
706 711
        'label': 'foo Events',
707 712
        'slug': 'foo-events',
708 713
        'kind': 'events',
709 714
        'minimal_booking_delay': 1,
710 715
        'minimal_booking_delay_in_working_days': True,
711 716
        'maximal_booking_delay': 3,
712 717
        'anonymize_delay': 30,
713 718
        'edit_role': 'Edit',
714 719
        'view_role': 'View',
720
        'category': 'category-a',
715 721
    }
716 722
    resp = app.post(api_url, params=params)
717 723
    assert not resp.json['err']
718 724
    assert len(resp.json['data']) == 1
719 725
    agenda = Agenda.objects.get(slug='foo-events')
720 726
    assert agenda.min_booking_datetime.date() == datetime.date(2021, 7, 12)
721 727
    assert agenda.edit_role == edit_group
722 728
    assert agenda.view_role == view_group
729
    assert agenda.category == category_a
723
-