[Tests] fix number of expected queries
[mygpo.git] / mygpo / web / forms.py
blobff0405cc643ef6de9964a34b83d11eefdb826591
1 import re
3 from django import forms
4 from django.utils.translation import ugettext as _
6 from mygpo.api.constants import DEVICE_TYPES
7 from mygpo.users.models import Device
9 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 """
20 email = forms.EmailField(
21 label=_('E-Mail address'),
22 widget=forms.TextInput(attrs={
23 'class': 'input input-sm form-control',
24 }),
25 required=True)
27 password_current = forms.CharField(
28 label=_(u'Current password'),
29 widget=forms.PasswordInput(render_value=False, attrs={
30 'class': 'input input-sm form-control',
31 }),
32 required=False)
34 password1 = forms.CharField(
35 label=_(u'New password'),
36 widget=forms.PasswordInput(render_value=False, attrs={
37 'class': 'input input-sm form-control',
38 }),
39 required=False)
41 password2 = forms.CharField(
42 label=_(u'Confirm password'),
43 widget=forms.PasswordInput(render_value=False, attrs={
44 'class': 'input input-sm form-control',
45 }),
46 required=False)
48 def is_valid(self):
49 if not super(UserAccountForm, self).is_valid():
50 return False
52 pw1 = self.cleaned_data['password1']
53 pw2 = self.cleaned_data['password2']
55 if self.cleaned_data['password_current'] or pw1 or pw2:
57 if self.cleaned_data['password_current'] == '':
58 return False # must give current password
60 if pw1 == '':
61 return False # cant set empty password
63 if pw1 != pw2:
64 return False # 5must confirm password
66 return True
69 class ProfileForm(forms.Form):
70 twitter = forms.CharField(
71 label=_(u'Twitter'),
72 widget=forms.TextInput(attrs={
73 'class': 'input input-sm form-control',
74 }),
75 required=False,
78 about = forms.CharField(
79 label=_(u'A few words about you'),
80 required=False,
81 widget=forms.Textarea(attrs={
82 'class': 'input input-sm form-control',
83 }),
84 help_text='You can use Markdown',
88 class FlattrForm(forms.Form):
89 """ Per-user Flattr settings """
91 # Authentication token; empty or None when not signed in
92 token = forms.CharField(
93 required=False,
94 label=_('Token'))
96 # Auto-flattring enabled
97 enable = forms.BooleanField(
98 required=False,
99 label=_('Auto-Flattr played episodes'),
100 widget=forms.CheckboxInput(attrs={
101 'class': 'input input-sm form-control',
105 # Auto-flattr mygpo (or whatever the FLATTR_MYGPO_THING
106 # in settings_prod.py is) on every other flattr
107 flattr_mygpo = forms.BooleanField(
108 required=False,
109 label=_('Flattr us'),
110 widget=forms.CheckboxInput(attrs={
111 'class': 'input input-sm form-control',
115 # username under which own content (eg podcast lists) should be published
116 username = forms.CharField(
117 required=False,
118 label=_('Username for own content'),
119 widget=forms.TextInput(attrs={
120 'class': 'input input-sm form-control',
121 'placeholder': 'Device Name',
126 class DeviceForm(forms.Form):
128 form for editing device information by a user.
130 name = forms.CharField(max_length=100, label=_('Name'),
131 widget=forms.TextInput(attrs={
132 'class': 'input input-sm form-control',
133 'placeholder': 'Device Name',
135 type = forms.ChoiceField(choices=DEVICE_TYPES, label=_('Type'),
136 widget=forms.Select(attrs={
137 'class': 'input input-sm form-control',
139 uid = forms.CharField(max_length=50, label=_('Device ID'),
140 widget=forms.TextInput(attrs={
141 'class': 'input input-sm form-control',
142 'placeholder': _('ID on device'),
146 class PrivacyForm(forms.Form):
148 Form for editing the privacy settings for a subscription. It is shown on a
149 podcast page if the current user is subscribed to the podcast.
152 public = forms.BooleanField(
153 required=False,
154 label=_('Share this subscription with other users (public)'))
157 class SyncForm(forms.Form):
159 Form that is used to select either a single devices or a device group.
162 targets = forms.CharField(
163 widget = forms.Select(attrs={
164 'class': 'input input-sm form-control',
168 def set_targets(self, sync_targets, label=''):
169 targets = map(self.sync_target_choice, sync_targets)
170 self.fields['targets'] = forms.ChoiceField(
171 choices=targets,
172 label=label)
174 def sync_target_choice(self, target):
176 returns a list of tuples that can be used as choices for a ChoiceField.
177 the first item in each tuple is a letter identifying the type of the
178 sync-target - either d for a Device, or g for a SyncGroup. This letter
179 is followed by the id of the target.
180 The second item in each tuple is the string-representation of the #
181 target.
184 if isinstance(target, Device):
185 return (target.uid, target.name)
187 elif isinstance(target, list):
188 return (target[0].uid, ', '.join(d.name for d in target))
190 def get_target(self):
192 returns the target (device or device group) that has been selected
193 in the form.
195 if not self.is_valid():
196 logger.warn('no target given in SyncForm')
197 raise ValueError(_('No device selected'))
199 target = self.cleaned_data['targets']
200 return target
203 class ResendActivationForm(forms.Form):
204 username = forms.CharField(
205 max_length=100,
206 label=_('Please enter your username'),
207 required=False)
209 email = forms.CharField(
210 max_length=100,
211 label=_('or the email address used while registering'),
212 required=False)
215 class RestorePasswordForm(forms.Form):
216 username = forms.CharField(
217 max_length=100,
218 label=_('Username'),
219 required=False)
221 email = forms.CharField(
222 max_length=100,
223 label=_('E-Mail address'),
224 required=False)