allow user to resend activation email
[mygpo.git] / mygpo / web / forms.py
blob8d4a94aea1fa566cea0973b9590e53d51d2a1006
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 email = forms.EmailField(label=_('Your Email Address'))
9 public = forms.BooleanField(required=False, label=_('May we use your subscriptions for the toplist and suggestions?'))
11 class DeviceForm(forms.Form):
12 name = forms.CharField(max_length=100, label=_('Name of this device'))
13 type = forms.ChoiceField(choices=DEVICE_TYPES, label=_('What kind of device is this?'))
14 uid = forms.CharField(max_length=50, label=_('What UID is configured on the physical device?'))
16 class PrivacyForm(forms.Form):
17 public = forms.BooleanField(required=False, label=_('May we include your subscription to this podcast in our (anonymous) statistics?'))
19 class SyncForm(forms.Form):
20 targets = forms.CharField()
22 def set_targets(self, sync_targets, label=''):
23 targets = self.sync_target_choices(sync_targets)
24 self.fields['targets'] = forms.ChoiceField(choices=targets, label=label)
26 def sync_target_choices(self, targets):
27 """
28 returns a list of tuples that can be used as choices for a ChoiceField.
29 the first item in each tuple is a letter identifying the type of the
30 sync-target - either d for a Device, or g for a SyncGroup. This letter
31 is followed by the id of the target.
32 The second item in each tuple is the string-representation of the #
33 target.
34 """
35 return [('%s%s' % ('d' if isinstance(t, Device) else 'g', t.id), t) for t in targets]
38 def get_target(self):
39 if not self.is_valid():
40 log('no target given in SyncForm')
41 raise ValueError(_('No device selected'))
43 target = self.cleaned_data['targets']
44 m = re.match('^([dg])(\d+)$', target)
45 if m == None:
46 log('invalid target %s given in SyncForm' % target)
47 raise ValueError(_('Invalid device selected: %s') % target)
49 if m.group(1) == 'd':
50 return Device.objects.get(pk=m.group(2))
51 else:
52 return SyncGroup.objects.get(pk=m.group(2))
54 class ResendActivationForm(forms.Form):
55 username = forms.CharField(max_length=100, label=_('Please enter your username'))