diff options
Diffstat (limited to 'ishtar_common/models_common.py')
-rw-r--r-- | ishtar_common/models_common.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index 2d8966066..17e643631 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -2369,6 +2369,40 @@ class DocumentItem: return actions +def clean_duplicate_association(document, related_item, action): + profile = get_current_profile() + if not profile.clean_redundant_document_association or action != "post_add": + return + class_name = related_item.__class__.__name__ + if class_name not in ("Find", "ContextRecord", "Operation"): + return + if class_name == "Find": + for cr in document.context_records.filter( + base_finds__find__pk=related_item.pk).all(): + document.context_records.remove(cr) + for ope in document.operations.filter( + context_record__base_finds__find__pk=related_item.pk).all(): + document.operations.remove(ope) + return + if class_name == "ContextRecord": + for ope in document.operations.filter( + context_record__pk=related_item.pk).all(): + document.operations.remove(ope) + if document.finds.filter( + base_finds__context_record=related_item.pk).count(): + document.context_records.remove(related_item) + return + if class_name == "Operation": + if document.context_records.filter( + operation=related_item.pk).count(): + document.operations.remove(related_item) + return + if document.finds.filter( + base_finds__context_record__operation=related_item.pk).count(): + document.operations.remove(related_item) + return + + def document_attached_changed(sender, **kwargs): # associate a default main image instance = kwargs.get("instance", None) @@ -2388,6 +2422,8 @@ def document_attached_changed(sender, **kwargs): return for item in items: + clean_duplicate_association(instance, item, + kwargs.get("action", None)) for doc in item.documents.all(): doc.regenerate_all_ids() q = item.documents.filter( |