diff --git a/lemarche/tenders/tests.py b/lemarche/tenders/tests.py index 0ee1e8c31..448c92328 100644 --- a/lemarche/tenders/tests.py +++ b/lemarche/tenders/tests.py @@ -24,6 +24,7 @@ from lemarche.users.factories import UserFactory from lemarche.users.models import User from lemarche.utils.admin.admin_site import MarcheAdminSite, get_admin_change_view_url +from lemarche.www.tenders import utils as tender_utils date_tomorrow = timezone.now() + timedelta(days=1) @@ -971,3 +972,29 @@ def test_edit_form_no_matching_on_validate_submission(self): self.assertTrue(hasattr(tender_response, "siae_count_annotated")) self.assertEqual(tender_response.siae_count_annotated, 1) self.assertEqual(tender_response.siae_count_annotated, self.tender.tendersiae_set.count()) + + +class TenderUtilsTest(TestCase): + @classmethod + def setUpTestData(cls): + cls.user_siae = UserFactory(kind=User.KIND_SIAE) + cls.user_buyer = UserFactory(kind=User.KIND_BUYER) + cls.siae_with_tender_1 = SiaeFactory(users=[cls.user_siae]) + siae_with_tender_2 = SiaeFactory(users=[cls.user_siae]) + cls.sector = SectorFactory() + cls.tender_with_siae = TenderFactory( + siaes=[cls.siae_with_tender_1, siae_with_tender_2], + sectors=[cls.sector], + author=cls.user_buyer, + deadline_date=date_tomorrow, + status=tender_constants.STATUS_SENT, + first_sent_at=timezone.now(), + ) + + def test_duplicate(self): + new_tender = tender_utils.duplicate(self.tender_with_siae) + self.assertEqual(self.tender_with_siae.title, new_tender.title) + self.assertEqual(self.tender_with_siae.author, new_tender.author) + self.assertNotEqual(self.tender_with_siae.status, new_tender.status) + self.assertEqual(self.tender_with_siae.sectors().count(), new_tender.sectors().count()) + self.assertNotEqual(self.tender_with_siae.siaes().count(), new_tender.siaes().count()) diff --git a/lemarche/www/tenders/utils.py b/lemarche/www/tenders/utils.py index 9d063d368..9bb67f065 100644 --- a/lemarche/www/tenders/utils.py +++ b/lemarche/www/tenders/utils.py @@ -1,5 +1,6 @@ from django.conf import settings +from lemarche.tenders import constants as tender_constants from lemarche.tenders.models import Tender, TenderQuestion from lemarche.users import constants as user_constants from lemarche.users.models import User @@ -101,7 +102,9 @@ def get_or_create_user(request_user, tender_dict: dict, source=user_constants.SO def duplicate(tender: Tender) -> Tender: FIELDS_TO_REMOVE = ( - ["_state"] + ["id", "slug", "siae_transactioned", "extra_data", "import_raw_object"] + Tender.READONLY_FIELDS + ["_state", "_django_version"] + + ["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 @@ -111,8 +114,8 @@ def duplicate(tender: Tender) -> Tender: 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["status"] = tender_constants.STATUS_PUBLISHED + new_tender_dict["source"] = tender_constants.SOURCE_STAFF_C4_CREATED new_tender_dict["logs"] = [{"action": "tender_duplicated", "from": tender.id}] # create duplicate tender