From 8d7ada5403da6dcea6670753e4e15ea45f27c2fe Mon Sep 17 00:00:00 2001 From: Jovit-Mathew236 Date: Wed, 20 Dec 2023 21:54:47 +0530 Subject: [PATCH] [FEAT] Add MentorSerializer and UserMentor model --- api/register/serializers.py | 45 ++++++++++++++++++++++++++++++++++--- db/user.py | 33 +++++++++++++-------------- 2 files changed, 58 insertions(+), 20 deletions(-) diff --git a/api/register/serializers.py b/api/register/serializers.py index 151207af..0beb8e70 100644 --- a/api/register/serializers.py +++ b/api/register/serializers.py @@ -1,3 +1,4 @@ +import uuid from django.contrib.auth.hashers import make_password from django.db import transaction from rest_framework import serializers @@ -13,8 +14,8 @@ UserOrganizationLink, Zone, ) -from db.task import InterestGroup, Level, MucoinInviteLog, UserLvlLink, Wallet -from db.user import Role, Socials, User, UserReferralLink, UserRoleLink, UserSettings +from db.task import InterestGroup, Level, MucoinInviteLog, UserIgLink, UserLvlLink, Wallet +from db.user import Role, Socials, User, UserMentor, UserReferralLink, UserRoleLink, UserSettings from utils.exception import CustomException from utils.types import OrganizationType, RoleType from utils.utils import DateTimeUtils @@ -130,6 +131,39 @@ class Meta: fields = ["user", "organizations", "department", "graduation_year"] +class MentorSerializer(serializers.ModelSerializer): + user = serializers.CharField(required=False) + about = serializers.CharField(required=False) + reason = serializers.CharField(required=False) + hours = serializers.IntegerField(required=False) + + def create(self, validated_data): + about = validated_data.get("about", None) + reason = validated_data.get("reason", None) + hours = validated_data.get("hours", None) + + UserMentor.objects.create( + user=validated_data["user"], + about=about, + reason=reason, + hours=hours, + created_by=validated_data["user"], + created_at=DateTimeUtils.get_current_utc_time(), + updated_by=validated_data["user"], + updated_at=DateTimeUtils.get_current_utc_time(), + ) + + class Meta: + model = UserMentor + fields = [ + "user", + "about", + "reason", + "hours", + ] + + + class ReferralSerializer(serializers.ModelSerializer): user = serializers.CharField(required=False) muid = serializers.CharField(required=False) @@ -275,12 +309,12 @@ class Meta: "district" ] - 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(): @@ -298,6 +332,10 @@ def create(self, validated_data): integration.update({"user": user}) IntegrationSerializer().create(integration) + if mentor := validated_data.pop("mentor", None): + mentor["user"] = user + MentorSerializer().create(mentor) + return user class Meta: @@ -307,6 +345,7 @@ class Meta: "organization", "referral", "param", + "mentor", ] diff --git a/db/user.py b/db/user.py index 762a9d80..d6152e15 100644 --- a/db/user.py +++ b/db/user.py @@ -3,13 +3,13 @@ from django.core.files.storage import FileSystemStorage from django.db import models -# from db.task import UserIgLink from django.conf import settings + from .managers import user_manager +# from .task import UserIgLink from decouple import config as decouple_config - # fmt: off # noinspection PyPep8 @@ -57,21 +57,20 @@ def save(self, *args, **kwargs): return super().save(*args, **kwargs) -# class UserMentor(models.Model): -# id = models.CharField(primary_key=True, max_length=36) -# user = models.ForeignKey(User, models.DO_NOTHING) -# about = models.CharField(max_length=1000, blank=True, null=True) -# reason = models.CharField(max_length=1000, blank=True, null=True) -# hours = models.IntegerField() -# interest_groups = models.ForeignKey(UserIgLink, models.DO_NOTHING, db_column='interest_groups', blank=True, null=True) -# updated_by = models.ForeignKey(User, models.DO_NOTHING, db_column='updated_by', related_name='usermentor_updated_by_set') -# updated_at = models.DateTimeField(blank=True, null=True) -# created_by = models.ForeignKey(User, models.DO_NOTHING, db_column='created_by', related_name='usermentor_created_by_set') -# created_at = models.DateTimeField(blank=True, null=True) - -# class Meta: -# managed = False -# db_table = 'user_mentor' +class UserMentor(models.Model): + id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4) + user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_mentor_user') + about = models.CharField(max_length=1000, blank=True, null=True) + reason = models.CharField(max_length=1000, blank=True, null=True) + hours = models.IntegerField() + updated_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='updated_by', related_name='user_mentor_updated_by_set') + updated_at = models.DateTimeField(blank=True, null=True) + created_by = models.ForeignKey(User, on_delete=models.CASCADE, db_column='created_by', related_name='user_mentor_created_by_set') + created_at = models.DateTimeField(blank=True, null=True) + + class Meta: + managed = False + db_table = 'user_mentor' class UserReferralLink(models.Model): id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4)