diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-02-20 12:03:22 +0100 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-02-20 12:03:22 +0100 | 
| commit | d41e913396ad470f7e898bdfbca3d2cb4d2144c2 (patch) | |
| tree | 08f867c0fa37ec5640c0ce275b38b3f5e5ff7514 | |
| parent | c37bb6ecce45fccc1f0c9d321a785ffeeefc3366 (diff) | |
| download | Ishtar-d41e913396ad470f7e898bdfbca3d2cb4d2144c2.tar.bz2 Ishtar-d41e913396ad470f7e898bdfbca3d2cb4d2144c2.zip | |
Warehouse: fix warehouse creation with no divisions (refs #3481)
| -rw-r--r-- | archaeological_warehouse/forms.py | 3 | ||||
| -rw-r--r-- | archaeological_warehouse/tests.py | 21 | ||||
| -rw-r--r-- | ishtar_common/wizards.py | 24 | 
3 files changed, 46 insertions, 2 deletions
| diff --git a/archaeological_warehouse/forms.py b/archaeological_warehouse/forms.py index d76ce6b70..553a01ce5 100644 --- a/archaeological_warehouse/forms.py +++ b/archaeological_warehouse/forms.py @@ -281,7 +281,8 @@ class LocalisationForm(forms.Form):                  if q.count():                      initial = q.all()[0].reference              self.fields['division_{}'.format(divlink.pk)] = forms.CharField( -                label=str(divlink.division), max_length=200, initial=initial) +                label=str(divlink.division), max_length=200, initial=initial, +            )  class ContainerDeletionForm(FinalForm): diff --git a/archaeological_warehouse/tests.py b/archaeological_warehouse/tests.py index 691275876..7eb3017bc 100644 --- a/archaeological_warehouse/tests.py +++ b/archaeological_warehouse/tests.py @@ -64,12 +64,31 @@ class WarehouseWizardCreationTest(WizardTest, FindInit, TestCase):                  ]              },          ), +        FormData( +            'Warehouse creation with no division', +            form_datas={ +                'warehouse-warehouse_creation': { +                    'name': 'warehouse-ref', +                    'warehouse_type': None, +                    'location': None, +                    'responsible': None, +                }, +                'divisions-warehouse_creation': [ +                    { +                        'order': 42 +                    } +                ] +            }, +        ),      ]      def pre_wizard(self):          main_data = self.form_datas[0].form_datas +        alt_data = self.form_datas[1].form_datas          main_data['warehouse-warehouse_creation']['warehouse_type'] = \              models.WarehouseType.objects.all()[0].pk +        alt_data['warehouse-warehouse_creation']['warehouse_type'] = \ +            models.WarehouseType.objects.all()[0].pk          main_data['divisions-warehouse_creation'][0]['division'] = \              models.WarehouseDivision.create_default_for_test()[0].pk          self.warehouse_number = models.Warehouse.objects.count() @@ -78,7 +97,7 @@ class WarehouseWizardCreationTest(WizardTest, FindInit, TestCase):      def post_wizard(self):          self.assertEqual(models.Warehouse.objects.count(), -                         self.warehouse_number + 1) +                         self.warehouse_number + 2)          self.assertEqual(models.WarehouseDivisionLink.objects.count(),                           self.warehouse_div_link + 1) diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index c2cd54d03..5c4e1d3f9 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -31,6 +31,7 @@ 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.related import ManyToManyField +from django.db.models.fields import NOT_PROVIDED  from django.http import HttpResponseRedirect  from django.forms import ValidationError @@ -665,11 +666,34 @@ class Wizard(NamedUrlWizardView):                      else:                          if issubclass(model, models.BaseHistorizedItem):                              value['history_modifier'] = self.request.user + +                        get_or_create = False                          if hasattr(model, 'RELATIVE_MODELS') and \                                  self.get_saved_model() in \                                  model.RELATIVE_MODELS:                              value[model.RELATIVE_MODELS[                                  self.get_saved_model()]] = obj +                            get_or_create = True + +                        # 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 + +                        has_problemetic_null = [ +                            (field.name, field.default == NOT_PROVIDED) +                            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)] +                        if has_problemetic_null: +                            continue + +                        if get_or_create:                              value, created = model.objects.get_or_create(                                  **value)                          else: | 
