diff options
Diffstat (limited to 'ishtar_common/wizards.py')
| -rw-r--r-- | ishtar_common/wizards.py | 73 | 
1 files changed, 45 insertions, 28 deletions
| diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index 3f90f8c48..128833ad4 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-2016  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2010-2017  É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,14 +22,15 @@ import logging  # from functools import wraps  from django.conf import settings -from django.contrib.formtools.wizard.views import NamedUrlWizardView, \ -    normalize_name, get_storage, StepsHelper +from formtools.wizard.views import NamedUrlWizardView, normalize_name, \ +    get_storage, StepsHelper +  from django.contrib.sites.models import Site  from django.core.exceptions import ObjectDoesNotExist  from django.core.files.images import ImageFile  from django.core.files.storage import default_storage  from django.core.mail import send_mail -from django.db.models.fields.files import FileField +from django.db.models.fields.files import FileField, ImageFieldFile  from django.db.models.fields.related import ManyToManyField  from django.db.models.fields import NOT_PROVIDED @@ -127,8 +128,7 @@ class Wizard(NamedUrlWizardView):      label = ''      translated_keys = []      modification = None  # True when the wizard modify an item -    storage_name = \ -        'django.contrib.formtools.wizard.storage.session.SessionStorage' +    storage_name = 'formtools.wizard.storage.session.SessionStorage'      wizard_done_template = 'ishtar/wizard/wizard_done.html'      wizard_done_window = ''      wizard_confirm = 'ishtar/wizard/confirm_wizard.html' @@ -357,7 +357,7 @@ class Wizard(NamedUrlWizardView):                  if form_datas:                      form_datas.append(("", "", "spacer"))                  items = hasattr(base_form, 'fields') and \ -                    base_form.fields.keyOrder or cleaned_data.keys() +                    base_form.fields.keys() or cleaned_data.keys()                  for key in items:                      lbl = None                      if key.startswith('hidden_'): @@ -368,7 +368,7 @@ class Wizard(NamedUrlWizardView):                          if hasattr(base_form, 'associated_labels') \                             and key in base_form.associated_labels:                              lbl = base_form.associated_labels[key] -                    if not lbl: +                    if not lbl or key not in cleaned_data:                          continue                      value = cleaned_data[key]                      if value is None or value == '': @@ -715,19 +715,22 @@ class Wizard(NamedUrlWizardView):                          # check if there is no missing fields                          # should be managed normally in forms but... -                        if hasattr(model._meta, 'get_fields'):  # django 1.8 -                            fields = model._meta.get_field() -                        else: -                            fields = model._meta.fields +                        fields = model._meta.get_fields() -                        has_problemetic_null = [ -                            (field.name, field.default == NOT_PROVIDED) -                            for field in fields + +                        has_problemetic_null = False +                        for field in fields:                              if (field.name not in value -                                or not value[field.name]) -                            and not field.null and not field.blank -                            and (not field.default -                                 or field.default == NOT_PROVIDED)] +                                or not value[field.name]) \ +                                and (hasattr(field, 'null') +                                     and not field.null) \ +                                and (hasattr(field, 'blank') +                                     and not field.blank) \ +                                and (hasattr(field, 'default') +                                     and (not field.default +                                          or field.default == NOT_PROVIDED)): +                                    has_problemetic_null = True +                                    break                          if has_problemetic_null:                              continue @@ -739,7 +742,9 @@ class Wizard(NamedUrlWizardView):                          value.save()  # force post_save                  # check that an item is not add multiple times (forged forms)                  if value not in related_model.all() and\ -                        hasattr(related_model, 'add'): +                        (not hasattr(related_model, 'through') or +                         not isinstance(value, related_model.through)): +                    # many to many and the value have been already managed                      related_model.add(value)                      # necessary to manage interaction between models like                      # material_index management for baseitems @@ -872,7 +877,7 @@ class Wizard(NamedUrlWizardView):              frm = form.forms[0]          if frm:              # autofocus on first field -            first_field = frm.fields[frm.fields.keyOrder[0]] +            first_field = frm.fields[frm.fields.keys()[0]]              attrs = first_field.widget.attrs              attrs.update({'autofocus': "autofocus"})              first_field.widget.attrs = attrs @@ -1108,7 +1113,13 @@ class Wizard(NamedUrlWizardView):                      continue                  if hasattr(value, 'pk'):                      value = value.pk -                if value in (True, False) or \ +                if isinstance(value, ImageFieldFile) \ +                        or isinstance(value, FileField): +                    try: +                        initial[base_field] = value.path +                    except ValueError: +                        pass +                elif value in (True, False) or \                     isinstance(value, FileField) or \                     isinstance(value, ImageFile):                      initial[base_field] = value @@ -1160,8 +1171,7 @@ class SearchWizard(NamedUrlWizardView):      model = None      label = ''      modification = None  # True when the wizard modify an item -    storage_name = \ -        'django.contrib.formtools.wizard.storage.session.SessionStorage' +    storage_name = 'formtools.wizard.storage.session.SessionStorage'      def get_wizard_name(self):          """ @@ -1373,17 +1383,24 @@ class AccountWizard(Wizard):              if key.startswith('hidden_password'):                  dct['password'] = dct.pop(key)          try: -            account = models.IshtarUser.objects.get(person=person) +            account = models.IshtarUser.objects.get(person=person).user_ptr              account.username = dct['username']              account.email = dct['email']          except ObjectDoesNotExist:              now = datetime.datetime.now() -            account = models.IshtarUser( -                person=person, username=dct['username'], email=dct['email'], +            account = models.User.objects.create( +                username=dct['username'], email=dct['email'],                  first_name=person.surname or '***',                  last_name=person.name or '***',                  is_staff=False, is_active=True, is_superuser=False, -                last_login=now, date_joined=now) +                last_login=now, date_joined=now +            ) +            ishtaruser = account.ishtaruser +            old_person_pk = ishtaruser.person.pk +            ishtaruser.person = person +            ishtaruser.save() +            models.Person.objects.get(pk=old_person_pk).delete() +          if dct['password']:              account.set_password(dct['password'])          account.save() | 
