Merge branch 'master' into static-media
[mygpo.git] / mygpo / web / forms.py
blob30758683c41f20373b3ac6ef1fdabda808586d88
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=_('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=_('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=_('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=_('Twitter'),
71 widget=forms.TextInput(attrs={
72 'class': 'input input-sm form-control',
73 }),
74 required=False,
77 about = forms.CharField(
78 label=_('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 DeviceForm(forms.Form):
88 """
89 form for editing device information by a user.
90 """
91 name = forms.CharField(max_length=100, label=_('Name'),
92 widget=forms.TextInput(attrs={
93 'class': 'input input-sm form-control',
94 'placeholder': 'Device Name',
95 }))
96 type = forms.ChoiceField(choices=Client.TYPES, label=_('Type'),
97 widget=forms.Select(attrs={
98 'class': 'input input-sm form-control',
99 }))
100 uid = forms.CharField(max_length=50, label=_('Device ID'),
101 widget=forms.TextInput(attrs={
102 'class': 'input input-sm form-control',
103 'placeholder': _('ID on device'),
107 class PrivacyForm(forms.Form):
109 Form for editing the privacy settings for a subscription. It is shown on a
110 podcast page if the current user is subscribed to the podcast.
113 public = forms.BooleanField(
114 required=False,
115 label=_('Share this subscription with other users (public)'))
118 class SyncForm(forms.Form):
120 Form that is used to select either a single devices or a device group.
123 targets = forms.CharField(
124 widget = forms.Select(attrs={
125 'class': 'input input-sm form-control',
129 def set_targets(self, sync_targets, label=''):
130 targets = list(map(self.sync_target_choice, sync_targets))
131 self.fields['targets'] = forms.ChoiceField(
132 choices=targets,
133 label=label)
135 def sync_target_choice(self, target):
137 returns a list of tuples that can be used as choices for a ChoiceField.
138 the first item in each tuple is a letter identifying the type of the
139 sync-target - either d for a Device, or g for a SyncGroup. This letter
140 is followed by the id of the target.
141 The second item in each tuple is the string-representation of the #
142 target.
145 if isinstance(target, Client):
146 return (target.uid, target.name)
148 elif isinstance(target, list):
149 return (target[0].uid, ', '.join(d.name for d in target))
151 def get_target(self):
153 returns the target (device or device group) that has been selected
154 in the form.
156 if not self.is_valid():
157 logger.warn('no target given in SyncForm')
158 raise ValueError(_('No device selected'))
160 target = self.cleaned_data['targets']
161 return target
164 class ResendActivationForm(forms.Form):
165 username = forms.CharField(
166 max_length=100,
167 label=_('Please enter your username'),
168 required=False)
170 email = forms.CharField(
171 max_length=100,
172 label=_('or the email address used while registering'),
173 required=False)
176 class RestorePasswordForm(forms.Form):
177 username = forms.CharField(
178 max_length=100,
179 label=_('Username'),
180 required=False)
182 email = forms.CharField(
183 max_length=100,
184 label=_('E-Mail address'),
185 required=False)