...
  View open merge request
......@@ -384,7 +384,8 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
self.start_reminder = start_reminder
if self.registration_required:
self.start_reminder.users.set(
self.participants.values_list('member', flat=True))
self.participants.filter(member__isnull=False)
.values_list('member', flat=True))
else:
self.start_reminder.users.set(Member.current_members.all())
elif start_reminder.pk is not None:
......
......@@ -27,12 +27,13 @@ def is_user_registered(member, event):
date_cancelled=None).count() > 0
def event_permissions(member, event):
def event_permissions(member, event, name=None):
"""
Returns a dictionary with the available event permissions of the user
:param member: the user
:param event: the event
:param name: the name of a non member registration
:return: the permission dictionary
"""
perms = {
......@@ -40,12 +41,13 @@ def event_permissions(member, event):
"cancel_registration": False,
"update_registration": False,
}
if member and member.is_authenticated:
if member and member.is_authenticated or name:
registration = None
try:
registration = Registration.objects.get(
event=event,
member=member
member=member,
name=name
)
except Registration.DoesNotExist:
pass
......@@ -53,17 +55,17 @@ def event_permissions(member, event):
perms["create_registration"] = (
(registration is None or registration.date_cancelled is not None)
and event.registration_allowed and
member.can_attend_events)
(name or member.can_attend_events))
perms["cancel_registration"] = (
registration is not None and
registration.date_cancelled is None and
event.cancellation_allowed)
(event.cancellation_allowed or name))
perms["update_registration"] = (
registration is not None and
registration.date_cancelled is None and
event.has_fields() and
event.registration_allowed and
member.can_attend_events)
(name or member.can_attend_events))
return perms
......@@ -160,6 +162,7 @@ def cancel_registration(member, event):
raise RegistrationError(_("You are not registered for this event."))
<<<<<<< HEAD
def pay_with_tpay(member, event):
"""
Add a Thalia Pay payment to an event registration
......@@ -203,14 +206,19 @@ def pay_with_tpay(member, event):
_("You do not have Thalia Pay enabled."))
def update_registration(member=None, event=None,
def update_registration(member=None, event=None, name=None,
registration=None, field_values=None):
=======
def update_registration(member=None, event=None, registration=None,
field_values=None, name=None):
>>>>>>> Add tests and fix tests for events guest registrations
"""
Updates a user registration of an event
:param request: http request
:param member: the user
:param event: the event
:param name: the name of a registration not associated with a user
:param registration: the registration
:param field_values: values for the information fields
"""
......@@ -218,7 +226,8 @@ def update_registration(member=None, event=None,
try:
registration = Registration.objects.get(
event=event,
member=member
member=member,
name=name
)
except Registration.DoesNotExist as error:
raise RegistrationError(
......@@ -226,8 +235,9 @@ def update_registration(member=None, event=None,
else:
member = registration.member
event = registration.event
name = registration.name
if (not event_permissions(member, event)["update_registration"] or
if (not event_permissions(member, event, name)["update_registration"] or
not field_values):
return
......@@ -248,11 +258,14 @@ def update_registration(member=None, event=None,
field.set_value_for(registration, field_value)
def registration_fields(request, member=None, event=None, registration=None):
def registration_fields(request, member=None, event=None, registration=None,
name=None):
"""
Returns information about the registration fields of a registration
:param member: the user (optional if registration provided)
:param name: the name of a non member registration
(optional if registration provided)
:param event: the event (optional if registration provided)
:param registration: the registration (optional if member & event provided)
:return: the fields
......@@ -262,7 +275,8 @@ def registration_fields(request, member=None, event=None, registration=None):
try:
registration = Registration.objects.get(
event=event,
member=member
member=member,
name=name
)
except Registration.DoesNotExist as error:
raise RegistrationError(
......@@ -273,8 +287,9 @@ def registration_fields(request, member=None, event=None, registration=None):
else:
member = registration.member
event = registration.event
name = registration.name
perms = (event_permissions(member, event)["update_registration"] or
perms = (event_permissions(member, event, name)["update_registration"] or
is_organiser(request.member, event))
if perms and registration:
information_fields = registration.information_fields
......
......@@ -13,7 +13,7 @@ from events.admin import (
RegistrationInformationFieldInline,
EventAdmin
)
from events.models import Event, RegistrationInformationField
from events.models import Event, RegistrationInformationField, Registration
from members.models import Member
from utils.admin import DoNextTranslatedModelAdmin
......@@ -240,3 +240,22 @@ class EventAdminTest(TestCase):
self.event.max_participants = 2
self.assertEqual(self.admin.num_participants(self.event),
'0/2')
self.event.max_participants = None
Registration.objects.create(
event=self.event,
name='test_cancelled',
date=timezone.now() - timezone.timedelta(days=1),
date_cancelled=timezone.now() - timezone.timedelta(seconds=10)
)
Registration.objects.create(
event=self.event,
name='test'
)
self.assertEqual(self.admin.num_participants(self.event),
'1/∞')
self.event.max_participants = 2
self.assertEqual(self.admin.num_participants(self.event),
'1/2')
......@@ -274,7 +274,7 @@ class ServicesTest(TestCase):
services.cancel_registration(self.member, self.event)
@mock.patch('events.services.event_permissions')
def test_update_registration(self, perms_mock):
def test_update_registration_user(self, perms_mock):
self.event.registration_start = timezone.now() - timedelta(hours=2)
self.event.registration_end = timezone.now() + timedelta(hours=1)
......@@ -346,6 +346,79 @@ class ServicesTest(TestCase):
field2.delete()
field3.delete()
@mock.patch('events.services.event_permissions')
def test_update_registration_guest(self, perms_mock):
self.event.registration_start = timezone.now() - timedelta(hours=2)
self.event.registration_end = timezone.now() + timedelta(hours=1)
perms_mock.return_value = {
"create_registration": False,
"cancel_registration": False,
"update_registration": False,
}
with self.assertRaises(RegistrationError):
services.update_registration(self.member, self.event,
field_values=None)
registration = Registration.objects.create(
event=self.event,
name="test",
)
services.update_registration(event=self.event, name="test",
field_values=None)
perms_mock.return_value["update_registration"] = True
field1 = RegistrationInformationField.objects.create(
type=RegistrationInformationField.INTEGER_FIELD,
event=self.event,
required=False,
)
field2 = RegistrationInformationField.objects.create(
type=RegistrationInformationField.BOOLEAN_FIELD,
event=self.event,
required=False,
)
field3 = RegistrationInformationField.objects.create(
type=RegistrationInformationField.TEXT_FIELD,
event=self.event,
required=False,
)
fields = [
('info_field_{}'.format(field1.id), None),
('info_field_{}'.format(field2.id), None),
('info_field_{}'.format(field3.id), None),
]
services.update_registration(name="test", event=self.event,
field_values=fields)
self.assertEqual(field1.get_value_for(registration), 0)
self.assertEqual(field2.get_value_for(registration), False)
self.assertEqual(field3.get_value_for(registration), '')
fields = [
('info_field_{}'.format(field1.id), 2),
('info_field_{}'.format(field2.id), True),
('info_field_{}'.format(field3.id), 'text'),
]
services.update_registration(name="test", event=self.event,
field_values=fields)
self.assertEqual(field1.get_value_for(registration), 2)
self.assertEqual(field2.get_value_for(registration), True)
self.assertEqual(field3.get_value_for(registration), 'text')
field1.delete()
field2.delete()
field3.delete()
@mock.patch('events.services.event_permissions')
def test_registration_fields(self, perms_mock):
perms_mock.return_value = {
......