Add Catch-all Update-Retrieval to Advanced API
[mygpo.git] / mygpo / web / forms.py
blobb47caa82dd9170824f630d294576391f7e62de6d
1 from django import forms
2 from django.utils.translation import ugettext as _
3 from django.forms.util import ValidationError
4 from mygpo.api.models import Device, DEVICE_TYPES, SyncGroup
5 from mygpo.log import log
6 import re
8 class UserAccountForm(forms.Form):
9 """
10 the form that is used in the account settings.
12 if one of the three password fields is set, a password change is assumed
13 and the current and new passwords are checked.
14 """
15 email = forms.EmailField(label=_('E-Mail address'))
16 password_current = forms.CharField(label=_(u'Current password'),widget=forms.PasswordInput(render_value=False), required=False)
17 password1 = forms.CharField(label=_(u'New password'),widget=forms.PasswordInput(render_value=False), required=False)
18 password2 = forms.CharField(label=_(u'Confirm password'),widget=forms.PasswordInput(render_value=False), required=False)
20 def is_valid(self):
21 if not super(UserAccountForm, self).is_valid(): return False
23 if self.cleaned_data['password_current'] or self.cleaned_data['password1'] or self.cleaned_data['password2']:
24 if self.cleaned_data['password_current'] == '':
25 return False #must give current password
27 if self.cleaned_data['password1'] == '':
28 return False #cant set empty password
30 if self.cleaned_data['password1'] != self.cleaned_data['password2']:
31 return False #must confirm password
33 return True
35 class DeviceForm(forms.Form):
36 """
37 form for editing device information by a user.
38 """
39 name = forms.CharField(max_length=100, label=_('Name'))
40 type = forms.ChoiceField(choices=DEVICE_TYPES, label=_('Type'))
41 uid = forms.CharField(max_length=50, label=_('Device ID'))
43 class PrivacyForm(forms.Form):
44 """
45 Form for editing the privacy settings for a subscription. It is shown on a
46 podcast page if the current user is subscribed to the podcast.
47 """
48 public = forms.BooleanField(required=False, label=_('Share this subscription with other users (public)'))
50 class SyncForm(forms.Form):
51 """
52 Form that is used to select either a single devices or a device group.
53 """
55 targets = forms.CharField()
57 def set_targets(self, sync_targets, label=''):
58 targets = self.sync_target_choices(sync_targets)
59 self.fields['targets'] = forms.ChoiceField(choices=targets, label=label)
61 def sync_target_choices(self, targets):
62 """
63 returns a list of tuples that can be used as choices for a ChoiceField.
64 the first item in each tuple is a letter identifying the type of the
65 sync-target - either d for a Device, or g for a SyncGroup. This letter
66 is followed by the id of the target.
67 The second item in each tuple is the string-representation of the #
68 target.
69 """
70 return [('%s%s' % ('d' if isinstance(t, Device) else 'g', t.id), t) for t in targets]
73 def get_target(self):
74 """
75 returns the target (device or device group) that has been selected
76 in the form.
77 """
78 if not self.is_valid():
79 log('no target given in SyncForm')
80 raise ValueError(_('No device selected'))
82 target = self.cleaned_data['targets']
83 m = re.match('^([dg])(\d+)$', target)
84 if m == None:
85 log('invalid target %s given in SyncForm' % target)
86 raise ValueError(_('Invalid device selected: %s') % target)
88 if m.group(1) == 'd':
89 return Device.objects.get(pk=m.group(2))
90 else:
91 return SyncGroup.objects.get(pk=m.group(2))
93 class ResendActivationForm(forms.Form):
94 username = forms.CharField(max_length=100, label=_('Please enter your username'), required=False)
95 email = forms.CharField(max_length=100, label=_('or the email address used while registering'), required=False)
98 class RestorePasswordForm(forms.Form):
99 username = forms.CharField(max_length=100, label=_('Username'), required=False)
100 email = forms.CharField(max_length=100, label=_('E-Mail address'), required=False)