Skip to content

Commit

Permalink
feat: changed manage user view
Browse files Browse the repository at this point in the history
  • Loading branch information
rb-25 committed Apr 28, 2024
1 parent 7da2ed6 commit de3818a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
OrganizationSubTypeAPIView,
OrganizationAdminViewSet,
OrganizationsListView,
OrganizationAddUserView,
OrganizationManageUserView,
OrganizationBulkUploadView,
OrganizationBulkDownloadView,
)
Expand Down Expand Up @@ -40,7 +40,7 @@

# URL PATTERNS
urlpatterns = [
path("organizations/add_user/", OrganizationAddUserView.as_view(), name="organization_add_user"),
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 @@ -142,7 +142,7 @@ def get_serializer_class(self):

return OrganizationDetailSerializer

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

Expand All @@ -153,7 +153,7 @@ def post(self,request):
role = data.get("role", None)
position = data.get("position", None)

if organization_id is None or user_email is None or role is 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,
Expand All @@ -167,7 +167,13 @@ def post(self,request):
status=status.HTTP_404_NOT_FOUND,
)

user = User.objects.get(email=user_email)
try:
user = User.objects.get(email=user_email)
except User.DoesNotExist:
return Response(
{"detail": "User not found"},
status=status.HTTP_404_NOT_FOUND,
)

user_organization_relation, created = UserOrganizationRelation.objects.update_or_create(
user=user,
Expand All @@ -188,6 +194,47 @@ def post(self,request):
{"detail": "User already exists in the organization"},
status=status.HTTP_400_BAD_REQUEST,
)

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:
organization = Organization.objects.get(id=organization_id)
except Organization.DoesNotExist:
return Response(
{"detail": "Organization not found"},
status=status.HTTP_404_NOT_FOUND,
)

try:
user = User.objects.get(email=user_email)
except User.DoesNotExist:
return Response(
{"detail": "User not found"},
status=status.HTTP_404_NOT_FOUND,
)

try:
user_organization_relation = UserOrganizationRelation.objects.get(user=user, organization=organization)
except UserOrganizationRelation.DoesNotExist:
return Response(
{"detail": "User does not exist in the organization"},
status=status.HTTP_404_NOT_FOUND,
)

user_organization_relation.delete()
return Response(
{"detail": "User removed from organization successfully"},
status=status.HTTP_200_OK,
)

class OrganizationBulkUploadView(BaseBulkUploadView):
csv_type = "organization"
Expand Down

0 comments on commit de3818a

Please sign in to comment.