devices can now be synchronized on the website
[mygpo.git] / mygpo / web / forms.py
blob6b23e894422ce988547c15ba40cf3f36e8f5e70c
1 from django import forms
2 from django.utils.translation import ugettext as _
3 from mygpo.api.models import Device, DEVICE_TYPES
5 class UserAccountForm(forms.Form):
6 email = forms.EmailField(label=_('Your Email Address'))
7 public = forms.BooleanField(required=False, label=_('May we use your subscriptions for the toplist and suggestions?'))
9 class DeviceForm(forms.Form):
10 name = forms.CharField(max_length=100, label=_('Name of this device'))
11 type = forms.ChoiceField(choices=DEVICE_TYPES, label=_('What kind of device is this?'))
12 uid = forms.CharField(max_length=50, label=_('What UID is configured on the pysical device?'))
15 class SyncForm(forms.Form):
16 targets = forms.CharField()
18 # def __init__(self, device=None, *args, **kwargs):
19 # super( SyncForm, self ).__init__(*args, **kwargs)
21 # targets = self.sync_target_choices(device.sync_targets())
22 # self.fields['targets'] = forms.ChoiceField(choices=targets, label=_('Synchronize with the following devices'))
24 def set_device(self, device):
25 targets = self.sync_target_choices(device.sync_targets())
26 self.fields['targets'] = forms.ChoiceField(choices=targets, label=_('Synchronize with the following devices'))
28 def sync_target_choices(self, targets):
29 """
30 returns a list of tuples that can be used as choices for a ChoiceField.
31 the first item in each tuple is a letter identifying the type of the
32 sync-target - either d for a Device, or g for a SyncGroup. This letter
33 is followed by the id of the target.
34 The second item in each tuple is the string-representation of the #
35 target.
36 """
37 return [('%s%s' % ('d' if isinstance(t, Device) else 'g', t.id), t) for t in targets]