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