diff --git a/api/register/register_views.py b/api/register/register_views.py index 84f886b5..d765cd26 100644 --- a/api/register/register_views.py +++ b/api/register/register_views.py @@ -219,14 +219,10 @@ class RegisterDataAPI(APIView): def post(self, request): data = request.data data = {key: value for key, value in data.items() if value} - # create_user = serializers.UserSerializer( - # data=data.get("user"), context={"request": request} - # ) - create_user = serializers.UserSerializer( - data=data.pop("user"), context={"request": request} + create_user = serializers.RegisterSerializer( + data=data, context={"request": request} ) - if not create_user.is_valid(): return CustomResponse(message=create_user.errors).get_failure_response() diff --git a/api/register/serializers.py b/api/register/serializers.py index 4f51202a..bd990dda 100644 --- a/api/register/serializers.py +++ b/api/register/serializers.py @@ -363,81 +363,6 @@ class Meta: ] -class RegisterSerializer(serializers.Serializer): - user = UserSerializer() - organization = UserOrgLinkSerializer(required=False) - referral = ReferralSerializer(required=False) - integration = IntegrationSerializer(required=False) - mentor = MentorSerializer(required=False) - - def create(self, validated_data): - with transaction.atomic(): - user = UserSerializer().create(validated_data.pop("user")) - - if organizations := validated_data.pop("organization", None): - organizations.update({"user": user}) - UserOrgLinkSerializer().create(organizations) - - if referral := validated_data.pop("referral", None): - referral.update({"user": user}) - ReferralSerializer().create(referral) - - if integration := validated_data.pop("integration", None): - integration.update({"user": user}) - IntegrationSerializer().create(integration) - - if mentor := validated_data.pop("mentor", None): - mentor["user"] = user - MentorSerializer().create(mentor) - - return user - - class Meta: - model = User - fields = [ - "user", - "organization", - "referral", - "param", - "mentor", - ] - - -class UserCountrySerializer(serializers.ModelSerializer): - country_name = serializers.CharField(source="name") - - class Meta: - model = Country - fields = ["country_name"] - - -class UserStateSerializer(serializers.ModelSerializer): - state_name = serializers.CharField(source="name") - - class Meta: - model = State - fields = ["state_name"] - - -class UserZoneSerializer(serializers.ModelSerializer): - zone_name = serializers.CharField(source="name") - - class Meta: - model = Zone - fields = ["zone_name"] - - -class LocationSerializer(serializers.ModelSerializer): - location = serializers.SerializerMethodField() - - class Meta: - model = District - fields = ("id", "location") - - def get_location(self, obj): - return f"{obj.name}, {obj.zone.state.name}, {obj.zone.state.country.name}" - - class UserInterestSerializer(serializers.ModelSerializer): id = serializers.CharField(read_only=True) user = serializers.CharField(read_only=True) @@ -506,3 +431,69 @@ class Meta: "created_at", "updated_at", ] + + +class RegisterSerializer(serializers.Serializer): + user = UserSerializer() + interests = UserInterestSerializer(required=True) + integration = IntegrationSerializer(required=False) + referral = ReferralSerializer(required=False) + + def create(self, validated_data): + with transaction.atomic(): + user = UserSerializer().create(validated_data.pop("user")) + UserInterestSerializer(context={"user": user}).create( + validated_data.pop("interests") + ) + if integration := validated_data.pop("integration", None): + integration.update({"user": user}) + IntegrationSerializer().create(integration) + + if referral := validated_data.pop("referral", None): + referral.update({"user": user}) + ReferralSerializer().create(referral) + return user + + class Meta: + model = User + fields = [ + "user", + "interests", + "integration", + "referral", + ] + + +class UserCountrySerializer(serializers.ModelSerializer): + country_name = serializers.CharField(source="name") + + class Meta: + model = Country + fields = ["country_name"] + + +class UserStateSerializer(serializers.ModelSerializer): + state_name = serializers.CharField(source="name") + + class Meta: + model = State + fields = ["state_name"] + + +class UserZoneSerializer(serializers.ModelSerializer): + zone_name = serializers.CharField(source="name") + + class Meta: + model = Zone + fields = ["zone_name"] + + +class LocationSerializer(serializers.ModelSerializer): + location = serializers.SerializerMethodField() + + class Meta: + model = District + fields = ("id", "location") + + def get_location(self, obj): + return f"{obj.name}, {obj.zone.state.name}, {obj.zone.state.country.name}"