Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dépôt de besoin x APProch : si besoin existant, mettre à jour certains champs #1069

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions lemarche/api/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from lemarche.perimeters.factories import PerimeterFactory
from lemarche.sectors.factories import SectorFactory
from lemarche.tenders import constants as tender_constants
from lemarche.tenders.factories import TenderFactory
from lemarche.tenders.models import Tender
from lemarche.users import constants as user_constants
from lemarche.users.factories import UserFactory
Expand Down Expand Up @@ -178,14 +179,34 @@ def test_partner_approch_can_create_tender(self):
tender = Tender.objects.last()
self.assertEqual(tender.author, self.user_partner_with_token)
self.assertEqual(tender.partner_approch_id, 123)
# existing tender
tender_data = TENDER_JSON.copy()
tender_data["contact_email"] = self.user_partner_with_token.email
tender_data["extra_data"] = {"id": 123}
response = self.client.post(self.url, data=tender_data, content_type="application/json")

def test_partner_approch_can_update_tender(self):
with self.settings(PARTNER_APPROCH_USER_ID=self.user_partner_with_token.id):
existing_tender_partner_data = {
"contact_email": self.user_partner_with_token.email,
"extra_data": {"id": 123},
"partner_approch_id": 123,
"title": "Test",
"description": "Description",
"deadline_date": "2024-06-30",
}
TenderFactory(**{**TENDER_JSON.copy(), **existing_tender_partner_data})
self.assertEqual(Tender.objects.count(), 1)
self.assertEqual(Tender.objects.first().title, "Test")
self.assertEqual(Tender.objects.first().deadline_date.strftime("%Y-%m-%d"), "2024-06-30")
# existing tender: won't be re-created
new_tender_partner_data = {
**existing_tender_partner_data,
"title": "Test changed",
"deadline_date": "2024-12-31",
}
response = self.client.post(
self.url, data={**TENDER_JSON.copy(), **new_tender_partner_data}, content_type="application/json"
)
self.assertEqual(response.status_code, 201)
self.assertEqual(Tender.objects.count(), 1)
tender = Tender.objects.last()
self.assertEqual(Tender.objects.first().title, "Test changed")
self.assertEqual(Tender.objects.first().deadline_date.strftime("%Y-%m-%d"), "2024-12-31")


class TenderChoicesApiTest(TestCase):
Expand Down
11 changes: 9 additions & 2 deletions lemarche/api/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from lemarche.www.tenders.utils import get_or_create_user_from_anonymous_content


PARTNER_APPROCH_UPDATE_FIELDS = ["title", "description", "deadline_date", "external_link"]


class TenderViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
serializer_class = TenderSerializer

Expand Down Expand Up @@ -54,8 +57,12 @@ def perform_create(self, serializer: TenderSerializer):
tender_partner_approch_id = serializer.validated_data.get("extra_data", {}).get("id", None)
if tender_partner_approch_id:
try:
Tender.objects.get(partner_approch_id=tender_partner_approch_id)
# TODO: update existing tender
# try to find an existing tender
# if found, update some fields
tender = Tender.objects.get(partner_approch_id=tender_partner_approch_id)
for field in PARTNER_APPROCH_UPDATE_FIELDS:
setattr(tender, field, serializer.validated_data.get(field))
tender.save(update_fields=PARTNER_APPROCH_UPDATE_FIELDS)
return
except Tender.DoesNotExist:
serializer.validated_data["partner_approch_id"] = tender_partner_approch_id
Expand Down
Loading