summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2019-09-12 19:13:54 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2019-09-12 19:14:47 +0200
commit70f0fa29fac15c03f1c7af0f185b70be480b2d45 (patch)
tree616f69b0393b8634ce967fa161e8d918acafc112 /ishtar_common
parentdd9915cbc67c1f33b94747f0fd4f072a6abe01d8 (diff)
downloadIshtar-70f0fa29fac15c03f1c7af0f185b70be480b2d45.tar.bz2
Ishtar-70f0fa29fac15c03f1c7af0f185b70be480b2d45.zip
Author: post merge duplicate authors on save - prevent duplicate in form validation
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/forms_common.py9
-rw-r--r--ishtar_common/models.py19
2 files changed, 27 insertions, 1 deletions
diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py
index dd55f95b4..486d25fcf 100644
--- a/ishtar_common/forms_common.py
+++ b/ishtar_common/forms_common.py
@@ -1425,6 +1425,15 @@ class AuthorForm(ManageOldType, NewItemForm):
initial=self.init_data.get('author_type'))
self.limit_fields()
+ def clean(self):
+ person_id = self.cleaned_data.get("person", None)
+ author_type_id = self.cleaned_data.get("author_type", None)
+ if not person_id or not author_type_id:
+ return self.cleaned_data
+ if models.Author.objects.filter(author_type_id=author_type_id,
+ person_id=person_id).count():
+ raise forms.ValidationError(_("This author already exist."))
+
def save(self, user):
dct = self.cleaned_data
dct['author_type'] = models.AuthorType.objects.get(
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index b750ee613..b3956e2f1 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -4853,8 +4853,25 @@ class Author(FullSearch):
"person": str(self.person)
}
+ def merge(self, item, keep_old=False):
+ merge_model_objects(self, item, keep_old=keep_old)
+
+
+def author_post_save(sender, **kwargs):
+ if not kwargs.get('instance'):
+ return
+ cached_label_changed(sender, **kwargs)
+ instance = kwargs.get('instance')
+ q = Author.objects.filter(person=instance.person,
+ author_type=instance.author_type)
+ if q.count() <= 1:
+ return
+ authors = list(q.all())
+ for author in authors[1:]:
+ authors[0].merge(author)
+
-post_save.connect(cached_label_changed, sender=Author)
+post_save.connect(author_post_save, sender=Author)
class SourceType(HierarchicalType):