From f31f0f269a68b2dfc5c834ce420bb5a02a3ecd0c Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Sun, 21 Oct 2012 15:04:28 +0200 Subject: Djangoization - Major refactoring (step 8) * clean-up on request and storage args in methods * view creation now managed by South * clean some mess in session values by using MultiValueDict --- ishtar_common/models.py | 8 ++-- .../templates/ishtar/wizard/towns_wizard.html | 40 ++++++++++++++++++ ishtar_common/templates/towns_wizard.html | 40 ------------------ ishtar_common/wizards.py | 48 +++++++++++----------- 4 files changed, 69 insertions(+), 67 deletions(-) create mode 100644 ishtar_common/templates/ishtar/wizard/towns_wizard.html delete mode 100644 ishtar_common/templates/towns_wizard.html (limited to 'ishtar_common') diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 3ca830aa3..d8d3c3213 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -579,9 +579,11 @@ class Person(Address, OwnPerms) : return lbl def full_label(self): - return u" ".join([unicode(getattr(self, attr)) - for attr in ('title', 'surname', 'name', 'attached_to') - if getattr(self, attr)]) + values = [unicode(_(self.title))] + values += [unicode(getattr(self, attr)) + for attr in ('surname', 'name', 'attached_to') + if getattr(self, attr)] + return u" ".join(values) class IshtarUser(User): person = models.ForeignKey(Person, verbose_name=_(u"Person"), unique=True) diff --git a/ishtar_common/templates/ishtar/wizard/towns_wizard.html b/ishtar_common/templates/ishtar/wizard/towns_wizard.html new file mode 100644 index 000000000..115ac9838 --- /dev/null +++ b/ishtar_common/templates/ishtar/wizard/towns_wizard.html @@ -0,0 +1,40 @@ +{% extends "default_wizard.html" %} +{% load i18n %} +{% load range %} +{% block extra_head %} +{{form.media}} +{% endblock %} +{% block content %} +
{% csrf_token %} + +
+{% if extra_context.TOWNS %} +{% if form.forms %} +
+ + {%if form.non_form_errors%}{%endif%} + {{ form.management_form }} + {% for formsetform in form.forms %} + {{ formsetform.as_table }} + {% endfor %} + +
{{form.non_form_errors}}
+{% else %} + + {{ form.as_table }} +
+{% endif %} +{% else %} +

{% trans "No town set in the associated file." %}

+{% endif %} + +{{ previous_fields|safe }} + +
+
+{% endblock %} diff --git a/ishtar_common/templates/towns_wizard.html b/ishtar_common/templates/towns_wizard.html deleted file mode 100644 index 115ac9838..000000000 --- a/ishtar_common/templates/towns_wizard.html +++ /dev/null @@ -1,40 +0,0 @@ -{% extends "default_wizard.html" %} -{% load i18n %} -{% load range %} -{% block extra_head %} -{{form.media}} -{% endblock %} -{% block content %} -
{% csrf_token %} - -
-{% if extra_context.TOWNS %} -{% if form.forms %} -
- - {%if form.non_form_errors%}{%endif%} - {{ form.management_form }} - {% for formsetform in form.forms %} - {{ formsetform.as_table }} - {% endfor %} - -
{{form.non_form_errors}}
-{% else %} - - {{ form.as_table }} -
-{% endif %} -{% else %} -

{% trans "No town set in the associated file." %}

