diff --git a/lemarche/api/tenders/serializers.py b/lemarche/api/tenders/serializers.py index a421fe724..72b6d6ce7 100644 --- a/lemarche/api/tenders/serializers.py +++ b/lemarche/api/tenders/serializers.py @@ -3,6 +3,7 @@ from lemarche.perimeters.models import Perimeter from lemarche.sectors.models import Sector from lemarche.tenders.models import Tender +from lemarche.users import constants as user_constants class TenderSerializer(serializers.ModelSerializer): @@ -14,6 +15,13 @@ class TenderSerializer(serializers.ModelSerializer): queryset=Perimeter.objects.all(), slug_field="slug", allow_null=True, required=False ) extra_data = serializers.JSONField(required=False) + # non-model fields + contact_kind = serializers.ChoiceField( + choices=user_constants.KIND_CHOICES, allow_blank=True, write_only=True, required=False + ) + contact_buyer_kind_detail = serializers.ChoiceField( + choices=user_constants.BUYER_KIND_DETAIL_CHOICES, allow_blank=True, write_only=True, required=False + ) class Meta: model = Tender @@ -47,4 +55,7 @@ class Meta: "deadline_date", # extra data "extra_data", + # non-model fields + "contact_kind", + "contact_buyer_kind_detail", ] diff --git a/lemarche/api/tenders/views.py b/lemarche/api/tenders/views.py index e6bc361e9..5cd5ceaca 100644 --- a/lemarche/api/tenders/views.py +++ b/lemarche/api/tenders/views.py @@ -26,17 +26,28 @@ def create(self, request, *args, **kwargs): return super().create(request, args, kwargs) def perform_create(self, serializer: TenderSerializer): + """ + - set Tender source + - set Tender author (create user if it doesn't exist) + - pop non-model fields + - create Tender ! + """ source = ( tender_constants.SOURCE_TALLY if serializer.validated_data.get("extra_data", {}).get("source") == tender_constants.SOURCE_TALLY else tender_constants.SOURCE_API ) + # 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, ) + # pop non-model fields + serializer.validated_data.pop("contact_kind", None) + serializer.validated_data.pop("contact_buyer_kind_detail", None) + # create Tender serializer.save( author=user, status=tender_constants.STATUS_PUBLISHED,