72 lines
2.3 KiB
Python
72 lines
2.3 KiB
Python
import uuid
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
from rest_framework import status
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.response import Response
|
|
|
|
from apps.common.serializers import AuthOrganizationSerializer
|
|
from apps.core.models import AuthOrganization
|
|
|
|
|
|
@api_view(["GET"])
|
|
@permission_classes([IsAuthenticated])
|
|
def get_organization_list(request):
|
|
organizations = AuthOrganization.objects.all()
|
|
serializer = AuthOrganizationSerializer(organizations, many=True)
|
|
return Response(serializer.data)
|
|
|
|
|
|
@api_view(["GET"])
|
|
@permission_classes([IsAuthenticated])
|
|
def read_organization(request):
|
|
organization_id = request.GET.get("id", None)
|
|
organization = get_object_or_404(AuthOrganization, id=organization_id)
|
|
serializer = AuthOrganizationSerializer(organization)
|
|
return Response(serializer.data)
|
|
|
|
|
|
@api_view(["POST"])
|
|
@permission_classes([IsAuthenticated])
|
|
def add_organization(request):
|
|
data = request.data
|
|
data["id"] = str(uuid.uuid4())
|
|
serializer = AuthOrganizationSerializer(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response({"message": "Organization added successfully"})
|
|
|
|
return Response(
|
|
{"error": "Invalid data", "details": serializer.errors},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
|
|
@api_view(["PUT"])
|
|
@permission_classes([IsAuthenticated])
|
|
def update_organization(request):
|
|
data = request.data
|
|
organization = get_object_or_404(AuthOrganization, id=data.get("id"))
|
|
serializer = AuthOrganizationSerializer(
|
|
instance=organization, data=request.data, partial=True
|
|
)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response({"message": "Organization updated successfully"})
|
|
|
|
return Response(
|
|
{"error": "Invalid data", "details": serializer.errors},
|
|
status=status.HTTP_400_BAD_REQUEST,
|
|
)
|
|
|
|
|
|
@api_view(["DELETE"])
|
|
@permission_classes([IsAuthenticated])
|
|
def delete_organization(request):
|
|
organization_id = request.GET.get("id", None)
|
|
organization = get_object_or_404(AuthOrganization, id=organization_id)
|
|
organization.delete()
|
|
return Response({"message": "Organization deleted successfully"})
|