api: keep user_absence_reason field (#63810)
This commit is contained in:
parent
b1c85499df
commit
4547d00787
|
@ -138,19 +138,24 @@ class RecurringFillslotsSerializer(MultipleAgendasEventsSlotsSerializer):
|
|||
|
||||
|
||||
class BookingSerializer(serializers.ModelSerializer):
|
||||
user_check_type = serializers.CharField(required=False, allow_blank=True, allow_null=True)
|
||||
user_absence_reason = serializers.CharField(required=False, allow_blank=True, allow_null=True)
|
||||
|
||||
class Meta:
|
||||
model = Booking
|
||||
fields = ['id', 'in_waiting_list', 'user_was_present', 'user_check_type', 'extra_data']
|
||||
fields = ['id', 'in_waiting_list', 'user_was_present', 'user_absence_reason', 'extra_data']
|
||||
read_only_fields = ['id', 'in_waiting_list', 'extra_data']
|
||||
|
||||
def validate_user_check_type(self, value):
|
||||
def to_representation(self, instance):
|
||||
ret = super().to_representation(instance)
|
||||
ret['user_absence_reason'] = self.instance.user_check_type
|
||||
return ret
|
||||
|
||||
def validate_user_absence_reason(self, value):
|
||||
if not value:
|
||||
return ''
|
||||
|
||||
if not self.instance.event.agenda.check_type_group:
|
||||
raise serializers.ValidationError(_('unknown check type'))
|
||||
raise serializers.ValidationError(_('unknown absence reason'))
|
||||
|
||||
check_types_qs = self.instance.event.agenda.check_type_group.check_types
|
||||
try:
|
||||
|
@ -158,10 +163,17 @@ class BookingSerializer(serializers.ModelSerializer):
|
|||
value = check_type.label
|
||||
except CheckType.DoesNotExist:
|
||||
if not check_types_qs.filter(label=value).exists():
|
||||
raise serializers.ValidationError(_('unknown check type'))
|
||||
raise serializers.ValidationError(_('unknown absence'))
|
||||
|
||||
return value
|
||||
|
||||
def validate(self, attrs):
|
||||
super().validate(attrs)
|
||||
if 'user_absence_reason' in attrs:
|
||||
attrs['user_check_type'] = attrs['user_absence_reason']
|
||||
del attrs['user_absence_reason']
|
||||
return attrs
|
||||
|
||||
|
||||
class ResizeSerializer(serializers.Serializer):
|
||||
count = serializers.IntegerField(min_value=1)
|
||||
|
|
|
@ -2225,11 +2225,15 @@ class BookingFilter(filters.FilterSet):
|
|||
category = filters.CharFilter(field_name='event__agenda__category__slug', lookup_expr='exact')
|
||||
date_start = filters.DateFilter(field_name='event__start_datetime', lookup_expr='gte')
|
||||
date_end = filters.DateFilter(field_name='event__start_datetime', lookup_expr='lt')
|
||||
user_absence_reason = filters.CharFilter(method='filter_user_absence_reason')
|
||||
|
||||
def filter_event(self, queryset, name, value):
|
||||
# we want to include bookings of event recurrences
|
||||
return queryset.filter(Q(event__slug=value) | Q(event__primary_event__slug=value))
|
||||
|
||||
def filter_user_absence_reason(self, queryset, name, value):
|
||||
return queryset.filter(user_check_type=value)
|
||||
|
||||
class Meta:
|
||||
model = Booking
|
||||
fields = [
|
||||
|
@ -2239,7 +2243,7 @@ class BookingFilter(filters.FilterSet):
|
|||
'date_start',
|
||||
'date_end',
|
||||
'user_was_present',
|
||||
'user_check_type',
|
||||
'user_absence_reason',
|
||||
'in_waiting_list',
|
||||
]
|
||||
|
||||
|
@ -2320,7 +2324,7 @@ class BookingAPI(APIView):
|
|||
if (
|
||||
self.booking.event.checked
|
||||
and self.booking.event.agenda.disable_check_update
|
||||
and ('user_was_present' in request.data or 'user_check_type' in request.data)
|
||||
and ('user_was_present' in request.data or 'user_absence_reason' in request.data)
|
||||
):
|
||||
raise APIErrorBadRequest(N_('event is marked as checked'), err=5)
|
||||
|
||||
|
@ -2334,7 +2338,7 @@ class BookingAPI(APIView):
|
|||
if 'user_was_present' in request.data:
|
||||
self.booking.secondary_booking_set.update(user_was_present=self.booking.user_was_present)
|
||||
self.booking.event.set_is_checked()
|
||||
if 'user_check_type' in request.data:
|
||||
if 'user_absence_reason' in request.data:
|
||||
self.booking.secondary_booking_set.update(user_check_type=self.booking.user_check_type)
|
||||
if extra_data:
|
||||
self.booking.secondary_booking_set.update(extra_data=self.booking.extra_data)
|
||||
|
|
|
@ -146,14 +146,14 @@ def test_bookings_api(app, user):
|
|||
'id': meetings_booking1.pk,
|
||||
'in_waiting_list': False,
|
||||
'user_was_present': None,
|
||||
'user_check_type': '',
|
||||
'user_absence_reason': '',
|
||||
'extra_data': None,
|
||||
},
|
||||
{
|
||||
'id': events_booking1.pk,
|
||||
'in_waiting_list': False,
|
||||
'user_was_present': None,
|
||||
'user_check_type': '',
|
||||
'user_absence_reason': '',
|
||||
'extra_data': None,
|
||||
'event': resp.json['data'][1]['event'],
|
||||
},
|
||||
|
@ -161,7 +161,7 @@ def test_bookings_api(app, user):
|
|||
'id': events_booking2.pk,
|
||||
'in_waiting_list': False,
|
||||
'user_was_present': None,
|
||||
'user_check_type': '',
|
||||
'user_absence_reason': '',
|
||||
'extra_data': None,
|
||||
'event': resp.json['data'][1]['event'],
|
||||
},
|
||||
|
@ -298,7 +298,7 @@ def test_bookings_api_filter_user_was_present(app, user):
|
|||
assert [b['id'] for b in resp.json['data']] == [booking3.pk]
|
||||
|
||||
|
||||
def test_bookings_api_filter_user_check_type(app, user):
|
||||
def test_bookings_api_filter_user_absence_reason(app, user):
|
||||
agenda = Agenda.objects.create(label='Foo bar')
|
||||
event = Event.objects.create(
|
||||
agenda=agenda, start_datetime=make_aware(datetime.datetime(2017, 5, 22, 0, 0)), places=10
|
||||
|
@ -307,10 +307,10 @@ def test_bookings_api_filter_user_check_type(app, user):
|
|||
booking2 = Booking.objects.create(event=event, user_external_id='42', user_check_type='foo-bar')
|
||||
|
||||
app.authorization = ('Basic', ('john.doe', 'password'))
|
||||
resp = app.get('/api/bookings/', params={'user_external_id': '42', 'user_check_type': 'foo'})
|
||||
resp = app.get('/api/bookings/', params={'user_external_id': '42', 'user_absence_reason': 'foo'})
|
||||
assert resp.json['err'] == 0
|
||||
assert [b['id'] for b in resp.json['data']] == []
|
||||
resp = app.get('/api/bookings/', params={'user_external_id': '42', 'user_check_type': 'foo-bar'})
|
||||
resp = app.get('/api/bookings/', params={'user_external_id': '42', 'user_absence_reason': 'foo-bar'})
|
||||
assert resp.json['err'] == 0
|
||||
assert [b['id'] for b in resp.json['data']] == [booking2.pk]
|
||||
|
||||
|
@ -391,7 +391,7 @@ def test_booking_api_present(app, user, flag):
|
|||
resp = app.get('/api/booking/%s/' % booking.pk)
|
||||
assert resp.json['booking_id'] == booking.pk
|
||||
assert resp.json['user_was_present'] == flag
|
||||
assert resp.json['user_check_type'] == 'foobar'
|
||||
assert resp.json['user_absence_reason'] == 'foobar'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('flag', [True, False])
|
||||
|
@ -527,32 +527,36 @@ def test_booking_patch_api_absence_reason(app, user):
|
|||
app.authorization = ('Basic', ('john.doe', 'password'))
|
||||
|
||||
# reasons not defined on agenda
|
||||
resp = app.patch_json('/api/booking/%s/' % booking.pk, params={'user_check_type': 'foobar'}, status=400)
|
||||
resp = app.patch_json(
|
||||
'/api/booking/%s/' % booking.pk, params={'user_absence_reason': 'foobar'}, status=400
|
||||
)
|
||||
assert resp.json['err'] == 4
|
||||
assert resp.json['err_desc'] == 'invalid payload'
|
||||
|
||||
group = CheckTypeGroup.objects.create(label='Foo')
|
||||
reason = CheckType.objects.create(group=group, label='Foo bar')
|
||||
check_type = CheckType.objects.create(group=group, label='Foo bar')
|
||||
|
||||
resp = app.patch_json('/api/booking/%s/' % booking.pk, params={'user_check_type': 'foobar'}, status=400)
|
||||
resp = app.patch_json(
|
||||
'/api/booking/%s/' % booking.pk, params={'user_absence_reason': 'foobar'}, status=400
|
||||
)
|
||||
assert resp.json['err'] == 4
|
||||
assert resp.json['err_desc'] == 'invalid payload'
|
||||
|
||||
# set reason
|
||||
# set check_type
|
||||
agenda.check_type_group = group
|
||||
agenda.save()
|
||||
# it works with label
|
||||
app.patch_json('/api/booking/%s/' % booking.pk, params={'user_check_type': 'Foo bar'})
|
||||
app.patch_json('/api/booking/%s/' % booking.pk, params={'user_absence_reason': 'Foo bar'})
|
||||
booking.refresh_from_db()
|
||||
assert booking.user_was_present is True # not changed
|
||||
assert booking.user_check_type == 'Foo bar'
|
||||
|
||||
# reset
|
||||
app.patch_json('/api/booking/%s/' % booking.pk, params={'user_check_type': ''})
|
||||
app.patch_json('/api/booking/%s/' % booking.pk, params={'user_absence_reason': ''})
|
||||
booking.refresh_from_db()
|
||||
assert booking.user_was_present is True # not changed
|
||||
assert booking.user_check_type == ''
|
||||
app.patch_json('/api/booking/%s/' % booking.pk, params={'user_check_type': None})
|
||||
app.patch_json('/api/booking/%s/' % booking.pk, params={'user_absence_reason': None})
|
||||
booking.refresh_from_db()
|
||||
assert booking.user_was_present is True # not changed
|
||||
assert booking.user_check_type == ''
|
||||
|
@ -564,7 +568,7 @@ def test_booking_patch_api_absence_reason(app, user):
|
|||
other_booking = Booking.objects.create(event=event)
|
||||
|
||||
# it works also with slug
|
||||
app.patch_json('/api/booking/%s/' % booking.pk, params={'user_check_type': reason.slug})
|
||||
app.patch_json('/api/booking/%s/' % booking.pk, params={'user_absence_reason': check_type.slug})
|
||||
booking.refresh_from_db()
|
||||
assert booking.user_was_present is True # not changed
|
||||
assert booking.user_check_type == 'Foo bar'
|
||||
|
@ -583,14 +587,14 @@ def test_booking_patch_api_absence_reason(app, user):
|
|||
# mark the event as checked
|
||||
event.checked = True
|
||||
event.save()
|
||||
resp = app.patch_json('/api/booking/%s/' % booking.pk, params={'user_check_type': reason.slug})
|
||||
resp = app.patch_json('/api/booking/%s/' % booking.pk, params={'user_absence_reason': check_type.slug})
|
||||
assert resp.json['err'] == 0
|
||||
|
||||
# now disable check update
|
||||
agenda.disable_check_update = True
|
||||
agenda.save()
|
||||
resp = app.patch_json(
|
||||
'/api/booking/%s/' % booking.pk, params={'user_check_type': reason.slug}, status=400
|
||||
'/api/booking/%s/' % booking.pk, params={'user_absence_reason': check_type.slug}, status=400
|
||||
)
|
||||
assert resp.json['err'] == 5
|
||||
assert resp.json['err_desc'] == 'event is marked as checked'
|
||||
|
|
Loading…
Reference in New Issue