forms.py 3.53 KB
Newer Older
1
from django import forms
2
from django.urls import reverse
3
from django.utils.translation import ugettext_lazy as _
4

5
6
7
8
9
10
11
12
13
from .models import RegistrationInformationField, Event, Registration
from .widgets import FieldsWidget


class RegistrationAdminForm(forms.ModelForm):
    """
    Custom admin form to add a link to the registration information
    fields admin
    """
Thijs de Jong's avatar
Thijs de Jong committed
14
    fields = forms.URLField(widget=FieldsWidget, required=False)
15
16
17

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
Luko van der Maas's avatar
Luko van der Maas committed
18
19
20
21
22
23
24
25
26
        try:
            if self.instance.event.has_fields():
                self.fields['fields'].initial = (
                    reverse('admin:events_registration_fields',
                            args=[self.instance.pk]))
            else:
                self.fields['fields'].widget = self.fields[
                    'fields'].hidden_widget()
        except Event.DoesNotExist:
27
28
29
30
31
32
            self.fields['fields'].widget = self.fields[
                'fields'].hidden_widget()

    class Meta:
        fields = '__all__'
        model = Registration
33
34
35


class RegistrationInformationFieldForm(forms.ModelForm):
36
37
38
39
    """
    Custom form for the registration information fields
    that adds an order field
    """
40
41
    order = forms.IntegerField(label=_('order'), initial=0)

42
43
44
45
46
47
48
49
50
51
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        try:
            event = self.instance.event
            order = event.get_registrationinformationfield_order()
            order_value = list(order).index(self.instance.pk)
            self.fields['order'].initial = order_value
        except Event.DoesNotExist:
            pass

52
53
54
55
56
57
58
59
60
    class Meta:
        fields = '__all__'
        model = RegistrationInformationField
        widgets = {
            'type': forms.Select,
        }


class FieldsForm(forms.Form):
61
    """Form that outputs the correct widgets for the information fields"""
62
    def __init__(self, *args, **kwargs):
63
        self.information_fields = kwargs.pop('fields')
64
65
        super(FieldsForm, self).__init__(*args, **kwargs)

66
67
        for key, field in self.information_fields.items():
            field_type = field["type"]
68

69
            if field_type == RegistrationInformationField.BOOLEAN_FIELD:
70
71
72
                self.fields[key] = forms.BooleanField(
                    required=False
                )
73
74
75
76
77
78
79
80
            elif field_type == RegistrationInformationField.INTEGER_FIELD:
                self.fields[key] = forms.IntegerField(
                    required=field["required"]
                )
            elif field_type == RegistrationInformationField.TEXT_FIELD:
                self.fields[key] = forms.CharField(
                    required=field["required"]
                )
81

82
83
84
            self.fields[key].label = field["label"]
            self.fields[key].help_text = field["description"]
            self.fields[key].initial = field['value']
85
86

    def field_values(self):
87
88
        for key, field in self.information_fields.items():
            yield (key, self.cleaned_data[key])
89
90
91
92


class EventMessageForm(forms.Form):
    """Form that outputs a widget to get info to send a push notification"""
93
94
95
96
    title_en = forms.CharField(label=_('Title (EN)'), max_length=150)
    title_nl = forms.CharField(label=_('Title (NL)'), max_length=150)
    body_en = forms.CharField(label=_('Body (EN)'), widget=forms.Textarea)
    body_nl = forms.CharField(label=_('Body (NL)'), widget=forms.Textarea)
97
98
99
100
101
    url = forms.CharField(
        max_length=256,
        required=False,
        help_text=_('The notification opens to the event by default.')
    )