1010from django .conf import settings
1111from django .core .files .storage import default_storage
1212from django .utils import timezone
13+ from django .contrib .auth .models import User
14+ from django .contrib .auth .forms import PasswordResetForm
1315from datetime import timedelta , datetime
1416from pydantic import ValidationError
1517from enum import StrEnum
3436 is_platform_admin ,
3537)
3638from typing import Any
39+ import uuid
3740import json
3841import logging
3942
@@ -417,6 +420,7 @@ def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-unt
417420
418421
419422@method_decorator (accessible_for (roles = {"admin" }), name = "post" )
423+ @method_decorator (accessible_for (roles = {"admin" }), name = "get" )
420424class OrganizationUsersView (View ):
421425 def post (self , request , * args , ** kwargs ) -> JsonResponse : # type: ignore[no-untyped-def]
422426 try :
@@ -430,7 +434,7 @@ def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-unt
430434 )
431435 org_user .save ()
432436 return JsonResponse (
433- serializers .OrganizationUserResponse .model_validate (
437+ serializers .OrganizationUserResponse .from_django_model (
434438 org_user
435439 ).model_dump (),
436440 status = 201 ,
@@ -442,9 +446,64 @@ def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-unt
442446 except IntegrityError as e :
443447 return JsonResponse ({"error" : str (e )}, status = 409 )
444448
449+ def get (self , request , * args , ** kwargs ) -> JsonResponse : # type: ignore[no-untyped-def]
450+ organization_users = OrganizationUser .objects .filter (
451+ organization_id = kwargs ["organization_id" ]
452+ )
453+ response_list = []
454+ for org_user in organization_users :
455+ response_list .append (
456+ serializers .OrganizationUserResponse .from_django_model (
457+ org_user
458+ ).model_dump ()
459+ )
460+ return JsonResponse ({"organization_users" : response_list }, status = 200 )
461+
462+
463+ @method_decorator (accessible_for (roles = {"admin" }), name = "delete" )
464+ class SingleOrganizationUserView (View ):
465+ def delete (self , request , * args , ** kwargs ): # type: ignore[no-untyped-def]
466+ try :
467+ org_user = OrganizationUser .objects .get (id = kwargs ["user_id" ])
468+ org_user .delete ()
469+ return JsonResponse (
470+ {"message" : "Organization user removed successfully" }, status = 200
471+ )
472+ except OrganizationUser .DoesNotExist :
473+ return JsonResponse ({"error" : "Organization user not found" }, status = 404 )
474+ except ValidationError as e :
475+ return JsonResponse ({"error" : e .json ()}, status = 400 )
476+ except IntegrityError as e :
477+ return JsonResponse ({"error" : str (e )}, status = 409 )
478+
479+ def post (self , request , * args , ** kwargs ) -> JsonResponse : # type: ignore[no-untyped-def]
480+ try :
481+ payload = json .loads (request .body )
482+ serializer = serializers .UpdateOrganizationUserRoleRequest .model_validate (
483+ payload
484+ )
485+ org_user = OrganizationUser .objects .get (
486+ organization_id = kwargs ["organization_id" ], user_id = kwargs ["user_id" ]
487+ )
488+ org_user .role = serializer .role
489+ org_user .save ()
490+ return JsonResponse (
491+ serializers .OrganizationUserResponse .from_django_model (
492+ org_user
493+ ).model_dump (),
494+ status = 200 ,
495+ )
496+ except OrganizationUser .DoesNotExist :
497+ return JsonResponse ({"error" : "Organization user not found" }, status = 404 )
498+ except ValidationError as e :
499+ return JsonResponse ({"error" : e .json ()}, status = 400 )
500+ except IntegrityError as e :
501+ return JsonResponse ({"error" : str (e )}, status = 409 )
502+
445503
446504@method_decorator (is_platform_admin (), name = "post" )
447505@method_decorator (is_platform_admin (), name = "delete" )
506+ @method_decorator (accessible_for (roles = {"admin" , "editor" , "viewer" }), name = "get" )
448507class SingleOrganizationView (View ):
449508 def post (self , request , * args , ** kwargs ) -> JsonResponse : # type: ignore[no-untyped-def]
450509 try :
@@ -488,6 +547,61 @@ def delete(self, request, *args, **kwargs): # type: ignore[no-untyped-def]
488547 except IntegrityError as e :
489548 return JsonResponse ({"error" : str (e )}, status = 409 )
490549
550+ def get (self , request , * args , ** kwargs ) -> JsonResponse : # type: ignore[no-untyped-def]
551+ try :
552+ organization = Organization .objects .get (id = kwargs ["organization_id" ])
553+ return JsonResponse (
554+ serializers .OrganizationResponse .from_django_model (
555+ organization ,
556+ request .build_absolute_uri ,
557+ ).model_dump (),
558+ status = 200 ,
559+ )
560+ except Organization .DoesNotExist :
561+ return JsonResponse ({"error" : "Organization not found" }, status = 404 )
562+ except ValidationError as e :
563+ return JsonResponse ({"error" : e .json ()}, status = 400 )
564+
565+
566+ @method_decorator ((is_an_organization_member (only_admin = True )), name = "post" )
567+ class GetOrCreateUserByEmail (View ):
568+ def post (self , request , * args , ** kwargs ) -> JsonResponse : # type: ignore[no-untyped-def]
569+ payload = json .loads (request .body )
570+ serializer = serializers .GetOrCreateUserRequest .model_validate (payload )
571+ try :
572+ email = serializer .email
573+ organization_id = serializer .organization_id
574+ user = User .objects .filter (email = email ).first ()
575+ if not user :
576+ user = User .objects .create_user (
577+ username = email , email = email , password = uuid .uuid4 ().hex
578+ )
579+ form = PasswordResetForm (data = {"email" : email })
580+ if form .is_valid ():
581+ form .save (
582+ request = request ,
583+ use_https = True ,
584+ from_email = settings .DJANGO_EMAIL_LEARNING ["FROM_EMAIL" ],
585+ email_template_name = "emails/password_reset.txt" ,
586+ html_email_template_name = "emails/password_reset.html" ,
587+ extra_email_context = {
588+ "organization" : Organization .objects .get (
589+ id = organization_id
590+ ).name
591+ },
592+ )
593+ else :
594+ raise ValueError (
595+ "Failed to send password reset email to the new user."
596+ )
597+ return JsonResponse (
598+ serializers .UserResponse .model_validate (user ).model_dump (), status = 200
599+ )
600+ except ValidationError as e :
601+ return JsonResponse ({"error" : e .json ()}, status = 400 )
602+ except IntegrityError as e :
603+ return JsonResponse ({"error" : str (e )}, status = 409 )
604+
491605
492606@method_decorator (accessible_for (roles = {"admin" , "editor" }), name = "post" )
493607class FileView (View ):
0 commit comments