FAS
[django-fdn-eligibility.git] / fdneligibility / forms.py
1 # -*- coding: utf-8 -*-
2 from django import forms
3 from django.utils.translation import ugettext_lazy as _
4 from django.utils.safestring import mark_safe
5 from django.utils.html import escape
6 from django_localflavor_fr.forms import FRPhoneNumberField, FRZipCodeField
7
8
9
10 class EligibilityForm1(forms.Form):
11     phone_number = FRPhoneNumberField(label=_(u'Phone number'), required=True)
12
13     zip_code     = FRZipCodeField(label=_(u'Zip code'), required=True)
14
15     def clean(self):
16         cleaned_data = super(EligibilityForm1, self).clean()
17         return cleaned_data
18
19
20 class EligibilityForm2(forms.Form):
21     plans = forms.ChoiceField(label=_('Plans'), widget=forms.RadioSelect, error_messages={'required': _('Please choose a plan')})
22
23     def format_plan(self, o):
24         if 'abo' in o and 'fas' in o:
25             plan=_(u"<strong>%(download)s</strong>/<strong>%(upload)s</strong>, %(unbundling)s %(abo)s€/month <small>(access to the service fees: %(fas)s€)</small>") % o
26         else:
27             plan=_(u"<strong>%(download)s</strong>/<strong>%(upload)s</strong>, %(unbundling)s") % o
28         return mark_safe(plan)
29
30     def __init__(self, *args, **kwargs):
31         si_res=kwargs.pop('si_res')
32         super(EligibilityForm2, self).__init__(*args, **kwargs)
33
34         def iter_plans():
35             for o in si_res['offres']:
36                 yield (
37                     o['plan_id'],
38                     self.format_plan(o)
39                 )
40
41         self.fields['plans'].choices=iter_plans()