Verified Commit ee539787 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Fix registration editing of non-members by organiser

parent 47d6ab67
......@@ -336,7 +336,7 @@ class RegistrationSerializer(serializers.ModelSerializer):
if instance:
self.information_fields = services.registration_fields(
kwargs['context']['request'],
instance.member, instance.event)
registration=instance)
except RegistrationError:
pass
......
......@@ -205,12 +205,11 @@ class RegistrationViewSet(GenericViewSet, RetrieveModelMixin,
self.request.member,
serializer.validated_data)
services.update_registration(registration.member,
registration.event,
serializer.field_values())
services.update_registration(registration=registration,
field_values=serializer.field_values())
serializer.information_fields = services.registration_fields(
serializer.context['request'],
registration.member, registration.event)
registration=registration)
def destroy(self, request, pk=None, **kwargs):
registration = self.get_object()
......
......@@ -154,27 +154,32 @@ def cancel_registration(request, member, 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
:param request: http request
:param member: the user
:param event: the event
:param registration: the registration
: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:
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
for field_id, field_value in field_values:
......@@ -194,22 +199,31 @@ def update_registration(member, event, field_values):
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
:param member: the user
:param event: the event
:param member: the user (optional if registration provided)
:param event: the event (optional if registration provided)
:param registration: the registration (optional if member & event provided)
:return: the fields
"""
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
if registration is None:
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
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
is_organiser(request.member, event))
......
......@@ -333,10 +333,14 @@ class RegistrationApiTest(TestCase):
self.assertEqual(field3.get_value_for(registration), 'no text')
def test_registration_organiser(self):
reg0 = Registration.objects.create(event=self.event, member=self.member)
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',
......@@ -356,7 +360,7 @@ class RegistrationApiTest(TestCase):
'payment': 'card_payment'
}, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['member'], '')
self.assertEqual(response.data['member'], None)
self.assertEqual(response.data['name'], 'Test 1')
reg1.refresh_from_db()
self.assertEqual(reg1.payment.type, 'card_payment')
......@@ -369,7 +373,7 @@ class RegistrationApiTest(TestCase):
'payment': 'cash_payment'
}, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['member'], '')
self.assertEqual(response.data['member'], None)
self.assertEqual(response.data['name'], 'Test 2')
reg2.refresh_from_db()
self.assertEqual(reg2.payment.type, 'cash_payment')
......
......@@ -285,14 +285,16 @@ class ServicesTest(TestCase):
}
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(
event=self.event,
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
......@@ -320,7 +322,8 @@ class ServicesTest(TestCase):
('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(field2.get_value_for(registration), False)
......@@ -332,7 +335,8 @@ class ServicesTest(TestCase):
('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(field2.get_value_for(registration), True)
......@@ -356,13 +360,16 @@ class ServicesTest(TestCase):
with self.assertRaises(RegistrationError):
services.registration_fields(mock_request, self.member, self.event)
Registration.objects.create(
registration = Registration.objects.create(
event=self.event,
member=self.member,
)
with self.assertRaises(RegistrationError):
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
......@@ -395,34 +402,39 @@ class ServicesTest(TestCase):
# set order
self.event.set_registrationinformationfield_order([1, 2, 3])
fields = services.registration_fields(
mock_request, self.member, self.event)
self.assertEqual(fields['info_field_1'], {
'type': 'integer',
'label': '1',
'description': None,
'value': None,
'required': False
})
self.assertEqual(fields['info_field_2'], {
'type': 'boolean',
'label': '2',
'description': None,
'value': None,
'required': True
})
self.assertEqual(fields['info_field_3'], {
'type': 'text',
'label': '3',
'description': None,
'value': None,
'required': False
})
self.assertEqual(len(fields), 3)
# Test that the ordering is correct
labels = [field['label'] for field in fields.values()]
self.assertEqual(labels, sorted(labels))
fields_list = [
services.registration_fields(mock_request,
self.member, self.event),
services.registration_fields(mock_request,
registration=registration),
]
for fields in fields_list:
self.assertEqual(fields['info_field_1'], {
'type': 'integer',
'label': '1',
'description': None,
'value': None,
'required': False
})
self.assertEqual(fields['info_field_2'], {
'type': 'boolean',
'label': '2',
'description': None,
'value': None,
'required': True
})
self.assertEqual(fields['info_field_3'], {
'type': 'text',
'label': '3',
'description': None,
'value': None,
'required': False
})
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):
def form_valid(self, form):
values = form.field_values()
try:
services.update_registration(self.request.member, self.event,
values)
services.update_registration(self.request.member,
self.event,
field_values=values)
messages.success(self.request,
_("Registration successfully saved."))
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