Skip to content

Commit

Permalink
feat(besoins): méthode pour dupliquer un besoin (#1097)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Feb 22, 2024
1 parent 380e710 commit 1c5a9ed
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions lemarche/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
25 changes: 25 additions & 0 deletions lemarche/www/tenders/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -97,3 +98,27 @@ def get_or_create_user(request_user, tender_dict: dict, source=user_constants.SO
if need_to_be_saved:
user.save()
return user


FIELDS_TO_REMOVE = ["siae_transactioned", "extra_data", "import_raw_object"] + Tender.READONLY_FIELDS


def duplicate(tender: Tender, fields_to_remove=FIELDS_TO_REMOVE) -> Tender:
fields_to_remove_full = ["_state", "_django_version", "id", "slug"] + fields_to_remove
fields_to_keep = [field for field in tender.__dict__.keys() if field not in fields_to_remove_full]
# 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_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
new_tender = Tender.objects.create(**new_tender_dict)
new_tender.sectors.set(tender.sectors.all())

return new_tender

0 comments on commit 1c5a9ed

Please sign in to comment.