summaryrefslogtreecommitdiff
path: root/ishtar_common/wizards.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2018-05-14 21:08:44 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2018-06-12 08:44:34 +0200
commit80093e0e188363f4e481085b91a9e7711cc4371c (patch)
tree3d7c3e2f3217849df3cfe2f8e461792c08473fa4 /ishtar_common/wizards.py
parentcb2bdf1cf1ee85ddf68e93c29611024571961c5d (diff)
downloadIshtar-80093e0e188363f4e481085b91a9e7711cc4371c.tar.bz2
Ishtar-80093e0e188363f4e481085b91a9e7711cc4371c.zip
Authors for M2M images (refs #4076)
Diffstat (limited to 'ishtar_common/wizards.py')
-rw-r--r--ishtar_common/wizards.py24
1 files changed, 20 insertions, 4 deletions
diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py
index 7556f5194..a6844b674 100644
--- a/ishtar_common/wizards.py
+++ b/ishtar_common/wizards.py
@@ -525,7 +525,7 @@ class Wizard(NamedUrlWizardView):
frm.cleaned_data.pop('DELETE')
for key in frm.cleaned_data:
value = frm.cleaned_data[key]
- if value is None or value in ['', []]:
+ if value is None or value in ['']:
continue
if key in associated_models:
if type(value) in (tuple, list):
@@ -745,7 +745,8 @@ class Wizard(NamedUrlWizardView):
related_data = {} # used for intermediary models
# an intermediary model is used
- if not related_model.through._meta.auto_created:
+ if hasattr(related_model, 'through') and \
+ not related_model.through._meta.auto_created:
for field in related_model.through._meta.get_fields():
# is used for the obj or target
if getattr(field, 'related_model', None) and \
@@ -765,7 +766,6 @@ class Wizard(NamedUrlWizardView):
m2m_items[key] = vals
else:
m2m_items[key] = related_model.all()
-
if value not in m2m_items[key]:
if type(value) == dict:
model = related_model.model
@@ -839,6 +839,12 @@ class Wizard(NamedUrlWizardView):
if k in value:
related_data[k] = value.pop(k)
+ # extract m2m values - not suitable for creation
+ m2m_values = {}
+ for k in value.keys():
+ if type(value[k]) in (list, tuple):
+ m2m_values[k] = value.pop(k)
+
if get_or_create:
value, created = model.objects.get_or_create(
**value)
@@ -854,6 +860,9 @@ class Wizard(NamedUrlWizardView):
value = instance
else:
value = model.objects.create(**value)
+
+ for k in m2m_values:
+ setattr(value, k, m2m_values[k])
value.save() # force post_save
# check that an item is not add multiple times (forged forms)
if value not in related_model.all() and \
@@ -862,7 +871,8 @@ class Wizard(NamedUrlWizardView):
# many to many and the value have been already managed
# an intermediary model is used
- if not related_model.through._meta.auto_created:
+ if hasattr(related_model, 'through') and \
+ not related_model.through._meta.auto_created:
for field in related_model.through._meta.get_fields():
if hasattr(field, 'related_model') \
and field.related_model:
@@ -1280,6 +1290,12 @@ class Wizard(NamedUrlWizardView):
value = getattr(child_obj, field)
if hasattr(value, 'pk'):
value = value.pk
+ elif hasattr(value, 'all'):
+ vals.setlist(field, [
+ unicode(v.pk)
+ for v in getattr(child_obj, field).all()
+ ])
+ return vals
if value is not None:
vals[field] = unicode(value)
elif hasattr(child_obj, field + "s"):