Update syntax for relative imports
[pgweb/local.git] / pgweb / events / forms.py
blob45a506892ec8abc35485016ae970d53728149202
1 from django import forms
2 from django.forms import ValidationError
4 from pgweb.core.models import Organisation
5 from .models import Event
8 class EventForm(forms.ModelForm):
9 toggle_fields = [
11 'name': 'isonline',
12 'invert': True,
13 'fields': ['city', 'state', 'country', ]
17 def __init__(self, *args, **kwargs):
18 super(EventForm, self).__init__(*args, **kwargs)
20 def filter_by_user(self, user):
21 self.fields['org'].queryset = Organisation.objects.filter(managers=user, approved=True)
23 def clean(self):
24 cleaned_data = super(EventForm, self).clean()
25 if not cleaned_data.get('isonline'):
26 # Non online events require city and country
27 # (we don't require state, since many countries have no such thing)
28 if not cleaned_data.get('city'):
29 self._errors['city'] = self.error_class(['City must be specified for non-online events'])
30 del cleaned_data['city']
31 if not cleaned_data.get('country'):
32 self._errors['country'] = self.error_class(['Country must be specified for non-online events'])
33 del cleaned_data['country']
34 return cleaned_data
36 def clean_startdate(self):
37 if self.instance.pk and self.instance.approved:
38 if self.cleaned_data['startdate'] != self.instance.startdate:
39 raise ValidationError("You cannot change the dates on events that have been approved")
40 return self.cleaned_data['startdate']
42 def clean_enddate(self):
43 if self.instance.pk and self.instance.approved:
44 if self.cleaned_data['enddate'] != self.instance.enddate:
45 raise ValidationError("You cannot change the dates on events that have been approved")
46 if 'startdate' in self.cleaned_data and self.cleaned_data['enddate'] < self.cleaned_data['startdate']:
47 raise ValidationError("End date cannot be before start date!")
48 return self.cleaned_data['enddate']
50 class Meta:
51 model = Event
52 exclude = ('submitter', 'approved', 'description_for_badged')