-
Notifications
You must be signed in to change notification settings - Fork 71
/
forms.py
100 lines (82 loc) · 3.07 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Esup-Pod forms handling."""
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from captcha.fields import CaptchaField
from .forms_utils import add_placeholder_and_asterisk
SUBJECT_CHOICES = getattr(
settings,
"SUBJECT_CHOICES",
(
("", "-----"),
("info", _("Request more information")),
("contribute", _("Learn more about how to contribute")),
("request_password", _("Password request for a video")),
("inappropriate_content", _("Report inappropriate content")),
("bug", _("Correction or bug report")),
("other", _("Other (please specify)")),
),
)
class DownloadFileForm(forms.Form):
"""Manage "Download File" form."""
filename = forms.FilePathField(
required=True,
path=settings.MEDIA_ROOT,
recursive=True,
allow_files=True,
allow_folders=False
)
def __init__(self, *args, **kwargs):
"""Init download file form."""
super(DownloadFileForm, self).__init__(*args, **kwargs)
class ContactUsForm(forms.Form):
"""Manage "Contact us" form."""
name = forms.CharField(
label=_("Name"),
required=True,
max_length=512,
widget=forms.TextInput(attrs={"autocomplete": "name"}),
)
email = forms.EmailField(
label=_("Email"),
required=True,
widget=forms.EmailInput(attrs={"autocomplete": "email"}),
)
subject = forms.ChoiceField(
label=_("Subject"),
help_text=_("Please choose a subject related to your request"),
required=True,
choices=SUBJECT_CHOICES,
widget=forms.Select(),
)
description = forms.CharField(
label=_("Description"),
help_text=_("Provide a full description for your request"),
widget=forms.Textarea(),
required=True,
)
captcha = CaptchaField(
label=_("Please indicate the result of the following operation")
)
url_referrer = forms.URLField(required=False, widget=forms.HiddenInput())
valid_human = forms.BooleanField(
required=False,
label=_("Check this box if you are a metal human (required)"),
widget=forms.CheckboxInput(),
)
def __init__(self, request, *args, **kwargs):
"""Init contact us form."""
super(ContactUsForm, self).__init__(*args, **kwargs)
if request.user and request.user.is_authenticated:
self.fields["name"].widget = forms.HiddenInput()
self.fields["email"].widget = forms.HiddenInput()
self.initial["name"] = "%s" % request.user
self.initial["email"] = "%s" % request.user.email
# del self.fields['captcha']
self.fields = add_placeholder_and_asterisk(self.fields)
def clean(self):
"""Clean "contact us" form submission."""
cleaned_data = super(ContactUsForm, self).clean()
if "subject" in cleaned_data and cleaned_data["subject"] == "-----":
self._errors["subject"] = self.error_class([_("Please specify a subject")])
return cleaned_data