From 034e6b02b0f9286002053a9622e4cb7234d560d1 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Wed, 21 Feb 2024 12:46:58 +0100 Subject: [PATCH] Tender: add duplicate() helper --- lemarche/tenders/models.py | 1 + lemarche/www/tenders/utils.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lemarche/tenders/models.py b/lemarche/tenders/models.py index e139f4efb..fa4570def 100644 --- a/lemarche/tenders/models.py +++ b/lemarche/tenders/models.py @@ -240,6 +240,7 @@ class Tender(models.Model): "published_at", "validated_at", "first_sent_at", + "last_sent_at", "siae_list_last_seen_date", "created_at", "updated_at", diff --git a/lemarche/www/tenders/utils.py b/lemarche/www/tenders/utils.py index 6a218ee78..9d063d368 100644 --- a/lemarche/www/tenders/utils.py +++ b/lemarche/www/tenders/utils.py @@ -97,3 +97,26 @@ def get_or_create_user(request_user, tender_dict: dict, source=user_constants.SO if need_to_be_saved: user.save() return user + + +def duplicate(tender: Tender) -> Tender: + FIELDS_TO_REMOVE = ( + ["_state"] + ["id", "slug", "siae_transactioned", "extra_data", "import_raw_object"] + Tender.READONLY_FIELDS + ) + FIELDS_TO_KEEP = [field for field in tender.__dict__.keys() if field not in FIELDS_TO_REMOVE] + # sectors # managed post-create + + new_tender_dict = dict() + for key in FIELDS_TO_KEEP: + new_tender_dict[key] = tender.__dict__[key] + + # overwrite some fields + new_tender_dict["status"] = Tender.STATUS_PUBLISHED + new_tender_dict["source"] = Tender.SOURCE_STAFF_C4_CREATED + new_tender_dict["logs"] = [{"action": "tender_duplicated", "from": tender.id}] + + # create duplicate tender + new_tender = Tender.objects.create(**new_tender_dict) + new_tender.sectors.set(tender.sectors.all()) + + return new_tender