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):
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
if not registration:
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."))
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,14 +199,17 @@ 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
"""
if registration is None:
try:
registration = Registration.objects.get(
event=event,
......@@ -210,6 +218,12 @@ def registration_fields(request, member, event):
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))
......
......@@ -331,3 +331,50 @@ class RegistrationApiTest(TestCase):
self.assertEqual(field1.get_value_for(registration), True)
self.assertEqual(field2.get_value_for(registration), 1337)
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):
}
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,9 +402,14 @@ class ServicesTest(TestCase):
# set order
self.event.set_registrationinformationfield_order([1, 2, 3])
fields = services.registration_fields(
mock_request, self.member, self.event)
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',
......
......@@ -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