diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-02-12 17:22:47 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2025-02-12 17:25:26 +0100 |
commit | a11b802415453d60b418c49a37eac1de1a85bdee (patch) | |
tree | d0a9e05a4217c97b37966aaeb417c4a2954a93aa /archaeological_files/admin.py | |
parent | cf386287da6c20da6f0df500ac5c228e6a847724 (diff) | |
download | Ishtar-a11b802415453d60b418c49a37eac1de1a85bdee.tar.bz2 Ishtar-a11b802415453d60b418c49a37eac1de1a85bdee.zip |
🐛 admin - preventive: price aggreement copy - fix child copy (refs #6174)
Diffstat (limited to 'archaeological_files/admin.py')
-rw-r--r-- | archaeological_files/admin.py | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/archaeological_files/admin.py b/archaeological_files/admin.py index 40fcfd42b..206e97348 100644 --- a/archaeological_files/admin.py +++ b/archaeological_files/admin.py @@ -193,22 +193,43 @@ class CopyPriceAgreementAdmin(GeneralTypeAdmin): destination = get_object_or_404( models.PriceAgreement, pk=form.cleaned_data["destination"] ) - for model, slug_name in ((models.Job, "txt_idx"), - (models.EquipmentServiceCost, "slug")): - items = model.objects.filter(price_agreement_id=source.pk).all() - for item in items: - slug = getattr(item, slug_name).split("--")[0] + "--" + \ - str(destination.pk) - q = model.objects.filter(**{slug_name: slug}) - if q.count(): - already_here += 1 - continue - new_item = item - new_item.pk = None - setattr(new_item, slug_name, slug) - new_item.price_agreement_id = destination.pk - new_item.save() - created += 1 + for model, slug_name, has_child in ( + (models.Job, "txt_idx", True), + (models.EquipmentServiceCost, "slug", False) + ): + base_query = model.objects.filter(price_agreement_id=source.pk) + if not has_child: + items_queries = [base_query] + else: + items_queries = [ + base_query.filter(child__isnull=True), + base_query.filter(child__isnull=False), + ] + children = {} + for items in items_queries: + for item in items.all(): + slug = getattr(item, slug_name).split("--")[0] + "--" + \ + str(destination.pk) + q = model.objects.filter(**{slug_name: slug}) + if q.count(): + already_here += 1 + continue + current_pk = item.pk + new_item = item + new_item.pk = None + setattr(new_item, slug_name, slug) + new_item.price_agreement_id = destination.pk + if has_child and new_item.child: + if new_item.child.pk not in children: + # previous child attached to a bad contract + # cannot correct automatically + pass + else: + new_item.child_id = children[new_item.child.pk] + new_item.save() + created += 1 + if has_child and not new_item.child: + children[current_pk] = new_item.pk if created: self.message_user( request, str(_("{} item(s) created.")).format(created) |