[History] handle TypeError when adding action via web
[mygpo.git] / mygpo / web / forms.py
blob432c6b9d272c5f52587156dcdd3f48aaa7170a96
1 import re
3 from django import forms
4 from django.utils.translation import ugettext as _
6 from mygpo.users.models import Client
8 import logging
9 logger = logging.getLogger(__name__)
12 class UserAccountForm(forms.Form):
13 """
14 the form that is used in the account settings.
16 if one of the three password fields is set, a password change is assumed
17 and the current and new passwords are checked.
18 """
19 email = forms.EmailField(
20 label=_('E-Mail address'),
21 widget=forms.TextInput(attrs={
22 'class': 'input input-sm form-control',
23 }),
24 required=True)
26 password_current = forms.CharField(
27 label=_(u'Current password'),
28 widget=forms.PasswordInput(render_value=False, attrs={
29 'class': 'input input-sm form-control',
30 }),
31 required=False)
33 password1 = forms.CharField(
34 label=_(u'New password'),
35 widget=forms.PasswordInput(render_value=False, attrs={
36 'class': 'input input-sm form-control',
37 }),
38 required=False)
40 password2 = forms.CharField(
41 label=_(u'Confirm password'),
42 widget=forms.PasswordInput(render_value=False, attrs={
43 'class': 'input input-sm form-control',
44 }),
45 required=False)
47 def is_valid(self):
48 if not super(UserAccountForm, self).is_valid():
49 return False
51 pw1 = self.cleaned_data['password1']
52 pw2 = self.cleaned_data['password2']
54 if self.cleaned_data['password_current'] or pw1 or pw2:
56 if self.cleaned_data['password_current'] == '':
57 return False # must give current password
59 if pw1 == '':
60 return False # cant set empty password
62 if pw1 != pw2:
63 return False # 5must confirm password
65 return True
68 class ProfileForm(forms.Form):
69 twitter = forms.CharField(
70 label=_(u'Twitter'),
71 widget=forms.TextInput(attrs={
72 'class': 'input input-sm form-control',
73 }),
74 required=False,
77 about = forms.CharField(
78 label=_(u'A few words about you'),
79 required=False,
80 widget=forms.Textarea(attrs={
81 'class': 'input input-sm form-control',
82 }),
83 help_text='You can use Markdown',
87 class FlattrForm(forms.Form):
88 """ Per-user Flattr settings """
90 # Authentication token; empty or None when not signed in
91 token = forms.CharField(
92 required=False,
93 label=_('Token'))
95 # Auto-flattring enabled
96 enable = forms.BooleanField(
97 required=False,
98 label=_('Auto-Flattr played episodes'),
99 widget=forms.CheckboxInput(attrs={
100 'class': 'input input-sm form-control',
104 # Auto-flattr mygpo (or whatever the FLATTR_MYGPO_THING
105 # in settings_prod.py is) on every other flattr
106 flattr_mygpo = forms.BooleanField(
107 required=False,
108 label=_('Flattr us'),
109 widget=forms.CheckboxInput(attrs={
110 'class': 'input input-sm form-control',
114 # username under which own content (eg podcast lists) should be published
115 username = forms.CharField(
116 required=False,
117 label=_('Username for own content'),
118 widget=forms.TextInput(attrs={
119 'class': 'input input-sm form-control',
120 'placeholder': 'Device Name',
125 class DeviceForm(forms.Form):
127 form for editing device information by a user.
129 name = forms.CharField(max_length=100, label=_('Name'),
130 widget=forms.TextInput(attrs={
131 'class': 'input input-sm form-control',
132 'placeholder': 'Device Name',
134 type = forms.ChoiceField(choices=Client.TYPES, label=_('Type'),
135 widget=forms.Select(attrs={
136 'class': 'input input-sm form-control',
138 uid = forms.CharField(max_length=50, label=_('Device ID'),
139 widget=forms.TextInput(attrs={
140 'class': 'input input-sm form-control',
141 'placeholder': _('ID on device'),
145 class PrivacyForm(forms.Form):
147 Form for editing the privacy settings for a subscription. It is shown on a
148 podcast page if the current user is subscribed to the podcast.
151 public = forms.BooleanField(
152 required=False,
153 label=_('Share this subscription with other users (public)'))
156 class SyncForm(forms.Form):
158 Form that is used to select either a single devices or a device group.
161 targets = forms.CharField(
162 widget = forms.Select(attrs={
163 'class': 'input input-sm form-control',
167 def set_targets(self, sync_targets, label=''):
168 targets = map(self.sync_target_choice, sync_targets)
169 self.fields['targets'] = forms.ChoiceField(
170 choices=targets,
171 label=label)
173 def sync_target_choice(self, target):
175 returns a list of tuples that can be used as choices for a ChoiceField.
176 the first item in each tuple is a letter identifying the type of the
177 sync-target - either d for a Device, or g for a SyncGroup. This letter
178 is followed by the id of the target.
179 The second item in each tuple is the string-representation of the #
180 target.
183 if isinstance(target, Client):
184 return (target.uid, target.name)
186 elif isinstance(target, list):
187 return (target[0].uid, ', '.join(d.name for d in target))
189 def get_target(self):
191 returns the target (device or device group) that has been selected
192 in the form.
194 if not self.is_valid():
195 logger.warn('no target given in SyncForm')
196 raise ValueError(_('No device selected'))
198 target = self.cleaned_data['targets']
199 return target
202 class ResendActivationForm(forms.Form):
203 username = forms.CharField(
204 max_length=100,
205 label=_('Please enter your username'),
206 required=False)
208 email = forms.CharField(
209 max_length=100,
210 label=_('or the email address used while registering'),
211 required=False)
214 class RestorePasswordForm(forms.Form):
215 username = forms.CharField(
216 max_length=100,
217 label=_('Username'),
218 required=False)
220 email = forms.CharField(
221 max_length=100,
222 label=_('E-Mail address'),
223 required=False)