Rename all "offres" to plan and all rejets to unavailable_plans
authorGu1 <gu1@cafai.fr>
Sun, 9 Jun 2013 01:45:15 +0000 (03:45 +0200)
committerGu1 <gu1@cafai.fr>
Sun, 9 Jun 2013 01:55:49 +0000 (03:55 +0200)
fdneligibility/forms.py
fdneligibility/views.py

index 2e52c63..f49ed64 100644 (file)
@@ -32,7 +32,7 @@ class EligibilityForm2(forms.Form):
         super(EligibilityForm2, self).__init__(*args, **kwargs)
 
         def iter_plans():
-            for o in si_res['offres']:
+            for o in si_res['plans']:
                 yield (
                     o['plan_id'],
                     self.format_plan(o)
index 1e20705..af7d09b 100644 (file)
@@ -61,7 +61,7 @@ plans_upload = {
     "18Mopt0"   : "1M",
 }
 
-def test_eligibility(phone_number, zip_code, customize_offer=None, sort_offers=None):
+def test_eligibility(phone_number, zip_code, customize_plan=None, sort_plans=None):
     try:
         si_res=contact_si_helper(phone_number, zip_code)
         if 'info_ligne' not in si_res or 'offres' not in si_res:
@@ -96,37 +96,38 @@ def test_eligibility(phone_number, zip_code, customize_offer=None, sort_offers=N
         except KeyError:
             o['upload'] = u'?'
 
-        if customize_offer:
-            customize_offer(o)
+        if customize_plan:
+            customize_plan(o)
 
-    if sort_offers:
-        si_res['offres']=sort_offers(si_res['offres'])
+    si_res['plans']=sort_plans(si_res['offres']) if sort_plans else si_res['offres']
+    del si_res['offres']
 
-    si_res['rejets']=clean_rejects(si_res['rejets'])
+    si_res['unavailable_plans']=clean_rejects(si_res['rejets'])
+    del si_res['rejets']
 
     return si_res
 
 
+def sort_plans_default(plans):
+    unit_table={'k': 1000, 'm': 1000000, 'g': 1000000000}
+    def _key(o):
+        unit=o['download'][-1].lower()
+        return int(o['download'][:-1])*unit_table[unit]
+    tmp=sorted(plans, key=lambda o: 0 if o['plan_id'].endswith('opt1') or o['plan_id'].endswith('opt0') else 1)
+    return sorted(tmp, key=_key, reverse=True)
+
+
 class EligibilityWizard(SessionWizardView):
 
     @staticmethod
-    def _customize_offer_impl(o):
+    def _customize_plan_impl(o):
         o['abo']=o['abo_fdn']
         o['fas']=o['fas_fdn']
         return o
 
-    customize_offer=_customize_offer_impl
-
-    @staticmethod
-    def _sort_offers_impl(offres):
-        unit_table={'k': 1000, 'm': 1000000, 'g': 1000000000}
-        def _key(o):
-            unit=o['download'][-1].lower()
-            return int(o['download'][:-1])*unit_table[unit]
-        tmp=sorted(offres, key=lambda o: 0 if o['plan_id'].endswith('opt1') or o['plan_id'].endswith('opt0') else 1)
-        return sorted(tmp, key=_key, reverse=True)
+    customize_plan=_customize_plan_impl
 
-    sort_offers=_sort_offers_impl
+    sort_plans=staticmethod(sort_plans_default)
 
     def get_template_names(self):
         return [TEMPLATES[self.steps.current], "fdneligibility/form.html"]
@@ -135,7 +136,7 @@ class EligibilityWizard(SessionWizardView):
         context = super(EligibilityWizard, self).get_context_data(form=form, **kwargs)
         if self.steps.current == '1':
             context.update({
-                'unavailable_plans': self.storage.extra_data['si_res']['rejets'],
+                'unavailable_plans': self.storage.extra_data['si_res']['unavailable_plans'],
                 'phone_number': self.storage.extra_data['phone_number'],
                 'line_info': self.storage.extra_data['si_res']['info_ligne']
             })
@@ -152,7 +153,7 @@ class EligibilityWizard(SessionWizardView):
             phone_number = form.cleaned_data.get('phone_number')
             zip_code     = form.cleaned_data.get('zip_code')
             try:
-                si_res=test_eligibility(phone_number, zip_code, self.customize_offer, self.sort_offers)
+                si_res=test_eligibility(phone_number, zip_code, self.customize_plan, self.sort_plans)
             except RuntimeError as e:
                 form._errors[NON_FIELD_ERRORS] = form.error_class([e.args[0]])
                 return self.render(form)
@@ -164,7 +165,7 @@ class EligibilityWizard(SessionWizardView):
         forms_data=[form.cleaned_data for form in form_list]
         si_res=self.storage.extra_data['si_res']
         _chosen_plan=forms_data[self.steps.step0]['plans']
-        chosen_plan=filter(lambda o: o['plan_id']==_chosen_plan, si_res['offres'])[0]
+        chosen_plan=filter(lambda o: o['plan_id']==_chosen_plan, si_res['plans'])[0]
         return TemplateResponse(self.request, 'fdneligibility/done.html', {
             'form_data': forms_data,
             'line_info': si_res['info_ligne'],