diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2015-08-18 15:07:58 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-08-18 15:07:58 +0200 |
commit | 601bcd199a5078ad951f3bce77edc0f1f940a790 (patch) | |
tree | cd116e3bfaab2167f5f63ddc350eb55b678acf23 | |
parent | b158c7375e7cf025b9a3f11cc6978b8a6c6f45b6 (diff) | |
download | Ishtar-601bcd199a5078ad951f3bce77edc0f1f940a790.tar.bz2 Ishtar-601bcd199a5078ad951f3bce77edc0f1f940a790.zip |
Archaeological files: associate many towns. PEP8 and formating.
-rw-r--r-- | archaeological_files/views.py | 24 | ||||
-rw-r--r-- | archaeological_files/wizards.py | 68 | ||||
-rw-r--r-- | archaeological_files_pdl/forms.py | 17 | ||||
-rw-r--r-- | archaeological_files_pdl/templates/ishtar/wizard/wizard_preventiveplanning.html | 5 | ||||
-rw-r--r-- | archaeological_files_pdl/wizards.py | 24 | ||||
-rw-r--r-- | archaeological_operations/forms.py | 43 | ||||
-rw-r--r-- | archaeological_operations/views.py | 7 | ||||
-rw-r--r-- | archaeological_operations/wizards.py | 98 | ||||
-rw-r--r-- | ishtar_common/wizards.py | 32 |
9 files changed, 168 insertions, 150 deletions
diff --git a/archaeological_files/views.py b/archaeological_files/views.py index 942ee6ae3..fd0507295 100644 --- a/archaeological_files/views.py +++ b/archaeological_files/views.py @@ -17,7 +17,8 @@ # See the file COPYING for details. -import json, re +import json +import re from django.core.urlresolvers import reverse from django.db.models import Q @@ -32,17 +33,17 @@ from archaeological_operations.models import Operation import models from ishtar_common.wizards import SearchWizard -from archaeological_operations.wizards import is_preventive, is_not_preventive +from archaeological_operations.wizards import AdministrativeActDeletionWizard, \ + is_preventive, is_not_preventive from wizards import * from ishtar_common.forms_common import TownFormset -from archaeological_operations.forms import ParcelFormSet, \ - DocumentGenerationAdminActForm +from archaeological_operations.forms import ParcelFormSet from forms import * -from archaeological_operations.views import generatedoc_administrativeactop -RE_YEAR_INDEX = re.compile(r"([1-2][0-9]{3})-([0-9]+)") # eg.: 2014-123 +RE_YEAR_INDEX = re.compile(r"([1-2][0-9]{3})-([0-9]+)") # eg.: 2014-123 + def autocomplete_file(request): if not request.user.has_perm('ishtar_common.view_file', models.File) and \ @@ -55,10 +56,10 @@ def autocomplete_file(request): query = Q() for q in q.split(' '): extra = Q(internal_reference__icontains=q) | \ - Q(towns__name__icontains=q) | \ - Q(address__icontains=q) + Q(towns__name__icontains=q) | \ + Q(address__icontains=q) try: - value = int(q) + int(q) extra = extra | Q(year=q) | Q(numeric_reference=q) except ValueError: pass @@ -69,8 +70,8 @@ def autocomplete_file(request): query = query & extra limit = 20 files = models.File.objects.filter(query)[:limit] - data = json.dumps([{'id':file.pk, 'value':unicode(file)} - for file in files]) + data = json.dumps([{'id': file.pk, 'value': unicode(file)} + for file in files]) return HttpResponse(data, mimetype='text/plain') get_file = get_item(models.File, 'get_file', 'file', @@ -88,6 +89,7 @@ get_file = get_item(models.File, 'get_file', 'file', },) revert_file = revert_item(models.File) + def extra_file_dct(request, item): dct = {} if (request.user.has_perm('ishtar_common.add_operation', Operation) diff --git a/archaeological_files/wizards.py b/archaeological_files/wizards.py index 00949b8db..7fac15c66 100644 --- a/archaeological_files/wizards.py +++ b/archaeological_files/wizards.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2012-2014 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2012-2015 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -25,15 +25,14 @@ from django.template import RequestContext from django.utils.translation import ugettext_lazy as _ from ishtar_common.forms import reverse_lazy -from ishtar_common.wizards import Wizard, ClosingWizard +from ishtar_common.wizards import ClosingWizard from archaeological_operations.wizards import OperationWizard,\ - OperationAdministrativeActWizard,\ - AdministrativeActDeletionWizard -from ishtar_common.models import Town + OperationAdministrativeActWizard from archaeological_operations.models import AdministrativeAct, Parcel, \ - Operation + Operation import models + class FileWizard(OperationWizard): model = models.File object_parcel_type = 'associated_file' @@ -41,46 +40,11 @@ class FileWizard(OperationWizard): town_step_keys = ['towns-'] wizard_done_window = reverse_lazy('show-file') - ''' - def get_form(self, step=None, data=None, files=None): - """ - Manage towns - """ - if data: - data = data.copy() - else: - data = {} - # manage the dynamic choice of towns - if not step: - # step = self.determine_step(request, storage) - step = self.steps.current - form = self.get_form_list()[step] - town_form_key = self.town_step_key - town_form_key += self.url_name - if step.startswith(self.parcel_step_key) \ - and hasattr(form, 'management_form') \ - and self.session_has_key(town_form_key): - towns = [] - qdict = self.request.session[self.storage.prefix]['step_data']\ - [town_form_key] - for k in qdict.keys(): - if k.endswith("town") and qdict[k]: - try: - town = Town.objects.get(pk=int(qdict[k][0])) - towns.append((town.pk, unicode(town))) - except (ObjectDoesNotExist, ValueError): - pass - data['TOWNS'] = sorted(towns, key=lambda x:x[1]) - form = super(FileWizard, self).get_form(step, data, files) - """ - return form - ''' - def get_extra_model(self, dct, form_list): dct = super(FileWizard, self).get_extra_model(dct, form_list) if not dct.get('numeric_reference'): - current_ref = models.File.objects.filter(year=dct['year'] - ).aggregate(Max('numeric_reference'))["numeric_reference__max"] + current_ref = models.File.objects.filter(year=dct['year'])\ + .aggregate(Max('numeric_reference'))["numeric_reference__max"] dct['numeric_reference'] = current_ref and current_ref + 1 or 1 return dct @@ -126,9 +90,11 @@ class FileWizard(OperationWizard): parcel.save() return res + class FileModificationWizard(FileWizard): modification = True + class FileClosingWizard(ClosingWizard): model = models.File fields = ['year', 'numeric_reference', 'internal_reference', @@ -139,12 +105,14 @@ class FileClosingWizard(ClosingWizard): fields += ['saisine_type', 'reference_number'] fields += ['towns'] + class FileDeletionWizard(FileClosingWizard): def get_formated_datas(self, forms): datas = super(FileDeletionWizard, self).get_formated_datas(forms) datas.append((_("Associated operations"), [])) for operation in Operation.objects.filter( - associated_file=self.current_obj).all(): + associated_file=self.current_obj).all(): + if operation.end_date: datas[-1][1].append(('', unicode(operation))) return datas @@ -154,8 +122,9 @@ class FileDeletionWizard(FileClosingWizard): for operation in Operation.objects.filter(associated_file=obj).all(): operation.delete() obj.delete() - return render_to_response('ishtar/wizard/wizard_delete_done.html', {}, - context_instance=RequestContext(self.request)) + return render_to_response( + 'ishtar/wizard/wizard_delete_done.html', {}, + context_instance=RequestContext(self.request)) class FileAdministrativeActWizard(OperationAdministrativeActWizard): @@ -168,8 +137,9 @@ class FileAdministrativeActWizard(OperationAdministrativeActWizard): # and '_deletion' so it is creation file_id = self.session_get_value(form_key, "pk") try: - return ((_(u"Archaelogical file"), - unicode(models.File.objects.get(pk=file_id))),) + return ( + (_(u"Archaelogical file"), + unicode(models.File.objects.get(pk=file_id))),) except models.File.DoesNotExist: return else: @@ -183,8 +153,10 @@ class FileAdministrativeActWizard(OperationAdministrativeActWizard): except AdministrativeAct.DoesNotExist: return + class FileEditAdministrativeActWizard(FileAdministrativeActWizard): model = AdministrativeAct edit = True + def get_associated_item(self, dct): return self.get_current_object().associated_file diff --git a/archaeological_files_pdl/forms.py b/archaeological_files_pdl/forms.py index 03c308ba5..393f53b8c 100644 --- a/archaeological_files_pdl/forms.py +++ b/archaeological_files_pdl/forms.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2014 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2014-2015 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -24,11 +24,10 @@ from django.core import validators from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ -from ishtar_common.models import Person, PersonType, valid_id +from ishtar_common.models import Person, PersonType, Town, valid_id from archaeological_files import models from ishtar_common.forms import get_now, reverse_lazy -from ishtar_common.forms_common import get_advanced_town_field from archaeological_files.forms import GENERAL_CONTRACTOR, \ GENERAL_CONTRACTOR_ORGA, RESPONSIBLE_PLANNING_SERVICE, \ RESPONSIBLE_PLANNING_SERVICE_ORGA @@ -75,10 +74,12 @@ class FileFormPreventiveType(forms.Form): class FileFormPlanning(forms.Form): form_label = _(u"Planning") - associated_models = {'main_town': models.Town} + base_model = 'town' + associated_models = {'town': Town} name = forms.CharField(label=_(u"Planning name"), required=False, max_length=100) - main_town = get_advanced_town_field(required=True) + town = widgets.MultipleAutocompleteField( + model=Town, label=_("Towns"), required=False) locality = forms.CharField(label=_(u"Locality"), max_length=100, required=False) address = forms.CharField( @@ -103,8 +104,10 @@ class FileFormPlanning(forms.Form): class FileFormResearchAddress(forms.Form): form_label = _(u"Address") - associated_models = {'main_town': models.Town} - main_town = get_advanced_town_field(required=True) + base_model = 'town' + associated_models = {'town': Town} + town = widgets.MultipleAutocompleteField( + model=Town, label=_("Towns"), required=False) locality = forms.CharField(label=_(u"Locality"), max_length=100, required=False) address = forms.CharField( diff --git a/archaeological_files_pdl/templates/ishtar/wizard/wizard_preventiveplanning.html b/archaeological_files_pdl/templates/ishtar/wizard/wizard_preventiveplanning.html index 66569a66d..6bd5598b6 100644 --- a/archaeological_files_pdl/templates/ishtar/wizard/wizard_preventiveplanning.html +++ b/archaeological_files_pdl/templates/ishtar/wizard/wizard_preventiveplanning.html @@ -25,7 +25,10 @@ </tr>{% endif %} <tr> - <td>{{wizard.form.main_town|safe}}</td> + <th colspan='2'><label for="id_preventiveplanning-{{CURRENT_ACTION}}-towns">{% trans "Towns" %}</label></th> +</tr> +<tr> + <td>{{wizard.form.town|safe}}</td> </tr> <tr> diff --git a/archaeological_files_pdl/wizards.py b/archaeological_files_pdl/wizards.py index 2d3491c8d..c081d13d1 100644 --- a/archaeological_files_pdl/wizards.py +++ b/archaeological_files_pdl/wizards.py @@ -19,21 +19,22 @@ from archaeological_files.wizards import FileWizard as BaseFileWizard + class FileWizard(BaseFileWizard): parcel_step_key = 'parcelspdl-' town_step_keys = ['preventiveplanning-', 'researchaddress-'] - town_input_id = 'main_town' + town_input_id = 'town' multi_towns = False wizard_templates = { - 'generalcontractor-%(url_name)s':\ - 'ishtar/wizard/wizard_generalcontractor.html', - 'planningservice-%(url_name)s':\ - 'ishtar/wizard/wizard_planningservice.html', - 'instruction-%(url_name)s':\ - 'ishtar/wizard/wizard_instruction.html', - 'preventiveplanning-%(url_name)s':\ - 'ishtar/wizard/wizard_preventiveplanning.html', - } + 'generalcontractor-%(url_name)s': + 'ishtar/wizard/wizard_generalcontractor.html', + 'planningservice-%(url_name)s': + 'ishtar/wizard/wizard_planningservice.html', + 'instruction-%(url_name)s': + 'ishtar/wizard/wizard_instruction.html', + 'preventiveplanning-%(url_name)s': + 'ishtar/wizard/wizard_preventiveplanning.html', + } def get_current_year(self): general_form_key = 'general-' + self.url_name @@ -41,7 +42,7 @@ class FileWizard(BaseFileWizard): def get_form_kwargs(self, *args, **kwargs): returned = super(FileWizard, self).get_form_kwargs(*args, **kwargs) - if args and (args[0].startswith('generalcontractor-') or + if args and (args[0].startswith('generalcontractor-') or args[0].startswith('planningservice-')): if 'status' in self.request.GET: returned['status'] = self.request.GET['status'] @@ -49,5 +50,6 @@ class FileWizard(BaseFileWizard): returned['year'] = self.get_current_year() return returned + class FileModificationWizard(FileWizard): modification = True diff --git a/archaeological_operations/forms.py b/archaeological_operations/forms.py index 691b0c90c..b1f2e053b 100644 --- a/archaeological_operations/forms.py +++ b/archaeological_operations/forms.py @@ -26,16 +26,13 @@ from itertools import groupby from django import forms from django.conf import settings from django.core import validators -from django.core.exceptions import ObjectDoesNotExist from django.db.models import Max from django.forms.formsets import DELETION_FIELD_NAME, TOTAL_FORM_COUNT -from django.shortcuts import render_to_response -from django.template import RequestContext from django.utils.translation import ugettext_lazy as _, pgettext_lazy from django.utils.safestring import mark_safe from ishtar_common.models import valid_id, PersonType, Person, Town, \ - DocumentTemplate, Organization, OrganizationType + DocumentTemplate, Organization, OrganizationType from ishtar_common.wizards import MultiValueDict @@ -48,15 +45,14 @@ import models from widgets import ParcelWidget, SelectParcelWidget from ishtar_common import widgets -from ishtar_common.forms import BaseFormSet, FinalForm, FormSet, \ - ClosingDateFormSelection, formset_factory, get_now, reverse_lazy, \ - get_form_selection, TableSelect -from ishtar_common.forms_common import TownForm, TownFormSet, TownFormset, \ - AuthorFormset, SourceForm, SourceSelect, \ - SourceDeletionForm, get_town_field +from ishtar_common.forms import FinalForm, FormSet, formset_factory, get_now, \ + reverse_lazy, get_form_selection, TableSelect +from ishtar_common.forms_common import TownFormSet, SourceForm, SourceSelect, \ + get_town_field from archaeological_operations.utils import parse_parcels + class ParcelField(forms.MultiValueField): def __init__(self, *args, **kwargs): if 'widget' not in kwargs: @@ -66,19 +62,22 @@ class ParcelField(forms.MultiValueField): def compress(data_list): return u"-".join(data_list) + class ParcelForm(forms.Form): form_label = _("Parcels") base_model = 'parcel' - associated_models = {'parcel':models.Parcel, 'town':models.Town,} + associated_models = {'parcel': models.Parcel, 'town': models.Town, } town = forms.ChoiceField(label=_("Town"), choices=(), required=False, validators=[valid_id(models.Town)]) year = forms.IntegerField(label=_("Year"), required=False, - validators=[validators.MinValueValidator(1900), - validators.MaxValueValidator(2100)]) + validators=[validators.MinValueValidator(1900), + validators.MaxValueValidator(2100)]) section = forms.CharField(label=_(u"Section"), required=False, - validators=[validators.MaxLengthValidator(4)]) - parcel_number = forms.CharField(label=_(u"Parcel number"), required=False, - validators=[validators.MaxLengthValidator(6)]) + validators=[validators.MaxLengthValidator(4)]) + parcel_number = forms.CharField( + label=_(u"Parcel number"), required=False, + validators=[validators.MaxLengthValidator(6)]) + def __init__(self, *args, **kwargs): towns = None if 'data' in kwargs and 'TOWNS' in kwargs['data']: @@ -524,12 +523,12 @@ class DashboardForm(forms.Form): class OperationFormGeneral(forms.Form): form_label = _(u"General") base_model = 'archaeological_site' - associated_models = {'scientist':Person, - 'in_charge':Person, - 'cira_rapporteur':Person, - 'operator':Organization, - 'operation_type':models.OperationType, - 'archaeological_site':models.ArchaeologicalSite} + associated_models = {'scientist': Person, + 'in_charge': Person, + 'cira_rapporteur': Person, + 'operator': Organization, + 'operation_type': models.OperationType, + 'archaeological_site': models.ArchaeologicalSite} pk = forms.IntegerField(required=False, widget=forms.HiddenInput) scientist = forms.IntegerField(label=_("Head scientist"), widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person', diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py index a8ded32cf..5e7288caa 100644 --- a/archaeological_operations/views.py +++ b/archaeological_operations/views.py @@ -18,21 +18,23 @@ # See the file COPYING for details. import json -import os from django.core.urlresolvers import reverse from django.db.models import Q from django.http import HttpResponse from django.shortcuts import render_to_response, redirect -from django.template.defaultfilters import slugify from django.utils.translation import ugettext_lazy as _, pgettext_lazy from ishtar_common.views import get_item, show_item, revert_item, new_item from ishtar_common.wizards import SearchWizard +from ishtar_common.forms import ClosingDateFormSelection +from ishtar_common.forms_common import AuthorFormset, TownFormset, \ + SourceDeletionForm from wizards import * from forms import * import models + def autocomplete_patriarche(request, non_closed=True): if (not request.user.has_perm('ishtar_common.view_operation', models.Operation) @@ -56,6 +58,7 @@ def autocomplete_patriarche(request, non_closed=True): for operation in operations]) return HttpResponse(data, mimetype='text/plain') + def autocomplete_archaeologicalsite(request): if (not request.user.has_perm( 'archaeological_operations.view_archaeologicalsite', diff --git a/archaeological_operations/wizards.py b/archaeological_operations/wizards.py index 9e6b9d03a..644bc7390 100644 --- a/archaeological_operations/wizards.py +++ b/archaeological_operations/wizards.py @@ -27,30 +27,28 @@ from django.utils.translation import ugettext_lazy as _ from ishtar_common.forms import reverse_lazy from ishtar_common.wizards import Wizard, ClosingWizard, DeletionWizard, \ - SourceWizard + SourceWizard import models -from forms import ParcelForm, GenerateDocForm +from forms import GenerateDocForm FILES_AVAILABLE = 'archaeological_files' in settings.INSTALLED_APPS if FILES_AVAILABLE: from archaeological_files.models import File + class OperationWizard(Wizard): model = models.Operation object_parcel_type = 'operation' parcel_step_key = 'parcelsgeneral-' # step contening the current(s) town(s) town_step_keys = ['towns-', 'townsgeneral-'] - town_input_id = 'town' # input id of the current(s) town(s) - multi_towns = True # true if current town are multi + town_input_id = 'town' # input id of the current(s) town(s) + multi_towns = True # true if current town are multi wizard_done_window = reverse_lazy('show-operation') def get_template_names(self): templates = super(OperationWizard, self).get_template_names() current_step = self.steps.current - if current_step.startswith('towns-'): - #templates = ['ishtar/wizard/towns_wizard.html'] + templates - pass if current_step.startswith(self.parcel_step_key): templates = ['ishtar/wizard/parcels_wizard.html'] + templates return templates @@ -88,10 +86,10 @@ class OperationWizard(Wizard): elif step.startswith('parcels-') and self.get_current_file(): # if a file is acciated to the operation add the button "Add all" context['add_all'] = True - if step.startswith('parcels') and\ + if step.startswith('parcels') and \ hasattr(self, 'automatic_parcel_association'): context['automatic_parcel_association'] = \ - self.automatic_parcel_association + self.automatic_parcel_association # reminder of the current file reminder = self.get_reminder() if reminder: @@ -112,7 +110,7 @@ class OperationWizard(Wizard): towns = [(town.pk, unicode(town)) for town in file.towns.all()] except (ValueError, ObjectDoesNotExist): pass - return sorted(towns, key=lambda x:x[1]) + return sorted(towns, key=lambda x: x[1]) def get_available_parcels(self, file): self.automatic_parcel_association = False @@ -136,7 +134,7 @@ class OperationWizard(Wizard): self.automatic_parcel_association = True except (ValueError, ObjectDoesNotExist): pass - return sorted(parcels, key=lambda x:x[1]) + return sorted(parcels, key=lambda x: x[1]) def get_form(self, step=None, data=None, files=None): """ @@ -147,14 +145,13 @@ class OperationWizard(Wizard): else: data = {} if not step: - #step = self.determine_step(request, storage) step = self.steps.current form = self.get_form_list()[step] # manage the dynamic choice of towns if step.startswith('towns') and hasattr(form, 'management_form'): data['TOWNS'] = self.get_towns() elif step.startswith(self.parcel_step_key) \ - and hasattr(form, 'management_form'): + and hasattr(form, 'management_form'): file = self.get_current_file() if file: data['PARCELS'] = self.get_available_parcels(file) @@ -162,12 +159,13 @@ class OperationWizard(Wizard): town_ids = [] for town_step_key in self.town_step_keys: town_form_key = town_step_key + self.url_name - town_ids = self.session_get_value(town_form_key, - self.town_input_id, - multi=self.multi_towns) or [] - if town_ids: + town_ids = self.session_get_value( + town_form_key, self.town_input_id, + multi=self.multi_towns) or [] + if town_ids and type(town_ids) == unicode: + town_ids = town_ids.split(',') break - if not self.multi_towns and town_ids: + if type(town_ids) not in (list, tuple): town_ids = [town_ids] towns = [] for town_id in town_ids: @@ -176,7 +174,7 @@ class OperationWizard(Wizard): towns.append((town.pk, unicode(town))) except (ValueError, ObjectDoesNotExist): pass - data['TOWNS'] = sorted(towns, key=lambda x:x[1]) + data['TOWNS'] = sorted(towns, key=lambda x: x[1]) data = data or None form = super(OperationWizard, self).get_form(step, data, files) return form @@ -188,7 +186,7 @@ class OperationWizard(Wizard): datas = super(OperationWizard, self).get_formated_datas(forms) # if the general town form is used the advertissement is relevant has_no_af = [form.prefix for form in forms - if form.prefix == 'townsgeneral-operation'] and True + if form.prefix == 'townsgeneral-operation'] and True if has_no_af: datas = [[_(u"Warning: No Archaelogical File is provided. " u"If you have forget it return to the first step."), []]]\ @@ -259,21 +257,26 @@ class OperationWizard(Wizard): request.POST = post_data return super(OperationWizard, self).post(*args, **kwargs) + class OperationModificationWizard(OperationWizard): modification = True + class OperationClosingWizard(ClosingWizard): model = models.Operation fields = ['year', 'operation_code', 'operation_type', 'associated_file', 'in_charge', 'scientist', 'start_date', 'excavation_end_date', 'comment', 'towns', 'remains'] + class OperationDeletionWizard(DeletionWizard): model = models.Operation fields = OperationClosingWizard.fields + class OperationSourceWizard(SourceWizard): model = models.OperationSource + def get_form_initial(self, step, data=None): initial = super(OperationSourceWizard, self).get_form_initial(step) # put default index and operation_id field in the main source form @@ -297,14 +300,16 @@ class OperationSourceWizard(SourceWizard): initial['hidden_operation_id'] = operation_id if 'index' not in initial: max_val = models.OperationSource.objects.filter( - operation__pk=operation_id).aggregate( - Max('index'))["index__max"] + operation__pk=operation_id).aggregate( + Max('index'))["index__max"] initial['index'] = max_val and (max_val + 1) or 1 return initial + class OperationSourceDeletionWizard(DeletionWizard): model = models.OperationSource - fields = ['operation', 'title', 'source_type', 'authors',] + fields = ['operation', 'title', 'source_type', 'authors'] + class OperationAdministrativeActWizard(OperationWizard): edit = False @@ -317,8 +322,10 @@ class OperationAdministrativeActWizard(OperationWizard): # and '_deletion' so it is creation operation_id = self.session_get_value(form_key, "pk") try: - return ((_(u"Operation"), - unicode(models.Operation.objects.get(pk=operation_id))),) + return ( + (_(u"Operation"), + unicode(models.Operation.objects.get(pk=operation_id))), + ) except models.Operation.DoesNotExist: return else: @@ -337,8 +344,8 @@ class OperationAdministrativeActWizard(OperationWizard): def get_context_data(self, form, **kwargs): # manage document generation - context = super(OperationAdministrativeActWizard, self - ).get_context_data(form, **kwargs) + context = super(OperationAdministrativeActWizard, + self).get_context_data(form, **kwargs) step = self.steps.current if step.startswith('final-'): general_form_key = 'administrativeact-' + self.url_name @@ -349,7 +356,7 @@ class OperationAdministrativeActWizard(OperationWizard): pass if act_type and act_type.associated_template.count(): context['extra_form'] = GenerateDocForm( - choices=act_type.associated_template.all()) + choices=act_type.associated_template.all()) return context def get_associated_item(self, dct): @@ -386,7 +393,7 @@ class OperationAdministrativeActWizard(OperationWizard): r = r[k] else: break - if k == keys[-1]: # the whole list as been traversed + if k == keys[-1]: # the whole list as been traversed wizard_done_window = unicode(self.wizard_done_window) if wizard_done_window: dct['wizard_done_window'] = wizard_done_window @@ -399,29 +406,34 @@ class OperationAdministrativeActWizard(OperationWizard): context_instance=RequestContext(self.request)) return res + class OperationEditAdministrativeActWizard(OperationAdministrativeActWizard): model = models.AdministrativeAct edit = True + def get_associated_item(self, dct): return self.get_current_object().operation + class AdministrativeActDeletionWizard(ClosingWizard): wizard_templates = { - 'final-operation_administrativeactop_deletion':\ - 'ishtar/wizard/wizard_adminact_deletion.html', - 'final-file_administrativeactfile_deletion':\ - 'ishtar/wizard/wizard_adminact_deletion.html'} + 'final-operation_administrativeactop_deletion': + 'ishtar/wizard/wizard_adminact_deletion.html', + 'final-file_administrativeactfile_deletion': + 'ishtar/wizard/wizard_adminact_deletion.html'} model = models.AdministrativeAct fields = ['act_type', 'in_charge', 'operator', 'scientist', 'signatory', - 'operation', 'associated_file', 'signature_date', 'act_object',] + 'operation', 'associated_file', 'signature_date', 'act_object'] if settings.COUNTRY == 'fr': fields += ['ref_sra'] def done(self, form_list, **kwargs): obj = self.get_current_object() obj.delete() - return render_to_response('ishtar/wizard/wizard_delete_done.html', {}, - context_instance=RequestContext(self.request)) + return render_to_response( + 'ishtar/wizard/wizard_delete_done.html', {}, + context_instance=RequestContext(self.request)) + def is_preventive(form_name, model, type_key='operation_type', key=''): def func(self): @@ -431,11 +443,11 @@ def is_preventive(form_name, model, type_key='operation_type', key=''): 'step_data' not in request.session[storage.prefix] or \ form_name not in request.session[storage.prefix]['step_data'] or\ form_name + '-' + type_key not in \ - request.session[storage.prefix]['step_data'][form_name]: + request.session[storage.prefix]['step_data'][form_name]: return False try: - typ = request.session[storage.prefix]['step_data']\ - [form_name][form_name+'-'+type_key] + typ = request.session[storage.prefix][ + 'step_data'][form_name][form_name+'-'+type_key] if type(typ) in (list, tuple): typ = typ[0] typ = int(typ) @@ -444,11 +456,13 @@ def is_preventive(form_name, model, type_key='operation_type', key=''): return False return func + def is_not_preventive(form_name, model, type_key='operation_type', key=''): def func(self): return not is_preventive(form_name, model, type_key, key)(self) return func + def has_associated_file(form_name, file_key='associated_file', negate=False): def func(self): request = self.request @@ -457,11 +471,11 @@ def has_associated_file(form_name, file_key='associated_file', negate=False): 'step_data' not in request.session[storage.prefix] or \ form_name not in request.session[storage.prefix]['step_data'] or\ form_name + '-' + file_key not in \ - request.session[storage.prefix]['step_data'][form_name]: + request.session[storage.prefix]['step_data'][form_name]: return negate try: - file_id = request.session[storage.prefix]['step_data']\ - [form_name][form_name+'-'+file_key] + file_id = request.session[storage.prefix][ + 'step_data'][form_name][form_name+'-'+file_key] if type(file_id) in (list, tuple): file_id = file_id[0] file_id = int(file_id) diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index e30d33711..c01ef5301 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2010-2014 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2010-2015 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -22,7 +22,7 @@ import datetime from django.conf import settings from django.contrib.formtools.wizard.storage import get_storage from django.contrib.formtools.wizard.views import NamedUrlWizardView, \ - normalize_name + normalize_name from django.core.exceptions import ObjectDoesNotExist from django.core.files.images import ImageFile from django.db.models.fields.files import FileField @@ -34,6 +34,7 @@ from django.utils.datastructures import MultiValueDict as BaseMultiValueDict from django.utils.translation import ugettext_lazy as _ import models + class MultiValueDict(BaseMultiValueDict): def get(self, *args, **kwargs): v = super(MultiValueDict, self).getlist(*args, **kwargs) @@ -45,6 +46,7 @@ class MultiValueDict(BaseMultiValueDict): v = super(MultiValueDict, self).get(*args, **kwargs) return v + class Wizard(NamedUrlWizardView): model = None label = '' @@ -614,13 +616,18 @@ class Wizard(NamedUrlWizardView): request = self.request storage = self.storage test = storage.prefix in request.session \ - and 'step_data' in request.session[storage.prefix] \ - and form_key in request.session[storage.prefix]['step_data'] + and 'step_data' in request.session[storage.prefix] \ + and form_key in request.session[storage.prefix]['step_data'] if not key or not test: return test + if multi: + # only check if the first field is available + key = key.startswith(form_key) and key or \ + form_key + '-0-' + key + if key in request.session[storage.prefix]['step_data'][form_key]: + return True key = key.startswith(form_key) and key or \ - not multi and form_key + '-' + key or \ - form_key + '-0-' + key #only check if the first field is available + form_key + '-' + key return key in request.session[storage.prefix]['step_data'][form_key] @classmethod @@ -646,7 +653,9 @@ class Wizard(NamedUrlWizardView): def session_get_value(self, form_key, key, multi=False): """Get the value of a specific form""" + print(form_key, key, multi) if not self.session_has_key(form_key, key, multi): + print('OUT!') return request = self.request storage = self.storage @@ -786,6 +795,7 @@ class Wizard(NamedUrlWizardView): initial.append(vals) return initial + class SearchWizard(NamedUrlWizardView): model = None label = '' @@ -816,6 +826,7 @@ class SearchWizard(NamedUrlWizardView): 'wizard_label':self.label}) return context + class DeletionWizard(Wizard): def __init__(self, *args, **kwargs): if (not hasattr(self, 'fields') or not self.fields) and \ @@ -865,6 +876,7 @@ class DeletionWizard(Wizard): return render_to_response('ishtar/wizard/wizard_delete_done.html', {}, context_instance=RequestContext(self.request)) + class ClosingWizard(Wizard): # "close" an item # to be define in the overloaded class @@ -915,24 +927,30 @@ class ClosingWizard(Wizard): return render_to_response('ishtar/wizard/wizard_closing_done.html', {}, context_instance=RequestContext(self.request)) + class PersonWizard(Wizard): model = models.Person + class PersonModifWizard(PersonWizard): modification = True + class PersonDeletionWizard(DeletionWizard): model = models.Person fields = model.TABLE_COLS wizard_templates = { 'final-person_deletion':'ishtar/wizard/wizard_person_deletion.html'} + class OrganizationWizard(Wizard): model = models.Organization + class OrganizationModifWizard(OrganizationWizard): modification = True + class OrganizationDeletionWizard(DeletionWizard): model = models.Organization fields = model.TABLE_COLS @@ -940,6 +958,7 @@ class OrganizationDeletionWizard(DeletionWizard): 'final-organization_deletion':\ 'ishtar/wizard/wizard_organization_deletion.html'} + class AccountWizard(Wizard): model = models.Person def get_formated_datas(self, forms): @@ -1023,6 +1042,7 @@ class AccountWizard(Wizard): form.is_hidden = False return form + class SourceWizard(Wizard): model = None def get_extra_model(self, dct, form_list): |