return values instead of raising them
[mygpo.git] / mygpo / web / forms.py
blob3d9b5299d00b4d548aa15eacbab09516317f1b8b
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.log import log
8 from mygpo.users.models import Device
11 class UserAccountForm(forms.Form):
12 """
13 the form that is used in the account settings.
15 if one of the three password fields is set, a password change is assumed
16 and the current and new passwords are checked.
17 """
18 email = forms.EmailField(label=_('E-Mail address'))
19 password_current = forms.CharField(
20 label=_(u'Current password'),
21 widget=forms.PasswordInput(render_value=False),
22 required=False)
24 password1 = forms.CharField(
25 label=_(u'New password'),
26 widget=forms.PasswordInput(render_value=False),
27 required=False)
29 password2 = forms.CharField(
30 label=_(u'Confirm password'),
31 widget=forms.PasswordInput(render_value=False),
32 required=False)
34 def is_valid(self):
35 if not super(UserAccountForm, self).is_valid():
36 return False
38 pw1 = self.cleaned_data['password1']
39 pw2 = self.cleaned_data['password2']
41 if self.cleaned_data['password_current'] or pw1 or pw2:
43 if self.cleaned_data['password_current'] == '':
44 return False # must give current password
46 if pw1 == '':
47 return False # cant set empty password
49 if pw1 != pw2:
50 return False # 5must confirm password
52 return True
55 class ProfileForm(forms.Form):
56 twitter = forms.CharField(
57 label=_(u'Twitter'),
58 required=False)
60 about = forms.CharField(
61 label=_(u'A few words about you'),
62 required=False,
63 widget=forms.Textarea,
64 help_text='You can use Markdown')
67 class FlattrForm(forms.Form):
68 """ Per-user Flattr settings """
70 # Authentication token; empty or None when not signed in
71 token = forms.CharField(
72 required=False,
73 label=_('Token'))
75 # Auto-flattring enabled
76 enable = forms.BooleanField(
77 required=False,
78 label=_('Auto-Flattr played episodes'))
80 # Auto-flattr mygpo (or whatever the FLATTR_MYGPO_THING
81 # in settings_prod.py is) on every other flattr
82 flattr_mygpo = forms.BooleanField(required=False, label=_('Flattr us'))
84 # username under which own content (eg podcast lists) should be published
85 username = forms.CharField(
86 required=False,
87 label=_('Username for own content'))
90 class DeviceForm(forms.Form):
91 """
92 form for editing device information by a user.
93 """
94 name = forms.CharField(max_length=100, label=_('Name'))
95 type = forms.ChoiceField(choices=DEVICE_TYPES, label=_('Type'))
96 uid = forms.CharField(max_length=50, label=_('Device ID'))
99 class PrivacyForm(forms.Form):
101 Form for editing the privacy settings for a subscription. It is shown on a
102 podcast page if the current user is subscribed to the podcast.
105 public = forms.BooleanField(
106 required=False,
107 label=_('Share this subscription with other users (public)'))
110 class SyncForm(forms.Form):
112 Form that is used to select either a single devices or a device group.
115 targets = forms.CharField()
117 def set_targets(self, sync_targets, label=''):
118 targets = map(self.sync_target_choice, sync_targets)
119 self.fields['targets'] = forms.ChoiceField(
120 choices=targets,
121 label=label)
123 def sync_target_choice(self, target):
125 returns a list of tuples that can be used as choices for a ChoiceField.
126 the first item in each tuple is a letter identifying the type of the
127 sync-target - either d for a Device, or g for a SyncGroup. This letter
128 is followed by the id of the target.
129 The second item in each tuple is the string-representation of the #
130 target.
133 if isinstance(target, Device):
134 return (target.uid, target.name)
136 elif isinstance(target, list):
137 return (target[0].uid, ', '.join(d.name for d in target))
139 def get_target(self):
141 returns the target (device or device group) that has been selected
142 in the form.
144 if not self.is_valid():
145 log('no target given in SyncForm')
146 raise ValueError(_('No device selected'))
148 target = self.cleaned_data['targets']
149 return target
152 class ResendActivationForm(forms.Form):
153 username = forms.CharField(
154 max_length=100,
155 label=_('Please enter your username'),
156 required=False)
158 email = forms.CharField(
159 max_length=100,
160 label=_('or the email address used while registering'),
161 required=False)
164 class RestorePasswordForm(forms.Form):
165 username = forms.CharField(
166 max_length=100,
167 label=_('Username'),
168 required=False)
170 email = forms.CharField(
171 max_length=100,
172 label=_('E-Mail address'),
173 required=False)