diff options
| -rw-r--r-- | ishtar/furnitures/forms.py | 98 | ||||
| -rw-r--r-- | ishtar/furnitures/models.py | 1 | ||||
| -rw-r--r-- | ishtar/templates/confirm_wizard.html | 25 | ||||
| -rw-r--r-- | ishtar/templates/default_wizard.html (renamed from ishtar/templates/file_wizard.html) | 0 | ||||
| -rw-r--r-- | ishtar/templates/wizard_done.html (renamed from ishtar/templates/file_done.html) | 6 | 
5 files changed, 111 insertions, 19 deletions
| diff --git a/ishtar/furnitures/forms.py b/ishtar/furnitures/forms.py index de221da70..91c9370bc 100644 --- a/ishtar/furnitures/forms.py +++ b/ishtar/furnitures/forms.py @@ -39,7 +39,18 @@ from django.utils.functional import lazy  reverse_lazy = lazy(reverse, unicode) +class FinalForm(forms.Form): +    final = True +    form_label = _("Confirm") +  class Wizard(NamedUrlSessionFormWizard): +    def get_template(self, request, storage): +        templates = ['default_wizard.html'] +        current_step = storage.get_current_step() or '0' +        if current_step == self.get_last_step(request, storage): +            templates = ['confirm_wizard.html'] + templates +        return templates +      def get_template_context(self, request, storage, form=None):          """          Add previous and current steps to manage the wizard path @@ -54,27 +65,82 @@ class Wizard(NamedUrlSessionFormWizard):          previous_steps = []          while step:              if step == current_step: -                context.update({'previous_steps':previous_steps}) -                return context -            else: -                previous_steps.append(self.form_list[step]) +                break +            previous_steps.append(self.form_list[step])              step = self.get_next_step(request, storage, step)          context.update({'previous_steps':previous_steps}) +        # not last step: validation +        if step != self.get_last_step(request, storage): +            return context +        final_form_list = [] +        for form_key in self.get_form_list(request, storage).keys(): +            form_obj = self.get_form(request, storage, step=form_key, +                    data=storage.get_step_data(form_key), +                    files=storage.get_step_files(form_key)) +            form_obj.is_valid() +            final_form_list.append(form_obj) +        context.update({'datas':self.get_formated_datas(final_form_list)})          return context -class FileWizard(Wizard): -    def get_template(self, request, form): -        return ['file_wizard.html'] +    def get_formated_datas(self, forms): +        datas = [] +        for form in forms: +            base_form = hasattr(form, 'forms') and form.forms[0] or form +            associated_models = hasattr(base_form, 'associated_models') and \ +                                base_form.associated_models or {} +            if not hasattr(form, 'cleaned_data'): +                continue +            cleaned_datas = type(form.cleaned_data) == list and \ +                                              form.cleaned_data \ +                            or [form.cleaned_data] +            for cleaned_data in cleaned_datas: +                for key in cleaned_data: +                    lbl = key +                    if hasattr(base_form, 'fields') and key in base_form.fields: +                        lbl = base_form.fields[key].label +                    value = cleaned_data[key] +                    if key in associated_models: +                        value = unicode(associated_models[key].objects.get( +                                                                      pk=value)) +                    datas.append((lbl, value)) +        return datas + +    def done(self, request, storage, form_list, **kwargs): +        dct, m2m = {}, [] +        for form in form_list: +            base_form = hasattr(form, 'forms') and form.forms[0] or form +            associated_models = hasattr(base_form, 'associated_models') and \ +                                base_form.associated_models or {} +            if type(form.cleaned_data) == dict: +                for key in form.cleaned_data: +                    value = form.cleaned_data[key] +                    if key in associated_models: +                        value = associated_models[key].objects.get(pk=value) +                    dct[key] = value +            elif type(form.cleaned_data) == list: +                for item in form.cleaned_data: +                    for key in item: +                        if key not in associated_models: +                            # datas not managed +                            continue +                        value = item[key] +                        value = associated_models[key].objects.get(pk=value) +                        m2m.append((key, value)) +        dct['history_modifier'] = request.user +        fle = models.File(**dct) +        fle.save() +        for key, value in m2m: +            getattr(fle, key+'s').add(value) +        return render_to_response('wizard_done.html', {}, +                                  context_instance=RequestContext(request)) -    def done(self, request, storage, form_list): -        return render_to_response( -            'file_done.html', -            {'form_list': [form.cleaned_data for form in form_list]}, -            context_instance=RequestContext(request) -            ) +class FileWizard(Wizard): +    pass  class FileForm1(forms.Form):      form_label = _("General") +    associated_models = {'in_charge':models.Person, +                         'file_type':models.FileType}      in_charge = forms.IntegerField(label=_("Person in charge"),           widget=widgets.JQueryAutoComplete(reverse_lazy('autocomplete-person'),                                             associated_model=models.Person), @@ -97,6 +163,7 @@ class FileForm2(forms.Form):  class TownForm(forms.Form):      form_label = _("Towns") +    associated_models = {'town':models.Town}      # !FIXME hard_link, reverse_lazy doen't seem to work with formsets      town = forms.IntegerField(label=_(u"Town"),           widget=widgets.JQueryAutoComplete("/" + settings.URL_PATH + \ @@ -108,6 +175,8 @@ TownFormSet.form_label = _("Towns")  class FileForm3(forms.Form):      form_label = _("Preventive informations") +    associated_models = {'general_contractor':models.Organization, +                         'saisine_type':models.SaisineType}      general_contractor = forms.IntegerField(label=_(u"General contractor"),              widget=widgets.JQueryAutoComplete(                  reverse_lazy('autocomplete-organization'), @@ -133,6 +202,7 @@ def is_preventive(self, request, storage):      except ValueError:          return False -file_creation_wizard = FileWizard([FileForm1, FileForm2, TownFormSet, FileForm3], +file_creation_wizard = FileWizard([FileForm1, FileForm2, TownFormSet, FileForm3, +                                   FinalForm],                     url_name='file_creation', condition_list={'3':is_preventive}) diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py index 008ad1dc3..0aac7d297 100644 --- a/ishtar/furnitures/models.py +++ b/ishtar/furnitures/models.py @@ -236,6 +236,7 @@ class File(BaseHistorizedItem, OwnPerms):      total_developed_surface = models.IntegerField(_(u"Total developed surface"),                                                    blank=True, null=True)      address = models.TextField(_(u"Address")) +    comment = models.TextField(_(u"Comment"))      history = HistoricalRecords()      class Meta: diff --git a/ishtar/templates/confirm_wizard.html b/ishtar/templates/confirm_wizard.html new file mode 100644 index 000000000..c83134dfb --- /dev/null +++ b/ishtar/templates/confirm_wizard.html @@ -0,0 +1,25 @@ +{% extends "base.html" %} +{% load i18n %} +{% load range %} +{% block content %} +<form action="." method="post">{% csrf_token %} +<ul id='form_path'> +{% for step in previous_steps %} +  <li>» <button name="form_prev_step" value="{{forloop.counter0}}">{{step.form_label}}</button></li> +{% endfor %} +  <li class='current'>» <a href='#'>{{current_step.form_label}}</a></li> +</ul> +<div class='form'> +<p>{% trans "You have entered the following informations:" %}</p> +<table> +{% for label, data in datas %} +<tr><th>{{label}}</th><td>{{data}}</td></th> +{% endfor %} +</table> +<p>{% trans "Would you like to save them?" %}</p> +<input type="hidden" name="{{ step_field }}" value="{{ step0 }}" /> +{{ previous_fields|safe }} +<input type="submit" value="{% trans "Validate" %}"/> +</div> +</form> +{% endblock %} diff --git a/ishtar/templates/file_wizard.html b/ishtar/templates/default_wizard.html index eaf40cc15..eaf40cc15 100644 --- a/ishtar/templates/file_wizard.html +++ b/ishtar/templates/default_wizard.html diff --git a/ishtar/templates/file_done.html b/ishtar/templates/wizard_done.html index 6bbeb53d4..468e8ed6a 100644 --- a/ishtar/templates/file_done.html +++ b/ishtar/templates/wizard_done.html @@ -7,10 +7,6 @@  {% endfor %}    <li class='current'>» <a href='#'>{{current_step.form_label}}</a></li>  </ul> -<div class='form'> -<pre> -{{ form_list|pprint }} -</pre> -</form> +<p>{%trans "Item successfully saved"%}</p>  </div>  {% endblock %} | 
