Skip to content

Commit

Permalink
Set partner_approch_id on Tender create
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jan 30, 2024
1 parent dd2a19b commit 1ff13a2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
28 changes: 28 additions & 0 deletions lemarche/api/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ def test_create_tender_with_different_contact_data(self):
self.assertEqual(author.buyer_kind_detail, user_constants.BUYER_KIND_DETAIL_PUBLIC_ASSOCIATION)


class TenderCreateApiPartnerTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.url = reverse("api:tenders-list") + "?token=approch"
cls.user_partner_with_token = UserFactory(email="[email protected]", api_key="approch")

def test_partner_approch_can_create_tender(self):
with self.settings(PARTNER_APPROCH_USER_ID=self.user_partner_with_token.id):
# new 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")
self.assertEqual(response.status_code, 201)
self.assertEqual(Tender.objects.count(), 1)
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")
self.assertEqual(response.status_code, 201)
self.assertEqual(Tender.objects.count(), 1)
tender = Tender.objects.last()


class TenderChoicesApiTest(TestCase):
def test_should_return_tender_kinds_list(self):
url = reverse("api:tender-kinds-list") # anonymous user
Expand Down
25 changes: 20 additions & 5 deletions lemarche/api/tenders/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.utils import timezone
from drf_spectacular.utils import OpenApiParameter, extend_schema
from rest_framework import mixins, viewsets
Expand Down Expand Up @@ -32,18 +33,32 @@ def perform_create(self, serializer: TenderSerializer):
- pop non-model fields
- create Tender !
"""
source = (
tender_source = (
tender_constants.SOURCE_TALLY
if serializer.validated_data.get("extra_data", {}).get("source") == tender_constants.SOURCE_TALLY
else tender_constants.SOURCE_API
)
user_source = (
user_constants.SOURCE_TALLY_FORM
if (tender_source == tender_constants.SOURCE_TALLY)
else user_constants.SOURCE_SIGNUP_FORM
)
# get Tender author
user = get_or_create_user_from_anonymous_content(
serializer.validated_data,
source=user_constants.SOURCE_TALLY_FORM
if source == tender_constants.SOURCE_TALLY
else user_constants.SOURCE_SIGNUP_FORM,
source=user_source,
)
# Manage Partner APProch
if tender_source == tender_constants.SOURCE_API:
if user.id == settings.PARTNER_APPROCH_USER_ID:
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
return
except Tender.DoesNotExist:
serializer.validated_data["partner_approch_id"] = tender_partner_approch_id
# pop non-model fields
serializer.validated_data.pop("contact_kind", None)
serializer.validated_data.pop("contact_buyer_kind_detail", None)
Expand All @@ -52,7 +67,7 @@ def perform_create(self, serializer: TenderSerializer):
author=user,
status=tender_constants.STATUS_PUBLISHED,
published_at=timezone.now(),
source=source,
source=tender_source,
import_raw_object=self.request.data,
)

Expand Down

0 comments on commit 1ff13a2

Please sign in to comment.