/
forms.py
94 lines (64 loc) · 2.83 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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
import re
from datetime import datetime
from random import randrange
from django import forms
from django.forms import widgets
from django.urls import reverse
from django.utils.safestring import mark_safe
from lib.l10n_utils.fluent import ftl
LANGS_TO_STRIP = ["en-US", "es"]
PARENTHETIC_RE = re.compile(r" \([^)]+\)$")
def strip_parenthetical(lang_name):
"""
Remove the parenthetical from the end of the language name string.
"""
return PARENTHETIC_RE.sub("", lang_name, 1)
class PrivacyWidget(widgets.CheckboxInput):
"""Render a checkbox with privacy text. Lots of pages need this so
it should be standardized"""
def render(self, name, value, attrs=None, renderer=None):
attrs["required"] = "required"
input_txt = super().render(name, value, attrs)
policy_txt = ftl("newsletter-form-im-okay-with-mozilla", url=reverse("privacy.notices.websites"))
return mark_safe(f"""<label for="{attrs['id']}" class="privacy-check-label">{input_txt}<span class="title">{policy_txt}</span></label>""")
class HoneyPotWidget(widgets.TextInput):
"""Render a text field to (hopefully) trick bots. Will be used on many pages."""
def render(self, name, value, attrs=None, renderer=None):
honeypot_txt = ftl("newsletter-form-leave-this-field-empty")
# semi-randomized in case we have more than one per page.
# this is maybe/probably overthought
honeypot_id = "office-fax-" + str(randrange(1001)) + "-" + str(datetime.now().strftime("%Y%m%d%H%M%S%f"))
return mark_safe(
'<div class="super-priority-field">'
'<label for="%s">%s</label>'
'<input type="text" name="office_fax" id="%s">'
"</div>" % (honeypot_id, honeypot_txt, honeypot_id)
)
class URLInput(widgets.TextInput):
input_type = "url"
class EmailInput(widgets.TextInput):
input_type = "email"
class DateInput(widgets.DateInput):
input_type = "date"
class TimeInput(widgets.TimeInput):
input_type = "time"
class TelInput(widgets.TextInput):
input_type = "tel"
class NumberInput(widgets.TextInput):
input_type = "number"
class MiecoEmailForm(forms.Form):
"""
A form class used to validate the data coming from the MIECO site.
"""
MESSAGE_ID_CHOICES = (
("innovations", "innovations"),
("mieco", "mieco"),
)
name = forms.CharField(required=False, max_length=100)
email = forms.EmailField()
interests = forms.CharField(required=False, max_length=100)
description = forms.CharField(required=False, max_length=750)
message_id = forms.ChoiceField(required=False, choices=MESSAGE_ID_CHOICES)