diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-02-22 17:45:05 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-02-28 12:15:24 +0100 |
commit | 8b3eae6c7d60a54bc61e29a6ea7539b8f809d26f (patch) | |
tree | 21d4c294c6f63e3d85ba8673d83e670adc62df30 /ishtar_common/models_common.py | |
parent | 5c9701f05ddaba63d957f1973652be545a57bea0 (diff) | |
download | Ishtar-8b3eae6c7d60a54bc61e29a6ea7539b8f809d26f.tar.bz2 Ishtar-8b3eae6c7d60a54bc61e29a6ea7539b8f809d26f.zip |
Option to auto-clean associated documents
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( |