Skip to content

Commit

Permalink
feat: endpoint to add user to org, school field in user (#8)
Browse files Browse the repository at this point in the history
* feat: endpoint to add user to org, school field in user

* feat: changed manage user view

* feat: modified try except block

* feat: removed User and Org db instances
  • Loading branch information
rb-25 authored Apr 28, 2024
1 parent 0b9c04f commit b5e2483
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
OrganizationSubTypeAPIView,
OrganizationAdminViewSet,
OrganizationsListView,
OrganizationManageUserView,
OrganizationBulkUploadView,
OrganizationBulkDownloadView,
)
Expand Down Expand Up @@ -39,6 +40,7 @@

# URL PATTERNS
urlpatterns = [
path("admin/organizations/manage_user/", OrganizationManageUserView.as_view(), name="organization_add_user"),
# LISTS
path("organizations/titles/", OrganizationsListView.as_view(), name="organizations_titles"),
path("special_organizations/", SpecialOrganizationsAPIView.as_view(), name="special_organizations"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from django_filters.rest_framework import DjangoFilterBackend


from student_welfare_backend.core.models import Organization
from student_welfare_backend.users.models import User
from student_welfare_backend.core.models import Organization, UserOrganizationRelation
from student_welfare_backend.core.api.serializers import (
OrganizationSerializer,
OrganizationDetailSerializer,
Expand Down Expand Up @@ -141,7 +142,87 @@ def get_serializer_class(self):

return OrganizationDetailSerializer

class OrganizationManageUserView(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated, IsDSW | IsADSW]

def post(self,request):
data = request.data
organization_id = data.get("organization_id", None)
user_email = data.get("user_email", None)
role = data.get("role", None)
position = data.get("position", None)

if None in [organization_id, user_email, role]:
return Response(
{"detail": "Please fill in all the required fields: organization_id, user_id, role"},
status=status.HTTP_400_BAD_REQUEST,
)

try:
user_organization_relation, created = UserOrganizationRelation.objects.update_or_create(
user__email=user_email,
organization__id=organization_id,
defaults={
"role": role,
"position": position,
},
)
if created:
return Response(
{"detail": "User added to organization successfully"},
status=status.HTTP_201_CREATED,
)
else:
return Response(
{"detail": "User already exists in the organization, updated successfully"},
status=status.HTTP_200_OK,
)
except Organization.DoesNotExist:
return Response(
{"detail": "Organization not found"},
status=status.HTTP_404_NOT_FOUND,
)
except User.DoesNotExist:
return Response(
{"detail": "User not found"},
status=status.HTTP_404_NOT_FOUND,
)

def delete(self,request):
data = request.data
organization_id = data.get("organization_id", None)
user_email = data.get("user_email", None)

if None in [organization_id, user_email]:
return Response(
{"detail": "Please fill in all the required fields: organization_id, user_id"},
status=status.HTTP_400_BAD_REQUEST,
)

try:
user_organization_relation = UserOrganizationRelation.objects.get(user__email=user_email, organization__id=organization_id)
user_organization_relation.delete()
return Response(
{"detail": "User removed from organization successfully"},
status=status.HTTP_200_OK,
)
except Organization.DoesNotExist:
return Response(
{"detail": "Organization not found"},
status=status.HTTP_404_NOT_FOUND,
)
except User.DoesNotExist:
return Response(
{"detail": "User not found"},
status=status.HTTP_404_NOT_FOUND,
)
except UserOrganizationRelation.DoesNotExist:
return Response(
{"detail": "User does not exist in the organization"},
status=status.HTTP_404_NOT_FOUND,
)

class OrganizationBulkUploadView(BaseBulkUploadView):
csv_type = "organization"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.10 on 2024-04-28 05:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('users', '0008_user_picture_url'),
]

operations = [
migrations.AddField(
model_name='user',
name='school',
field=models.CharField(blank=True, choices=[('SAS', 'SAS'), ('SCORE', 'SCORE'), ('VSPARC', 'V-SPARC'), ('SMEC', 'SMEC'), ('SELECT', 'SELECT'), ('SCHEME', 'SCHEME'), ('HOT', 'HOT'), ('VITBS', 'VIT BS'), ('SCOPE', 'SCOPE'), ('VSIGN', 'VSIGN'), ('SSL', 'SSL'), ('SENSE', 'SENSE'), ('SCE', 'SCE'), ('SBST', 'SBST'), ('VAIAL', 'VAIAL')], max_length=50, null=True, verbose_name='School'),
),
]
21 changes: 20 additions & 1 deletion student_welfare_backend/student_welfare_backend/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ class User(AbstractUser):
check forms.SignupForm and forms.SocialSignupForms accordingly.
"""

school_type_choices = [
("SAS", "SAS"),
("SCORE", "SCORE"),
("VSPARC", "V-SPARC"),
("SMEC", "SMEC"),
("SELECT", "SELECT"),
("SCHEME", "SCHEME"),
("HOT", "HOT"),
("VITBS", "VIT BS"),
("SCOPE", "SCOPE"),
("VSIGN", "VSIGN"),
("SSL", "SSL"),
("SENSE", "SENSE"),
("SCE", "SCE"),
("SBST", "SBST"),
("VAIAL", "VAIAL"),
]

#: First and last name do not cover name patterns around the globe
name = models.CharField(_("Name of User"), blank=False, max_length=255)
email = models.EmailField(_("Email of User"), max_length=254, validators=[validate_email])
Expand All @@ -46,7 +64,8 @@ class User(AbstractUser):
is_dsw = models.BooleanField(_("User is DSW"), default=False)
is_adsw = models.BooleanField(_("User is ADSW"), default=False)
office_location = models.CharField(_("Office location of the user"), blank=True, max_length=255)

school = models.CharField(_("School"), max_length=50, choices=school_type_choices,blank=True, null=True)

def get_absolute_url(self):
"""Get url for user's detail view.
Expand Down

0 comments on commit b5e2483

Please sign in to comment.