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
10 logger
= logging
.getLogger(__name__
)
13 class UserAccountForm(forms
.Form
):
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.
20 email
= forms
.EmailField(
21 label
=_('E-Mail address'),
22 widget
=forms
.TextInput(attrs
={
23 'class': 'input input-sm form-control',
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',
34 password1
= forms
.CharField(
35 label
=_(u
'New password'),
36 widget
=forms
.PasswordInput(render_value
=False, attrs
={
37 'class': 'input input-sm form-control',
41 password2
= forms
.CharField(
42 label
=_(u
'Confirm password'),
43 widget
=forms
.PasswordInput(render_value
=False, attrs
={
44 'class': 'input input-sm form-control',
49 if not super(UserAccountForm
, self
).is_valid():
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
61 return False # cant set empty password
64 return False # 5must confirm password
69 class ProfileForm(forms
.Form
):
70 twitter
= forms
.CharField(
72 widget
=forms
.TextInput(attrs
={
73 'class': 'input input-sm form-control',
78 about
= forms
.CharField(
79 label
=_(u
'A few words about you'),
81 widget
=forms
.Textarea(attrs
={
82 'class': 'input input-sm form-control',
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(
96 # Auto-flattring enabled
97 enable
= forms
.BooleanField(
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(
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(
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(
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(
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 #
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
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']
203 class ResendActivationForm(forms
.Form
):
204 username
= forms
.CharField(
206 label
=_('Please enter your username'),
209 email
= forms
.CharField(
211 label
=_('or the email address used while registering'),
215 class RestorePasswordForm(forms
.Form
):
216 username
= forms
.CharField(
221 email
= forms
.CharField(
223 label
=_('E-Mail address'),