-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathforms.py
72 lines (55 loc) · 2.35 KB
/
forms.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from captcha.fields import ReCaptchaField
from django import forms
from django.conf import settings
from django.core.validators import validate_email
from django.db import transaction
from django.utils.translation import gettext_lazy as _
from .models import Event
class BetterReCaptchaField(ReCaptchaField):
"""A ReCaptchaField that always works in DEBUG mode"""
def clean(self, values):
if settings.DEBUG:
return values[0]
return super().clean(values)
class AddOrganizerForm(forms.Form):
"""
Custom form for adding new organizers to an existing event.
If user of given email already exists, they're added to the event and
receive e-mail notification about it.
If user is new, they're created (randomly generated password), invited
to Slack and receive e-mail notification with instructions to login
(including password).
"""
event = forms.ModelChoiceField(queryset=Event.objects.all())
name = forms.CharField(label=_("Organizer's first and last name"))
email = forms.CharField(label=_("E-mail address"), validators=[validate_email])
def __init__(self, *args, **kwargs):
event_choices = kwargs.pop("event_choices", None)
super().__init__(*args, **kwargs)
if event_choices is not None:
self.fields["event"].queryset = event_choices
def save(self, *args, **kwargs):
assert self.is_valid()
self._errors = []
email = self.cleaned_data["email"]
event = self.cleaned_data["event"]
first_name, _, last_name = self.cleaned_data["name"].partition(" ")
user = event.add_organizer(email, first_name, last_name)
return user
class EventChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return f"{obj.city}, {obj.country}"
class EventForm(forms.ModelForm):
class Meta:
model = Event
fields = ["city", "country", "date", "email", "latlng", "name", "page_title", "page_url"]
@transaction.atomic
def save(self, commit=True):
"""Save the event and create default content in case of new instances"""
created = not self.instance.pk
instance = super().save(commit=commit)
if commit and created:
# create default content
instance.add_default_content()
instance.add_default_menu()
return instance