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(label
=_('E-Mail address'))
21 password_current
= forms
.CharField(
22 label
=_(u
'Current password'),
23 widget
=forms
.PasswordInput(render_value
=False),
26 password1
= forms
.CharField(
27 label
=_(u
'New password'),
28 widget
=forms
.PasswordInput(render_value
=False),
31 password2
= forms
.CharField(
32 label
=_(u
'Confirm password'),
33 widget
=forms
.PasswordInput(render_value
=False),
37 if not super(UserAccountForm
, self
).is_valid():
40 pw1
= self
.cleaned_data
['password1']
41 pw2
= self
.cleaned_data
['password2']
43 if self
.cleaned_data
['password_current'] or pw1
or pw2
:
45 if self
.cleaned_data
['password_current'] == '':
46 return False # must give current password
49 return False # cant set empty password
52 return False # 5must confirm password
57 class ProfileForm(forms
.Form
):
58 twitter
= forms
.CharField(
62 about
= forms
.CharField(
63 label
=_(u
'A few words about you'),
65 widget
=forms
.Textarea
,
66 help_text
='You can use Markdown')
69 class FlattrForm(forms
.Form
):
70 """ Per-user Flattr settings """
72 # Authentication token; empty or None when not signed in
73 token
= forms
.CharField(
77 # Auto-flattring enabled
78 enable
= forms
.BooleanField(
80 label
=_('Auto-Flattr played episodes'))
82 # Auto-flattr mygpo (or whatever the FLATTR_MYGPO_THING
83 # in settings_prod.py is) on every other flattr
84 flattr_mygpo
= forms
.BooleanField(required
=False, label
=_('Flattr us'))
86 # username under which own content (eg podcast lists) should be published
87 username
= forms
.CharField(
89 label
=_('Username for own content'))
92 class DeviceForm(forms
.Form
):
94 form for editing device information by a user.
96 name
= forms
.CharField(max_length
=100, label
=_('Name'))
97 type = forms
.ChoiceField(choices
=DEVICE_TYPES
, label
=_('Type'))
98 uid
= forms
.CharField(max_length
=50, label
=_('Device ID'))
101 class PrivacyForm(forms
.Form
):
103 Form for editing the privacy settings for a subscription. It is shown on a
104 podcast page if the current user is subscribed to the podcast.
107 public
= forms
.BooleanField(
109 label
=_('Share this subscription with other users (public)'))
112 class SyncForm(forms
.Form
):
114 Form that is used to select either a single devices or a device group.
117 targets
= forms
.CharField()
119 def set_targets(self
, sync_targets
, label
=''):
120 targets
= map(self
.sync_target_choice
, sync_targets
)
121 self
.fields
['targets'] = forms
.ChoiceField(
125 def sync_target_choice(self
, target
):
127 returns a list of tuples that can be used as choices for a ChoiceField.
128 the first item in each tuple is a letter identifying the type of the
129 sync-target - either d for a Device, or g for a SyncGroup. This letter
130 is followed by the id of the target.
131 The second item in each tuple is the string-representation of the #
135 if isinstance(target
, Device
):
136 return (target
.uid
, target
.name
)
138 elif isinstance(target
, list):
139 return (target
[0].uid
, ', '.join(d
.name
for d
in target
))
141 def get_target(self
):
143 returns the target (device or device group) that has been selected
146 if not self
.is_valid():
147 logger
.warn('no target given in SyncForm')
148 raise ValueError(_('No device selected'))
150 target
= self
.cleaned_data
['targets']
154 class ResendActivationForm(forms
.Form
):
155 username
= forms
.CharField(
157 label
=_('Please enter your username'),
160 email
= forms
.CharField(
162 label
=_('or the email address used while registering'),
166 class RestorePasswordForm(forms
.Form
):
167 username
= forms
.CharField(
172 email
= forms
.CharField(
174 label
=_('E-Mail address'),