services.py 7.31 KB
Newer Older
1
2
from collections import OrderedDict

3
4
5
6
7
8
9
10
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _, get_language

from events import emails
from events.exceptions import RegistrationError
from events.models import Registration, RegistrationInformationField


11
def is_user_registered(member, event):
12
13
14
15
16
17
    """
    Returns if the user is registered for the specified event
    :param member: the user
    :param event: the event
    :return: None if registration is not required or no member else True/False
    """
18
    if not event.registration_required or not member.is_authenticated:
19
20
21
        return None

    return event.registrations.filter(
22
        member=member,
23
24
25
        date_cancelled=None).count() > 0


26
def event_permissions(member, event):
27
28
29
30
31
32
    """
    Returns a dictionary with the available event permissions of the user
    :param member: the user
    :param event: the event
    :return: the permission dictionary
    """
33
34
35
36
37
    perms = {
        "create_registration": False,
        "cancel_registration": False,
        "update_registration": False,
    }
38
    if member and member.is_authenticated:
39
40
41
42
        registration = None
        try:
            registration = Registration.objects.get(
                event=event,
43
                member=member
44
45
46
47
48
49
            )
        except Registration.DoesNotExist:
            pass

        perms["create_registration"] = (
            (registration is None or registration.date_cancelled is not None)
50
51
            and event.registration_allowed and
            member.can_attend_events)
52
53
54
55
56
57
        perms["cancel_registration"] = (
            registration is not None and
            registration.date_cancelled is None and
            event.cancellation_allowed)
        perms["update_registration"] = (
            registration is not None and
58
59
60
61
            registration.date_cancelled is None and
            event.has_fields() and
            event.registration_allowed and
            member.can_attend_events)
62
63
64
65

    return perms


66
def is_organiser(member, event):
67
68
69
    if member and member.is_authenticated:
        if member.is_superuser or member.has_perm("events.override_organiser"):
            return True
70

71
        if event and member.has_perm('events.change_event'):
72
73
            return member.get_committees().filter(
                    pk=event.organiser.pk).count() != 0
74
75
76
77

    return False


78
def create_registration(member, event):
79
80
81
82
83
84
    """
    Creates a new user registration for an event
    :param member: the user
    :param event: the event
    :return: returns the registration if successful
    """
85
    if event_permissions(member, event)["create_registration"]:
86
87
88
89
        registration = None
        try:
            registration = Registration.objects.get(
                event=event,
90
                member=member
91
92
93
94
95
96
97
            )
        except Registration.DoesNotExist:
            pass

        if registration is None:
            return Registration.objects.create(
                event=event,
98
                member=member
99
100
101
102
103
104
105
106
107
108
109
110
            )
        elif registration.date_cancelled is not None:
            if registration.is_late_cancellation():
                raise RegistrationError(_("You cannot re-register anymore "
                                          "since you've cancelled after the "
                                          "deadline."))
            else:
                registration.date = timezone.now()
                registration.date_cancelled = None
                registration.save()

        return registration
111
    elif event_permissions(member, event)["cancel_registration"]:
112
113
114
115
116
        raise RegistrationError(_("You were already registered."))
    else:
        raise RegistrationError(_("You may not register."))


117
def cancel_registration(request, member, event):
118
119
120
121
122
123
    """
    Cancel a user registration for an event
    :param request: the request object
    :param member: the user
    :param event: the event
    """
124
125
126
127
    registration = None
    try:
        registration = Registration.objects.get(
            event=event,
128
            member=member
129
130
131
132
        )
    except Registration.DoesNotExist:
        pass

133
134
    if (event_permissions(member, event)["cancel_registration"] and
            registration):
135
136
137
        if registration.queue_position == 0:
            emails.notify_first_waiting(request, event)

138
139
140
141
            if (event.send_cancel_email and
                    event.after_cancel_deadline):
                emails.notify_organiser(event, registration)

142
143
144
145
146
147
148
149
150
151
        # Note that this doesn"t remove the values for the
        # information fields that the user entered upon registering.
        # But this is regarded as a feature, not a bug. Especially
        # since the values will still appear in the backend.
        registration.date_cancelled = timezone.now()
        registration.save()
    else:
        raise RegistrationError(_("You are not registered for this event."))


152
def update_registration(member, event, field_values):
153
154
155
156
157
158
    """
    Updates a user registration of an event
    :param member: the user
    :param event: the event
    :param field_values: values for the information fields
    """
159
160
161
162
    registration = None
    try:
        registration = Registration.objects.get(
            event=event,
163
            member=member
164
165
166
167
168
169
170
        )
    except Registration.DoesNotExist:
        pass

    if not registration:
        raise RegistrationError(_("You are not registered for this event."))

171
    if not event_permissions(member, event)["update_registration"]:
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
        return

    for field_id, field_value in field_values:
        field = RegistrationInformationField.objects.get(
            id=field_id.replace("info_field_", ""))

        if (field.type == RegistrationInformationField.INTEGER_FIELD
                and field_value is None):
            field_value = 0
        elif (field.type == RegistrationInformationField.BOOLEAN_FIELD
                and field_value is None):
            field_value = False
        elif (field.type == RegistrationInformationField.TEXT_FIELD
              and field_value is None):
            field_value = ''

        field.set_value_for(registration, field_value)


191
def registration_fields(member, event):
192
193
194
195
196
197
    """
    Returns information about the registration fields of a registration
    :param member: the user
    :param event: the event
    :return: the fields
    """
198
199
200
    try:
        registration = Registration.objects.get(
            event=event,
201
            member=member
202
        )
203
204
205
    except Registration.DoesNotExist as error:
        raise RegistrationError(
            _("You are not registered for this event.")) from error
206

207
208
    if (event_permissions(member, event)["update_registration"] and
            registration):
209
        information_fields = registration.information_fields
210
        fields = OrderedDict()
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

        for information_field in information_fields:
            field = information_field["field"]

            fields["info_field_{}".format(field.id)] = {
                "type": field.type,
                "label": getattr(field, "{}_{}".format(
                    "name",  get_language())),
                "description": getattr(field, "{}_{}".format(
                    "description", get_language())),
                "value": information_field["value"],
                "required": field.required
            }

        return fields
    else:
227
228
        raise RegistrationError(
            _("You are not allowed to update this registration."))