diff options
| author | Étienne Loks <etienne.loks@proxience.com> | 2014-04-04 10:42:23 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@proxience.com> | 2014-04-04 10:42:23 +0200 | 
| commit | 432538ba84140fecef871a7ff2ea2c1d274bd04e (patch) | |
| tree | 430919a878338620fcfb75e654d40c7dbdae8519 /archaeological_operations/wizards.py | |
| parent | 1879535e1f1ecca799fff5e2439b6d826d60773e (diff) | |
| download | Ishtar-432538ba84140fecef871a7ff2ea2c1d274bd04e.tar.bz2 Ishtar-432538ba84140fecef871a7ff2ea2c1d274bd04e.zip | |
Fix and simplify current file in operations forms (refs #1745)
Diffstat (limited to 'archaeological_operations/wizards.py')
| -rw-r--r-- | archaeological_operations/wizards.py | 72 | 
1 files changed, 26 insertions, 46 deletions
| diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index eb6788ac4..6d117ae43 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -53,15 +53,15 @@ class OperationWizard(Wizard):          return templates      def get_current_file(self): -        if not FILES_AVAILABLE: -            return          step = self.steps.current -        if not step: +        if not FILES_AVAILABLE or not step:              return -        main_form_key = 'general-' + self.url_name +        file_form_key = 'general-' + self.url_name +        if self.url_name == 'operation_creation': +            file_form_key = 'filechoice-' + self.url_name +        file_id = self.session_get_value(file_form_key, "associated_file")          try: -            idx = int(self.session_get_value(main_form_key, -                                             'associated_file')) +            idx = int(file_id)              current_file = File.objects.get(pk=idx)              return current_file          except(TypeError, ValueError, ObjectDoesNotExist): @@ -81,7 +81,7 @@ class OperationWizard(Wizard):              context['add_all'] = True          # reminder of the current file          archaeological_file = self.get_current_file() -        if not archaeological_file or self.steps.current.startswith('general-'): +        if not archaeological_file:              return context          context['reminders'] = ((_("Archaelogical file"),                                   unicode(archaeological_file)),) @@ -89,32 +89,25 @@ class OperationWizard(Wizard):      def get_towns(self):          """ -        Obtention des villes disponibles +        Get available towns          """          if not FILES_AVAILABLE:              return -1 -        file_form_key = 'general-' + self.url_name -        if self.url_name == 'operation_creation': -            file_form_key = 'filechoice-' + self.url_name          towns = [] -        file_id = self.session_get_value(file_form_key, "associated_file") -        if file_id: -            try: -                for town in File.objects.get(pk=int(file_id) -                                                    ).towns.all(): -                    towns.append((town.pk, unicode(town))) -            except (ValueError, ObjectDoesNotExist): -                pass -            return sorted(towns, key=lambda x:x[1]) -        else: +        file = self.get_current_file() +        if not file:              return -1 +        try: +            towns = [(town.pk, unicode(town)) for town in file.towns.all()] +        except (ValueError, ObjectDoesNotExist): +            pass +        return sorted(towns, key=lambda x:x[1]) -    def get_available_parcels(self, file_id): +    def get_available_parcels(self, file):          parcels = []          try: -            for parcel in File.objects.get(pk=int(file_id) -                                                ).parcels.all(): -                parcels.append((parcel.pk, parcel.short_label)) +            parcels = [(parcel.pk, parcel.short_label) +                        for parcel in file.parcels.all()]          except (ValueError, ObjectDoesNotExist):              pass          return sorted(parcels, key=lambda x:x[1]) @@ -131,16 +124,13 @@ class OperationWizard(Wizard):              #step = self.determine_step(request, storage)              step = self.steps.current          form = self.get_form_list()[step] -        file_form_key = 'general-' + self.url_name -        if self.url_name == 'operation_creation': -            file_form_key = 'filechoice-' + self.url_name          # manage the dynamic choice of towns          if step.startswith('towns') and hasattr(form, 'management_form'):              data['TOWNS'] = self.get_towns()          elif step.startswith('parcels') and hasattr(form, 'management_form'): -            file_id = self.session_get_value(file_form_key, "associated_file") -            if file_id: -                data['PARCELS'] = self.get_available_parcels(file_id) +            file = self.get_current_file() +            if file: +                data['PARCELS'] = self.get_available_parcels(file)              else:                  town_form_key = step.startswith('parcelsgeneral') \                                  and 'townsgeneral-' or 'towns-' @@ -196,15 +186,9 @@ class OperationWizard(Wizard):      def _copy_from_associated_field(self):          initial = {} -        file_form_key = 'filechoice-' + self.url_name -        file_id = self.session_get_value(file_form_key, -                                         "associated_file") -        if not file_id: +        file = self.get_current_file() +        if not file:              return initial -        try: -            file = File.objects.get(pk=file_id) -        except File.DoesNotExist: -            return          keys = ((('in_charge', 'pk'), 'in_charge'),                  (('name',), 'common_name'),                  (('total_surface',), 'surface'), @@ -227,12 +211,8 @@ class OperationWizard(Wizard):          if not post_data.get('add_all_parcels'):              return super(OperationWizard, self).post(*args, **kwargs) -        file_form_key = 'general-' + self.url_name -        if self.url_name == 'operation_creation': -            file_form_key = 'filechoice-' + self.url_name -        file_id = self.session_get_value(file_form_key, -                                         "associated_file") -        if not file_id: +        file = self.get_current_file() +        if not file:              return super(OperationWizard, self).post(*args, **kwargs)          parcel_form_key = "parcels-" + self.url_name          idx = -1 @@ -240,7 +220,7 @@ class OperationWizard(Wizard):          for k in post_data.keys():              if k.startswith(parcel_form_key) and k.endswith('-DELETE'):                  post_data.pop(k) -        for idx, parcel in enumerate(self.get_available_parcels(file_id)): +        for idx, parcel in enumerate(self.get_available_parcels(file)):              parcel_pk, parcel_name = parcel              post_data["%s-%d-parcel" % (parcel_form_key, idx)] = parcel_pk          post_data[parcel_form_key+'-TOTAL_FORMS'] = idx + 1 | 
