remove deprecation warning for request.raw_post_data
[mygpo.git] / mygpo / web / forms.py
blob85a4293746f6f49c69835000e8801548942a48d6
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.log import log
8 from mygpo.users.models import Device
11 class UserAccountForm(forms.Form):
12 """
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.
17 """
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)
23 def is_valid(self):
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
36 return True
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 FlattrForm(forms.Form):
45 """ Per-user Flattr settings """
47 # Authentication token; empty or None when not signed in
48 token = forms.CharField(required=False, label=_('Token'))
50 # Auto-flattring enabled
51 enable = forms.BooleanField(required=False,
52 label=_('Auto-Flattr played episodes'))
54 # Auto-flattr mygpo (or whatever the FLATTR_MYGPO_THING
55 # in settings_prod.py is) on every other flattr
56 flattr_mygpo = forms.BooleanField(required=False, label=_('Flattr us'))
58 # username under which own content (eg podcast lists) should be published
59 username = forms.CharField(required=False,
60 label=_('Username for own content'))
63 class DeviceForm(forms.Form):
64 """
65 form for editing device information by a user.
66 """
67 name = forms.CharField(max_length=100, label=_('Name'))
68 type = forms.ChoiceField(choices=DEVICE_TYPES, label=_('Type'))
69 uid = forms.CharField(max_length=50, label=_('Device ID'))
71 class PrivacyForm(forms.Form):
72 """
73 Form for editing the privacy settings for a subscription. It is shown on a
74 podcast page if the current user is subscribed to the podcast.
75 """
76 public = forms.BooleanField(required=False, label=_('Share this subscription with other users (public)'))
78 class SyncForm(forms.Form):
79 """
80 Form that is used to select either a single devices or a device group.
81 """
83 targets = forms.CharField()
85 def set_targets(self, sync_targets, label=''):
86 targets = map(self.sync_target_choice, sync_targets)
87 self.fields['targets'] = forms.ChoiceField(choices=targets, label=label)
90 def sync_target_choice(self, target):
91 """
92 returns a list of tuples that can be used as choices for a ChoiceField.
93 the first item in each tuple is a letter identifying the type of the
94 sync-target - either d for a Device, or g for a SyncGroup. This letter
95 is followed by the id of the target.
96 The second item in each tuple is the string-representation of the #
97 target.
98 """
100 if isinstance(target, Device):
101 return (target.uid, target.name)
103 elif isinstance(target, list):
104 return (target[0].uid, ', '.join(d.name for d in target))
107 def get_target(self):
109 returns the target (device or device group) that has been selected
110 in the form.
112 if not self.is_valid():
113 log('no target given in SyncForm')
114 raise ValueError(_('No device selected'))
116 target = self.cleaned_data['targets']
117 return target
120 class ResendActivationForm(forms.Form):
121 username = forms.CharField(max_length=100, label=_('Please enter your username'), required=False)
122 email = forms.CharField(max_length=100, label=_('or the email address used while registering'), required=False)
125 class RestorePasswordForm(forms.Form):
126 username = forms.CharField(max_length=100, label=_('Username'), required=False)
127 email = forms.CharField(max_length=100, label=_('E-Mail address'), required=False)