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 : stocker l'ID lorsqu'un nouveau besoin est reçu via l'API #1062

Merged
merged 1 commit into from
Jan 30, 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
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
Loading