services.py 5.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
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


def is_user_registered(event, user):
    if not event.registration_required:
        return None

    return event.registrations.filter(
14
        member=user,
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        date_cancelled=None).count() > 0


def event_permissions(user, event):
    perms = {
        "create_registration": False,
        "cancel_registration": False,
        "update_registration": False,
    }
    if user.is_authenticated and user.member.can_attend_events:
        registration = None
        try:
            registration = Registration.objects.get(
                event=event,
29
                member=user
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
            )
        except Registration.DoesNotExist:
            pass

        perms["create_registration"] = (
            (registration is None or registration.date_cancelled is not None)
            and event.registration_allowed)
        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
            registration.date_cancelled is None and event.has_fields() and
            event.registration_allowed)

    return perms


def is_organiser(user, event):
    if user.is_superuser or user.has_perm("events.override_organiser"):
        return True

    if event and user.has_perm('events.change_event'):
        committees = 0
        if event is not None:
            committees = user.member.get_committees().filter(
                pk=event.organiser.pk).count()
        return committees != 0

    return False


def create_registration(user, event):
    if event_permissions(user, event)["create_registration"]:
        registration = None
        try:
            registration = Registration.objects.get(
                event=event,
69
                member=user
70
71
72
73
74
75
76
            )
        except Registration.DoesNotExist:
            pass

        if registration is None:
            return Registration.objects.create(
                event=event,
77
                member=user
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
            )
        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
    elif event_permissions(user, event)["cancel_registration"]:
        raise RegistrationError(_("You were already registered."))
    else:
        raise RegistrationError(_("You may not register."))


def cancel_registration(request, user, event):
    registration = None
    try:
        registration = Registration.objects.get(
            event=event,
101
            member=user
102
103
104
105
106
107
108
109
        )
    except Registration.DoesNotExist:
        pass

    if event_permissions(user, event)["cancel_registration"] and registration:
        if registration.queue_position == 0:
            emails.notify_first_waiting(request, event)

110
111
112
113
            if (event.send_cancel_email and
                    event.after_cancel_deadline):
                emails.notify_organiser(event, registration)

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        # 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."))


def update_registration(user, event, field_values):
    registration = None
    try:
        registration = Registration.objects.get(
            event=event,
129
            member=user
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
        )
    except Registration.DoesNotExist:
        pass

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

    if not event_permissions(user, event)["update_registration"]:
        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)


def registration_fields(user, event):
    registration = None
    try:
        registration = Registration.objects.get(
            event=event,
162
            member=user
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
        )
    except Registration.DoesNotExist:
        pass

    if event_permissions(user, event)["update_registration"] and registration:
        information_fields = registration.information_fields
        fields = {}

        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:
        raise RegistrationError(_("You are not registered for this event."))