diff --git a/api/dashboard/user/dash_user_serializer.py b/api/dashboard/user/dash_user_serializer.py index b3e3c833..c68b0314 100644 --- a/api/dashboard/user/dash_user_serializer.py +++ b/api/dashboard/user/dash_user_serializer.py @@ -4,7 +4,7 @@ from django.db import transaction from rest_framework import serializers -from db.organization import Organization, UserOrganizationLink +from db.organization import Department, Organization, UserOrganizationLink from db.task import UserIgLink from db.user import User, UserRoleLink from utils.permission import JWTUtils @@ -383,3 +383,46 @@ def update(self, instance, validated_data): ) return super().update(instance, validated_data) +class UserOrgLinkSerializer(serializers.ModelSerializer): + department = serializers.PrimaryKeyRelatedField( + queryset=Department.objects.all(), required=False + ) + graduation_year = serializers.CharField(required=False) + organization = serializers.PrimaryKeyRelatedField( + queryset=Organization.objects.all(), many=False, required=True + ) + is_alumni=serializers.BooleanField(required=False) + + def create(self, validated_data): + department = validated_data.get("department", None) + graduation_year = validated_data.get("graduation_year", None) + is_alumni=validated_data.get("is_alumni",False) + is_college = lambda org: org.org_type == OrganizationType.COLLEGE.value + user_id=self.context.get('user') + + return UserOrganizationLink.objects.create( + user=user_id, + org=validated_data.get("organization"), + created_by=user_id, + created_at=DateTimeUtils.get_current_utc_time(), + verified=True, + department=department if is_college(validated_data.get("organization")) else None, + graduation_year=graduation_year if is_college(validated_data.get("organization")) else None, + is_alumni=is_alumni if is_college(validated_data.get("organization")) else None, + ) + + + + class Meta: + model = UserOrganizationLink + fields = [ "organization", "department", "graduation_year","is_alumni"] + +class GetUserLinkSerializer(serializers.ModelSerializer): + org_id=serializers.CharField(source='org.id') + org_title=serializers.CharField(source='org.title') + dept_id = serializers.CharField(source='department.id', allow_null=True, required=False) + dept_title=serializers.CharField(source='department.title', allow_null=True, required=False) + is_alumni=serializers.BooleanField() + class Meta: + model = UserOrganizationLink + fields=["org_id","org_title","dept_id","dept_title","is_alumni"] diff --git a/api/dashboard/user/dash_user_views.py b/api/dashboard/user/dash_user_views.py index d669a626..bd20b284 100644 --- a/api/dashboard/user/dash_user_views.py +++ b/api/dashboard/user/dash_user_views.py @@ -7,10 +7,11 @@ from django.db.models import Q from rest_framework.views import APIView +from db.organization import UserOrganizationLink from db.user import ForgotPassword, User, UserRoleLink from utils.permission import CustomizePermission, JWTUtils, role_required from utils.response import CustomResponse -from utils.types import RoleType, WebHookActions, WebHookCategory +from utils.types import OrganizationType, RoleType, WebHookActions, WebHookCategory from utils.utils import CommonUtils, DateTimeUtils, DiscordWebhooks, send_template_mail from . import dash_user_serializer from django.core.cache import cache @@ -372,3 +373,28 @@ def post(self, request): return CustomResponse( response={"user_id": user.id, "profile_pic": uploaded_file_url} ).get_success_response() + + +class UserAddOrgAPI(APIView): + def post(self,request): + user = User.objects.filter(id=JWTUtils.fetch_user_id(request)).first() + if user is None: + return CustomResponse( + general_message="No user data available" + ).get_failure_response() + serializer=dash_user_serializer.UserOrgLinkSerializer(data=request.data,context={'user':user}) + if(serializer.is_valid()): + serializer.save() + return CustomResponse(general_message="organisation linked successfully").get_success_response() + return CustomResponse(response=serializer.errors).get_failure_response() + def get(self,request): + user = User.objects.filter(id=JWTUtils.fetch_user_id(request)).first() + if user is None: + return CustomResponse( + general_message="No user data available" + ).get_failure_response() + + links = UserOrganizationLink.objects.filter(user=user,org__org_type=OrganizationType.COLLEGE.value).select_related('org','department') + serializer=dash_user_serializer.GetUserLinkSerializer(instance=links,many=True) + return CustomResponse(response=serializer.data).get_success_response() + \ No newline at end of file diff --git a/api/dashboard/user/urls.py b/api/dashboard/user/urls.py index 8bfe8eb8..dc9192ce 100644 --- a/api/dashboard/user/urls.py +++ b/api/dashboard/user/urls.py @@ -8,6 +8,8 @@ path('verification/csv/', dash_user_views.UserVerificationCSV.as_view(), name='csv-verification'), path('verification//', dash_user_views.UserVerificationAPI.as_view(), name='edit-verification'), path('verification//', dash_user_views.UserVerificationAPI.as_view(), name='delete-verification'), + path('organization/',dash_user_views.UserAddOrgAPI.as_view(),name="user-org-link"), + path('organization/list',dash_user_views.UserAddOrgAPI.as_view(),name="get-user-org-link"), path('info/', dash_user_views.UserInfoAPI.as_view()), path('forgot-password/', dash_user_views.ForgotPasswordAPI.as_view(), name="forgot-password"), diff --git a/api/register/register_views.py b/api/register/register_views.py index 7291b9dd..e86ffd5f 100644 --- a/api/register/register_views.py +++ b/api/register/register_views.py @@ -203,8 +203,8 @@ def post(self, request): # data=data.get("user"), context={"request": request} # ) - create_user = serializers.RegisterSerializer( - data=data, context={"request": request} + create_user = serializers.UserSerializer( + data=data.pop("user"), context={"request": request} ) if not create_user.is_valid(): diff --git a/api/register/urls.py b/api/register/urls.py index daf1d4ad..0537f4a0 100644 --- a/api/register/urls.py +++ b/api/register/urls.py @@ -4,7 +4,7 @@ urlpatterns = [ path("", register_views.RegisterDataAPI.as_view()), - path("validate/", register_views.UserRegisterValidateAPI.as_view()), + # path("validate/", register_views.UserRegisterValidateAPI.as_view()), path("role/list/", register_views.RoleAPI.as_view()), path("colleges/", register_views.CollegesAPI.as_view()), path("department/list/", register_views.DepartmentAPI.as_view()), diff --git a/db/organization.py b/db/organization.py index 7c5bab15..12984318 100644 --- a/db/organization.py +++ b/db/organization.py @@ -158,11 +158,10 @@ class UserOrganizationLink(models.Model): related_name='user_organization_link_department') graduation_year = models.CharField(max_length=10, blank=True, null=True) verified = models.BooleanField() - is_alumni = models.IntegerField(blank=True, null=True, default=False) + is_alumni = models.BooleanField(blank=True, null=True, default=False) created_by = models.ForeignKey(User, on_delete=models.SET(settings.SYSTEM_ADMIN_ID), db_column='created_by', related_name='user_organization_link_created_by') created_at = models.DateTimeField(auto_now_add=True) - class Meta: managed = False db_table = 'user_organization_link'