From 2fe12d70c4a11e2aabcda5fc9ec81578be90250d Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 12 Sep 2019 19:13:54 +0200 Subject: Author: post merge duplicate authors on save - prevent duplicate in form validation --- ishtar_common/models.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'ishtar_common/models.py') 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): -- cgit v1.2.3