From 31fdf8721bc4bc8747ae14a49feb01c3ceae66ee Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Tue, 7 Apr 2020 15:16:58 +0200 Subject: [PATCH] API: Fix selecting checkin question answers by option identifier --- src/pretix/base/models/items.py | 9 ++++++--- src/tests/api/test_checkin.py | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/pretix/base/models/items.py b/src/pretix/base/models/items.py index b463c94332..bc7b63678c 100644 --- a/src/pretix/base/models/items.py +++ b/src/pretix/base/models/items.py @@ -1116,10 +1116,13 @@ class Question(LoggedModel): return None if self.type == Question.TYPE_CHOICE: - try: - return self.options.get(Q(pk=answer) | Q(identifier=answer)) - except: + q = Q(identifier=answer) + if answer.isdigit(): + q |= Q(pk=answer) + o = self.options.filter(q).first() + if not o: raise ValidationError(_('Invalid option selected.')) + return o elif self.type == Question.TYPE_CHOICE_MULTIPLE: if isinstance(answer, str): l_ = list(self.options.filter( diff --git a/src/tests/api/test_checkin.py b/src/tests/api/test_checkin.py index b619dd4378..546a43bb29 100644 --- a/src/tests/api/test_checkin.py +++ b/src/tests/api/test_checkin.py @@ -749,6 +749,29 @@ def test_question_choice(token_client, organizer, clist, event, order, question) assert list(order.positions.first().answers.get(question=question[0]).options.all()) == [question[1]] +@pytest.mark.django_db +def test_question_choice_identifier(token_client, organizer, clist, event, order, question): + with scopes_disabled(): + p = order.positions.first() + resp = token_client.post('/api/v1/organizers/{}/events/{}/checkinlists/{}/positions/{}/redeem/'.format( + organizer.slug, event.slug, clist.pk, p.pk + ), {}, format='json') + assert resp.status_code == 400 + assert resp.data['status'] == 'incomplete' + with scopes_disabled(): + assert resp.data['questions'] == [QuestionSerializer(question[0]).data] + + resp = token_client.post('/api/v1/organizers/{}/events/{}/checkinlists/{}/positions/{}/redeem/'.format( + organizer.slug, event.slug, clist.pk, p.pk + ), {'answers': {question[0].pk: str(question[1].identifier)}}, format='json') + print(resp.data) + assert resp.status_code == 201 + assert resp.data['status'] == 'ok' + with scopes_disabled(): + assert order.positions.first().answers.get(question=question[0]).answer == 'M' + assert list(order.positions.first().answers.get(question=question[0]).options.all()) == [question[1]] + + @pytest.mark.django_db def test_question_invalid(token_client, organizer, clist, event, order, question): with scopes_disabled():