summaryrefslogtreecommitdiff
path: root/pgweb/events/forms.py
blob: 59d740d42c8b48745a6983c2df17a56875b03484 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
from django import forms
from django.forms import ValidationError

from pgweb.core.models import Organisation
from .models import Event


class EventForm(forms.ModelForm):
    form_intro = 'Before submitting an event, please read the <a href="/about/policies/news-and-events/">current policy</a> for News and Events'

    toggle_fields = [
        {
            'name': 'isonline',
            'invert': True,
            'fields': ['city', 'state', 'country', ]
        },
    ]

    def __init__(self, *args, **kwargs):
        super(EventForm, self).__init__(*args, **kwargs)
        self.fields['startdate'].help_text = 'Use format YYYY-MM-DD'
        self.fields['enddate'].help_text = 'Use format YYYY-MM-DD'

    def filter_by_user(self, user):
        self.fields['org'].queryset = Organisation.objects.filter(managers=user, approved=True)

    def clean(self):
        cleaned_data = super(EventForm, self).clean()
        if not cleaned_data.get('isonline'):
            # Non online events require city and country
            # (we don't require state, since many countries have no such thing)
            if not cleaned_data.get('city'):
                self._errors['city'] = self.error_class(['City must be specified for non-online events'])
                del cleaned_data['city']
            if not cleaned_data.get('country'):
                self._errors['country'] = self.error_class(['Country must be specified for non-online events'])
                del cleaned_data['country']
        return cleaned_data

    def clean_startdate(self):
        if self.instance.pk and self.instance.approved:
            if self.cleaned_data['startdate'] != self.instance.startdate:
                raise ValidationError("You cannot change the dates on events that have been approved")
        return self.cleaned_data['startdate']

    def clean_enddate(self):
        if self.instance.pk and self.instance.approved:
            if self.cleaned_data['enddate'] != self.instance.enddate:
                raise ValidationError("You cannot change the dates on events that have been approved")
        if 'startdate' in self.cleaned_data and self.cleaned_data['enddate'] < self.cleaned_data['startdate']:
            raise ValidationError("End date cannot be before start date!")
        return self.cleaned_data['enddate']

    class Meta:
        model = Event
        exclude = ('submitter', 'approved', 'description_for_badged')