use django.contrib.staticfiles everywhere
[mygpo.git] / mygpo / web / forms.py
blob2feee34637eb1f4c6c1ce46465b2a950d22f1266
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.users.models import Device
9 import logging
10 logger = logging.getLogger(__name__)
13 class UserAccountForm(forms.Form):
14 """
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.
19 """
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),
24 required=False)
26 password1 = forms.CharField(
27 label=_(u'New password'),
28 widget=forms.PasswordInput(render_value=False),
29 required=False)
31 password2 = forms.CharField(
32 label=_(u'Confirm password'),
33 widget=forms.PasswordInput(render_value=False),
34 required=False)
36 def is_valid(self):
37 if not super(UserAccountForm, self).is_valid():
38 return False
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
48 if pw1 == '':
49 return False # cant set empty password
51 if pw1 != pw2:
52 return False # 5must confirm password
54 return True
57 class ProfileForm(forms.Form):
58 twitter = forms.CharField(
59 label=_(u'Twitter'),
60 required=False)
62 about = forms.CharField(
63 label=_(u'A few words about you'),
64 required=False,
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(
74 required=False,
75 label=_('Token'))
77 # Auto-flattring enabled
78 enable = forms.BooleanField(
79 required=False,
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(
88 required=False,
89 label=_('Username for own content'))
92 class DeviceForm(forms.Form):
93 """
94 form for editing device information by a user.
95 """
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(
108 required=False,
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(
122 choices=targets,
123 label=label)
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 #
132 target.
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
144 in the form.
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']
151 return target
154 class ResendActivationForm(forms.Form):
155 username = forms.CharField(
156 max_length=100,
157 label=_('Please enter your username'),
158 required=False)
160 email = forms.CharField(
161 max_length=100,
162 label=_('or the email address used while registering'),
163 required=False)
166 class RestorePasswordForm(forms.Form):
167 username = forms.CharField(
168 max_length=100,
169 label=_('Username'),
170 required=False)
172 email = forms.CharField(
173 max_length=100,
174 label=_('E-Mail address'),
175 required=False)