diff options
Diffstat (limited to 'ishtar_common/wizards.py')
-rw-r--r-- | ishtar_common/wizards.py | 63 |
1 files changed, 55 insertions, 8 deletions
diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index c065459f6..2fbe30e0e 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -18,6 +18,7 @@ # See the file COPYING for details. import datetime +import logging # from functools import wraps from django.conf import settings @@ -38,6 +39,8 @@ from django.utils.datastructures import MultiValueDict as BaseMultiValueDict from django.utils.translation import ugettext_lazy as _ import models +logger = logging.getLogger(__name__) + class MultiValueDict(BaseMultiValueDict): def get(self, *args, **kwargs): @@ -126,6 +129,8 @@ class Wizard(NamedUrlWizardView): current_obj_slug = '' file_storage = default_storage + saved_args = {} # argument to pass on object save + ''' # buggy and unecessary... def __init__(self, *args, **kwargs): @@ -165,6 +170,13 @@ class Wizard(NamedUrlWizardView): return super(Wizard, self).dispatch(request, *args, **kwargs) + def get_form_kwargs(self, step=None): + kwargs = super(Wizard, self).get_form_kwargs(step) + if hasattr(self.form_list[step], 'need_user_for_initialization') and\ + self.form_list[step].need_user_for_initialization: + kwargs['user'] = self.request.user + return kwargs + def get_prefix(self, *args, **kwargs): """As the class name can interfere when reused prefix with the url_name """ @@ -194,7 +206,7 @@ class Wizard(NamedUrlWizardView): self.request.session['CURRENT_ACTION'] = self.get_wizard_name() step = self.steps.first current_step = self.steps.current - dct = {'current_step': self.form_list[current_step], + dct = {'current_step_label': self.form_list[current_step].form_label, 'wizard_label': self.label, 'current_object': self.get_current_object(), 'is_search': current_step.startswith('selec-') @@ -209,7 +221,7 @@ class Wizard(NamedUrlWizardView): or (previous_steps and previous_steps[-1] == self.form_list[step]): break - previous_steps.append(self.form_list[step]) + previous_steps.append(self.form_list[step].form_label) previous_step_counter += 1 if previous_step_counter >= len(self.steps): break @@ -250,7 +262,7 @@ class Wizard(NamedUrlWizardView): if step == next_step: current_step_passed = True elif current_step_passed: - next_steps.append(self.form_list[next_step]) + next_steps.append(self.form_list[next_step].form_label) next_step = self.get_next_step(next_step) context.update({'next_steps': next_steps}) # not last step: validation @@ -483,9 +495,12 @@ class Wizard(NamedUrlWizardView): elif type(dct[k]) not in (list, tuple): dct[k] = [dct[k]] setattr(obj, k, dct[k]) + if hasattr(obj, 'pre_save'): + obj.pre_save() try: obj.full_clean() - except ValidationError: + except ValidationError as e: + logger.warning(unicode(e)) return self.render(form_list[-1]) for dependant_item in other_objs: c_item = getattr(obj, dependant_item) @@ -543,12 +558,19 @@ class Wizard(NamedUrlWizardView): dct[dependant_item] = c_item if 'pk' in dct: dct.pop('pk') + saved_args = self.saved_args.copy() + for k in saved_args: + if k in dct: + saved_args[k] = dct.pop(k) obj = self.get_saved_model()(**dct) + if hasattr(obj, 'pre_save'): + obj.pre_save() try: obj.full_clean() - except ValidationError: + except ValidationError as e: + logger.warning(unicode(e)) return self.render(form_list[-1]) - obj.save() + obj.save(**saved_args) for k in adds: getattr(obj, k).add(adds[k]) # necessary to manage interaction between models like @@ -561,6 +583,12 @@ class Wizard(NamedUrlWizardView): old_m2ms = {} for model in whole_associated_models: related_model = getattr(obj, model + 's') + # manage through + if hasattr(related_model, 'through') and related_model.through: + related_set_name = str( + related_model.through.__name__ + '_set').lower() + if hasattr(obj, related_set_name): + related_model = getattr(obj, related_set_name) # clear real m2m if hasattr(related_model, 'clear'): old_m2ms[model] = [] @@ -587,6 +615,9 @@ class Wizard(NamedUrlWizardView): if value not in m2m_items[key]: if type(value) == dict: model = related_model.model + if hasattr(related_model, 'through') and \ + related_model.through: + model = related_model.through # not m2m -> foreign key if not hasattr(related_model, 'clear'): assert hasattr(model, 'MAIN_ATTR'), \ @@ -611,6 +642,11 @@ class Wizard(NamedUrlWizardView): else: if issubclass(model, models.BaseHistorizedItem): value['history_modifier'] = self.request.user + if hasattr(model, 'RELATIVE_MODELS') and \ + self.get_saved_model() in \ + model.RELATIVE_MODELS: + value[model.RELATIVE_MODELS[ + self.get_saved_model()]] = obj value = model.objects.create(**value) value.save() # check that an item is not add multiple times (forged forms) @@ -626,6 +662,7 @@ class Wizard(NamedUrlWizardView): self.request.session[self.current_obj_slug] = unicode(obj.pk) self.request.session[self.get_object_name(obj)] = unicode(obj.pk) dct = {'item': obj} + self.current_object = obj # force evaluation of lazy urls wizard_done_window = unicode(self.wizard_done_window) if wizard_done_window: @@ -980,8 +1017,18 @@ class Wizard(NamedUrlWizardView): if not hasattr(obj, key): return initial keys = c_form.form.base_fields.keys() - query = getattr(obj, key) - if not obj._meta.ordering: + related = getattr(obj, key) + # manage through + through = False + if hasattr(related, 'through') and related.through: + related_set_name = str( + related.through.__name__ + '_set').lower() + if hasattr(obj, related_set_name): + through = True + related = getattr(obj, related_set_name) + + query = related + if not through and not obj._meta.ordering: query = query.order_by('pk') for child_obj in query.all(): if not keys: |