-{% endif %} - -{{ previous_fields|safe }} - -
-
-{% endblock %} diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index 7895d98e3..e423b834e 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -24,8 +24,8 @@ from django.contrib.formtools.wizard.views import NamedUrlWizardView from django.core.exceptions import ObjectDoesNotExist from django.shortcuts import render_to_response from django.template import RequestContext +from django.utils.datastructures import MultiValueDict from django.utils.translation import ugettext_lazy as _ - import models class Wizard(NamedUrlWizardView): @@ -134,7 +134,7 @@ class Wizard(NamedUrlWizardView): # check if the form is initialized otherwise initialize it if not storage.get_step_data(next_step): values = self.get_form_initial(next_step) - prefixed_values = {} + prefixed_values = MultiValueDict() if not isinstance(values, list): for key in values: form_key = next_step + '-' + key @@ -525,12 +525,13 @@ class Wizard(NamedUrlWizardView): request.POST = post_data return super(Wizard, self).post(*args, **kwargs) - @classmethod - def session_has_key(cls, request, storage, form_key, key=None, multi=None): + def session_has_key(self, form_key, key=None, multi=None): """Check if the session has value of a specific form and (if provided) of a key """ print "17" + 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'] @@ -541,12 +542,13 @@ class Wizard(NamedUrlWizardView): form_key + '-0-' + key #only check if the first field is available return key in request.session[storage.prefix]['step_data'][form_key] - @classmethod - def session_get_value(cls, request, storage, form_key, key, multi=False): + def session_get_value(self, form_key, key, multi=False): """Get the value of a specific form""" print "18" - if not cls.session_has_key(request, storage, form_key, key, multi): + if not self.session_has_key(form_key, key, multi): return + request = self.request + storage = self.storage if not multi: key = key.startswith(form_key) and key or form_key + '-' + key return request.session[storage.prefix]['step_data'][form_key][key] @@ -564,8 +566,7 @@ class Wizard(NamedUrlWizardView): current_obj = None main_form_key = 'selec-' + self.url_name try: - idx = self.session_get_value(self.request, self.storage, - main_form_key, 'pk') + idx = self.session_get_value(main_form_key, 'pk') if type(idx) in (tuple, list): idx = idx[0] idx = int(idx) @@ -588,12 +589,12 @@ class Wizard(NamedUrlWizardView): self.storage.reset() val = model_name in request.session and request.session[model_name] if val: - return {'pk':val} + return MultiValueDict({'pk':val}) elif current_obj: return self.get_instanced_init(current_obj, step) current_form = self.form_list[current_step] if hasattr(current_form, 'currents'): - initial = {} + initial = MultiValueDict() for key in current_form.currents: model_name = current_form.currents[key].__name__.lower() val = model_name in request.session and \ @@ -616,11 +617,11 @@ class Wizard(NamedUrlWizardView): if len(prefixes) > 1 and prefixes[-2].startswith(obj_name): obj_name = prefixes[-2] self.request.session[obj_name] = unicode(obj.pk) - initial = {} + initial = MultiValueDict() if self.request.POST or \ (step in self.request.session[self.storage.prefix] and\ self.request.session[self.storage.prefix]['step_data'][step]): - return {} + return initial if hasattr(c_form, 'base_fields'): for base_field in c_form.base_fields.keys(): fields = base_field.split('__') @@ -723,11 +724,11 @@ class DeletionWizard(Wizard): datas[0][1].append(res[field]) return datas - def done(self, request, storage, form_list, **kwargs): - obj = self.get_current_object(request, storage) + def done(self, form_list, **kwargs): + obj = self.get_current_object() obj.delete() return render_to_response('wizard_delete_done.html', {}, - context_instance=RequestContext(request)) + context_instance=RequestContext(self.request)) class ClosingWizard(Wizard): # "close" an item @@ -769,15 +770,15 @@ class ClosingWizard(Wizard): datas[0][1].append(res[field]) return datas - def done(self, request, storage, form_list, **kwargs): - obj = self.get_current_object(request, storage) + def done(self, form_list, **kwargs): + obj = self.get_current_object() for form in form_list: if form.is_valid(): if 'end_date' in form.cleaned_data and hasattr(obj, 'end_date'): obj.end_date = form.cleaned_data['end_date'] obj.save() return render_to_response('wizard_closing_done.html', {}, - context_instance=RequestContext(request)) + context_instance=RequestContext(self.request)) class PersonWizard(Wizard): model = models.Person @@ -863,16 +864,15 @@ class AccountWizard(Wizard): form = super(AccountWizard, self).get_form(step, data, files) if not hasattr(form, 'is_hidden'): return form - if self.session_get_value(self.request, self.storage, - 'account-account_management', 'hidden_password'): + if self.session_get_value('account-account_management', + 'hidden_password'): form.is_hidden = False return form class SourceWizard(Wizard): model = None - def get_extra_model(self, dct, request, storage, form_list): - dct = super(SourceWizard, self).get_extra_model(dct, request, storage, - form_list) + def get_extra_model(self, dct, form_list): + dct = super(SourceWizard, self).get_extra_model(dct, form_list) if 'history_modifier' in dct: dct.pop('history_modifier') return dct -- cgit v1.2.3