Skip to content

Commit

Permalink
Update Tender serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Dec 12, 2023
1 parent 11f7744 commit 81671ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lemarche/api/tenders/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -47,4 +55,7 @@ class Meta:
"deadline_date",
# extra data
"extra_data",
# non-model fields
"contact_kind",
"contact_buyer_kind_detail",
]
11 changes: 11 additions & 0 deletions lemarche/api/tenders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 81671ed

Please sign in to comment.