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
):
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.
18 email
= forms
.EmailField(label
=_('E-Mail address'))
19 password_current
= forms
.CharField(label
=_(u
'Current password'),widget
=forms
.PasswordInput(render_value
=False), required
=False)
20 password1
= forms
.CharField(label
=_(u
'New password'),widget
=forms
.PasswordInput(render_value
=False), required
=False)
21 password2
= forms
.CharField(label
=_(u
'Confirm password'),widget
=forms
.PasswordInput(render_value
=False), required
=False)
24 if not super(UserAccountForm
, self
).is_valid(): return False
26 if self
.cleaned_data
['password_current'] or self
.cleaned_data
['password1'] or self
.cleaned_data
['password2']:
27 if self
.cleaned_data
['password_current'] == '':
28 return False #must give current password
30 if self
.cleaned_data
['password1'] == '':
31 return False #cant set empty password
33 if self
.cleaned_data
['password1'] != self
.cleaned_data
['password2']:
34 return False #must confirm password
39 class ProfileForm(forms
.Form
):
40 twitter
= forms
.CharField(label
=_(u
'Twitter'), required
=False)
41 about
= forms
.CharField(label
=_(u
'A few words about you'), required
=False, widget
=forms
.Textarea
, help_text
='You can use Markdown')
44 class DeviceForm(forms
.Form
):
46 form for editing device information by a user.
48 name
= forms
.CharField(max_length
=100, label
=_('Name'))
49 type = forms
.ChoiceField(choices
=DEVICE_TYPES
, label
=_('Type'))
50 uid
= forms
.CharField(max_length
=50, label
=_('Device ID'))
52 class PrivacyForm(forms
.Form
):
54 Form for editing the privacy settings for a subscription. It is shown on a
55 podcast page if the current user is subscribed to the podcast.
57 public
= forms
.BooleanField(required
=False, label
=_('Share this subscription with other users (public)'))
59 class SyncForm(forms
.Form
):
61 Form that is used to select either a single devices or a device group.
64 targets
= forms
.CharField()
66 def set_targets(self
, sync_targets
, label
=''):
67 targets
= map(self
.sync_target_choice
, sync_targets
)
68 self
.fields
['targets'] = forms
.ChoiceField(choices
=targets
, label
=label
)
71 def sync_target_choice(self
, target
):
73 returns a list of tuples that can be used as choices for a ChoiceField.
74 the first item in each tuple is a letter identifying the type of the
75 sync-target - either d for a Device, or g for a SyncGroup. This letter
76 is followed by the id of the target.
77 The second item in each tuple is the string-representation of the #
81 if isinstance(target
, Device
):
82 return (target
.uid
, target
.name
)
84 elif isinstance(target
, list):
85 return (target
[0].uid
, ', '.join(d
.name
for d
in target
))
90 returns the target (device or device group) that has been selected
93 if not self
.is_valid():
94 log('no target given in SyncForm')
95 raise ValueError(_('No device selected'))
97 target
= self
.cleaned_data
['targets']
101 class ResendActivationForm(forms
.Form
):
102 username
= forms
.CharField(max_length
=100, label
=_('Please enter your username'), required
=False)
103 email
= forms
.CharField(max_length
=100, label
=_('or the email address used while registering'), required
=False)
106 class RestorePasswordForm(forms
.Form
):
107 username
= forms
.CharField(max_length
=100, label
=_('Username'), required
=False)
108 email
= forms
.CharField(max_length
=100, label
=_('E-Mail address'), required
=False)