Commit b7673ca2 authored by Luko van der Maas's avatar Luko van der Maas
Browse files

Merge branch 'fix/registration-editing-organiser-non-member' into 'master'

Fix registration editing organiser non member

Closes #839

See merge request !1194
parents 5ad9dab2 ee539787
...@@ -336,7 +336,7 @@ class RegistrationSerializer(serializers.ModelSerializer): ...@@ -336,7 +336,7 @@ class RegistrationSerializer(serializers.ModelSerializer):
if instance: if instance:
self.information_fields = services.registration_fields( self.information_fields = services.registration_fields(
kwargs['context']['request'], kwargs['context']['request'],
instance.member, instance.event) registration=instance)
except RegistrationError: except RegistrationError:
pass pass
......
...@@ -205,12 +205,11 @@ class RegistrationViewSet(GenericViewSet, RetrieveModelMixin, ...@@ -205,12 +205,11 @@ class RegistrationViewSet(GenericViewSet, RetrieveModelMixin,
self.request.member, self.request.member,
serializer.validated_data) serializer.validated_data)
services.update_registration(registration.member, services.update_registration(registration=registration,
registration.event, field_values=serializer.field_values())
serializer.field_values())
serializer.information_fields = services.registration_fields( serializer.information_fields = services.registration_fields(
serializer.context['request'], serializer.context['request'],
registration.member, registration.event) registration=registration)
def destroy(self, request, pk=None, **kwargs): def destroy(self, request, pk=None, **kwargs):
registration = self.get_object() registration = self.get_object()
......
...@@ -154,27 +154,32 @@ def cancel_registration(request, member, event): ...@@ -154,27 +154,32 @@ def cancel_registration(request, member, event):
raise RegistrationError(_("You are not registered for this event.")) raise RegistrationError(_("You are not registered for this event."))
def update_registration(member, event, field_values): def update_registration(member=None, event=None,
registration=None, field_values=None):
""" """
Updates a user registration of an event Updates a user registration of an event
:param request: http request
:param member: the user :param member: the user
:param event: the event :param event: the event
:param registration: the registration
:param field_values: values for the information fields :param field_values: values for the information fields
""" """
registration = None
try:
registration = Registration.objects.get(
event=event,
member=member
)
except Registration.DoesNotExist:
pass
if not registration: if not registration:
raise RegistrationError(_("You are not registered for this event.")) try:
registration = Registration.objects.get(
event=event,
member=member
)
except Registration.DoesNotExist as error:
raise RegistrationError(
_("You are not registered for this event.")) from error
else:
member = registration.member
event = registration.event
if not event_permissions(member, event)["update_registration"]: if (not event_permissions(member, event)["update_registration"] or
not field_values):
return return
for field_id, field_value in field_values: for field_id, field_value in field_values:
...@@ -194,22 +199,31 @@ def update_registration(member, event, field_values): ...@@ -194,22 +199,31 @@ def update_registration(member, event, field_values):
field.set_value_for(registration, field_value) field.set_value_for(registration, field_value)
def registration_fields(request, member, event): def registration_fields(request, member=None, event=None, registration=None):
""" """
Returns information about the registration fields of a registration Returns information about the registration fields of a registration
:param member: the user :param member: the user (optional if registration provided)
:param event: the event :param event: the event (optional if registration provided)
:param registration: the registration (optional if member & event provided)
:return: the fields :return: the fields
""" """
try:
registration = Registration.objects.get( if registration is None:
event=event, try:
member=member registration = Registration.objects.get(
) event=event,
except Registration.DoesNotExist as error: member=member
raise RegistrationError( )
_("You are not registered for this event.")) from error except Registration.DoesNotExist as error:
raise RegistrationError(
_("You are not registered for this event.")) from error
except Registration.MultipleObjectsReturned as error:
raise RegistrationError(
_("Unable to find the right registration.")) from error
else:
member = registration.member
event = registration.event
perms = (event_permissions(member, event)["update_registration"] or perms = (event_permissions(member, event)["update_registration"] or
is_organiser(request.member, event)) is_organiser(request.member, event))
......
...@@ -331,3 +331,50 @@ class RegistrationApiTest(TestCase): ...@@ -331,3 +331,50 @@ class RegistrationApiTest(TestCase):
self.assertEqual(field1.get_value_for(registration), True) self.assertEqual(field1.get_value_for(registration), True)
self.assertEqual(field2.get_value_for(registration), 1337) self.assertEqual(field2.get_value_for(registration), 1337)
self.assertEqual(field3.get_value_for(registration), 'no text') self.assertEqual(field3.get_value_for(registration), 'no text')
def test_registration_organiser(self):
reg0 = Registration.objects.create(event=self.event,
member=self.member)
reg1 = Registration.objects.create(event=self.event, name="Test 1")
reg2 = Registration.objects.create(event=self.event, name="Test 2")
self.member.is_superuser = True
self.member.save()
response = self.client.patch(
'/api/v1/registrations/{}/'.format(reg0.pk), {
'csrf': 'random',
'present': True,
'payment': 'cash_payment'
}, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['member'], self.member.pk)
reg0.refresh_from_db()
self.assertIsNotNone(reg0.payment_id)
self.assertTrue(reg0.present)
response = self.client.patch(
'/api/v1/registrations/{}/'.format(reg1.pk), {
'csrf': 'random',
'present': True,
'payment': 'card_payment'
}, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['member'], None)
self.assertEqual(response.data['name'], 'Test 1')
reg1.refresh_from_db()
self.assertEqual(reg1.payment.type, 'card_payment')
self.assertTrue(reg1.present)
response = self.client.patch(
'/api/v1/registrations/{}/'.format(reg2.pk), {
'csrf': 'random',
'present': False,
'payment': 'cash_payment'
}, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['member'], None)
self.assertEqual(response.data['name'], 'Test 2')
reg2.refresh_from_db()
self.assertEqual(reg2.payment.type, 'cash_payment')
self.assertFalse(reg2.present)
...@@ -285,14 +285,16 @@ class ServicesTest(TestCase): ...@@ -285,14 +285,16 @@ class ServicesTest(TestCase):
} }
with self.assertRaises(RegistrationError): with self.assertRaises(RegistrationError):
services.update_registration(self.member, self.event, None) services.update_registration(self.member, self.event,
field_values=None)
registration = Registration.objects.create( registration = Registration.objects.create(
event=self.event, event=self.event,
member=self.member, member=self.member,
) )
services.update_registration(self.member, self.event, None) services.update_registration(self.member, self.event,
field_values=None)
perms_mock.return_value["update_registration"] = True perms_mock.return_value["update_registration"] = True
...@@ -320,7 +322,8 @@ class ServicesTest(TestCase): ...@@ -320,7 +322,8 @@ class ServicesTest(TestCase):
('info_field_{}'.format(field3.id), None), ('info_field_{}'.format(field3.id), None),
] ]
services.update_registration(self.member, self.event, fields) services.update_registration(self.member, self.event,
field_values=fields)
self.assertEqual(field1.get_value_for(registration), 0) self.assertEqual(field1.get_value_for(registration), 0)
self.assertEqual(field2.get_value_for(registration), False) self.assertEqual(field2.get_value_for(registration), False)
...@@ -332,7 +335,8 @@ class ServicesTest(TestCase): ...@@ -332,7 +335,8 @@ class ServicesTest(TestCase):
('info_field_{}'.format(field3.id), 'text'), ('info_field_{}'.format(field3.id), 'text'),
] ]
services.update_registration(self.member, self.event, fields) services.update_registration(self.member, self.event,
field_values=fields)
self.assertEqual(field1.get_value_for(registration), 2) self.assertEqual(field1.get_value_for(registration), 2)
self.assertEqual(field2.get_value_for(registration), True) self.assertEqual(field2.get_value_for(registration), True)
...@@ -356,13 +360,16 @@ class ServicesTest(TestCase): ...@@ -356,13 +360,16 @@ class ServicesTest(TestCase):
with self.assertRaises(RegistrationError): with self.assertRaises(RegistrationError):
services.registration_fields(mock_request, self.member, self.event) services.registration_fields(mock_request, self.member, self.event)
Registration.objects.create( registration = Registration.objects.create(
event=self.event, event=self.event,
member=self.member, member=self.member,
) )
with self.assertRaises(RegistrationError): with self.assertRaises(RegistrationError):
services.registration_fields(mock_request, self.member, self.event) services.registration_fields(mock_request, self.member, self.event)
with self.assertRaises(RegistrationError):
services.registration_fields(mock_request,
registration=registration)
perms_mock.return_value["update_registration"] = True perms_mock.return_value["update_registration"] = True
...@@ -395,34 +402,39 @@ class ServicesTest(TestCase): ...@@ -395,34 +402,39 @@ class ServicesTest(TestCase):
# set order # set order
self.event.set_registrationinformationfield_order([1, 2, 3]) self.event.set_registrationinformationfield_order([1, 2, 3])
fields = services.registration_fields( fields_list = [
mock_request, self.member, self.event) services.registration_fields(mock_request,
self.member, self.event),
self.assertEqual(fields['info_field_1'], { services.registration_fields(mock_request,
'type': 'integer', registration=registration),
'label': '1', ]
'description': None,
'value': None, for fields in fields_list:
'required': False self.assertEqual(fields['info_field_1'], {
}) 'type': 'integer',
'label': '1',
self.assertEqual(fields['info_field_2'], { 'description': None,
'type': 'boolean', 'value': None,
'label': '2', 'required': False
'description': None, })
'value': None,
'required': True self.assertEqual(fields['info_field_2'], {
}) 'type': 'boolean',
'label': '2',
self.assertEqual(fields['info_field_3'], { 'description': None,
'type': 'text', 'value': None,
'label': '3', 'required': True
'description': None, })
'value': None,
'required': False self.assertEqual(fields['info_field_3'], {
}) 'type': 'text',
'label': '3',
self.assertEqual(len(fields), 3) 'description': None,
# Test that the ordering is correct 'value': None,
labels = [field['label'] for field in fields.values()] 'required': False
self.assertEqual(labels, sorted(labels)) })
self.assertEqual(len(fields), 3)
# Test that the ordering is correct
labels = [field['label'] for field in fields.values()]
self.assertEqual(labels, sorted(labels))
...@@ -155,8 +155,9 @@ class RegistrationView(FormView): ...@@ -155,8 +155,9 @@ class RegistrationView(FormView):
def form_valid(self, form): def form_valid(self, form):
values = form.field_values() values = form.field_values()
try: try:
services.update_registration(self.request.member, self.event, services.update_registration(self.request.member,
values) self.event,
field_values=values)
messages.success(self.request, messages.success(self.request,
_("Registration successfully saved.")) _("Registration successfully saved."))
return redirect(self.event) return redirect(self.event)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment