Skip to content

Commit

Permalink
fix(register): Accept interest details on register api
Browse files Browse the repository at this point in the history
  • Loading branch information
aswanthabam committed Jan 4, 2025
1 parent 403ad38 commit efbcd01
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 81 deletions.
8 changes: 2 additions & 6 deletions api/register/register_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
141 changes: 66 additions & 75 deletions api/register/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}"

0 comments on commit efbcd01

Please sign in to comment.