Bump gevent from 21.8.0 to 21.12.0
[mygpo.git] / mygpo / web / forms.py
blobf603ff64685f12c9e9c788f5f0c3758110e08ac9
1 import re
3 from django import forms
4 from django.utils.translation import gettext as _
6 from mygpo.users.models import Client
8 import logging
10 logger = logging.getLogger(__name__)
13 class UserAccountForm(forms.Form):
14 """
15 the form that is used in the account settings.
17 if one of the three password fields is set, a password change is assumed
18 and the current and new passwords are checked.
19 """
21 email = forms.EmailField(
22 label=_("E-Mail address"),
23 widget=forms.TextInput(attrs={"class": "input input-sm form-control"}),
24 required=True,
27 password_current = forms.CharField(
28 label=_("Current password"),
29 widget=forms.PasswordInput(
30 render_value=False, attrs={"class": "input input-sm form-control"}
32 required=False,
35 password1 = forms.CharField(
36 label=_("New password"),
37 widget=forms.PasswordInput(
38 render_value=False, attrs={"class": "input input-sm form-control"}
40 required=False,
43 password2 = forms.CharField(
44 label=_("Confirm password"),
45 widget=forms.PasswordInput(
46 render_value=False, attrs={"class": "input input-sm form-control"}
48 required=False,
51 def is_valid(self):
52 if not super(UserAccountForm, self).is_valid():
53 return False
55 pw1 = self.cleaned_data["password1"]
56 pw2 = self.cleaned_data["password2"]
58 if self.cleaned_data["password_current"] or pw1 or pw2:
60 if self.cleaned_data["password_current"] == "":
61 return False # must give current password
63 if pw1 == "":
64 return False # cant set empty password
66 if pw1 != pw2:
67 return False # 5must confirm password
69 return True
72 class ProfileForm(forms.Form):
73 twitter = forms.CharField(
74 label=_("Twitter"),
75 widget=forms.TextInput(attrs={"class": "input input-sm form-control"}),
76 required=False,
79 about = forms.CharField(
80 label=_("A few words about you"),
81 required=False,
82 widget=forms.Textarea(attrs={"class": "input input-sm form-control"}),
83 help_text="You can use Markdown",
87 class DeviceForm(forms.Form):
88 """
89 form for editing device information by a user.
90 """
92 name = forms.CharField(
93 max_length=100,
94 label=_("Name"),
95 widget=forms.TextInput(
96 attrs={"class": "input input-sm form-control", "placeholder": "Device Name"}
99 type = forms.ChoiceField(
100 choices=Client.TYPES,
101 label=_("Type"),
102 widget=forms.Select(attrs={"class": "input input-sm form-control"}),
104 uid = forms.CharField(
105 max_length=50,
106 label=_("Device ID"),
107 widget=forms.TextInput(
108 attrs={
109 "class": "input input-sm form-control",
110 "placeholder": _("ID on device"),
116 class PrivacyForm(forms.Form):
118 Form for editing the privacy settings for a subscription. It is shown on a
119 podcast page if the current user is subscribed to the podcast.
122 public = forms.BooleanField(
123 required=False, label=_("Share this subscription with other users (public)")
127 class SyncForm(forms.Form):
129 Form that is used to select either a single devices or a device group.
132 targets = forms.CharField(
133 widget=forms.Select(attrs={"class": "input input-sm form-control"})
136 def set_targets(self, sync_targets, label=""):
137 targets = list(map(self.sync_target_choice, sync_targets))
138 self.fields["targets"] = forms.ChoiceField(choices=targets, label=label)
140 def sync_target_choice(self, target):
142 returns a list of tuples that can be used as choices for a ChoiceField.
143 the first item in each tuple is a letter identifying the type of the
144 sync-target - either d for a Device, or g for a SyncGroup. This letter
145 is followed by the id of the target.
146 The second item in each tuple is the string-representation of the #
147 target.
150 if isinstance(target, Client):
151 return (target.uid, target.name)
153 elif isinstance(target, list):
154 return (target[0].uid, ", ".join(d.name for d in target))
156 def get_target(self):
158 returns the target (device or device group) that has been selected
159 in the form.
161 if not self.is_valid():
162 logger.warning("no target given in SyncForm")
163 raise ValueError(_("No device selected"))
165 target = self.cleaned_data["targets"]
166 return target
169 class ResendActivationForm(forms.Form):
170 username = forms.CharField(
171 max_length=100, label=_("Please enter your username"), required=False
174 email = forms.CharField(
175 max_length=100,
176 label=_("or the email address used while registering"),
177 required=False,
181 class RestorePasswordForm(forms.Form):
182 username = forms.CharField(max_length=100, label=_("Username"), required=False)
184 email = forms.CharField(max_length=100, label=_("E-Mail address"), required=False)