124 lines
4.0 KiB
Python
124 lines
4.0 KiB
Python
import uuid
|
|
|
|
from drf_spectacular.utils import extend_schema
|
|
from rest_framework.permissions import AllowAny, IsAdminUser # type: ignore
|
|
from rest_framework.viewsets import ModelViewSet # type: ignore
|
|
from rest_framework.views import APIView # type: ignore
|
|
|
|
from rest_framework.request import HttpRequest # type: ignore
|
|
from rest_framework.response import Response # type: ignore
|
|
from rest_framework.decorators import action # type: ignore
|
|
from rest_framework.generics import get_object_or_404 # type: ignore
|
|
from rest_framework import status # type: ignore
|
|
|
|
from django_core.mixins import BaseViewSetMixin # type: ignore
|
|
from core.apps.contracts.models import (
|
|
ContractModel,
|
|
ContractAttachedFileModel,
|
|
ContractOwnerModel
|
|
)
|
|
from core.apps.contracts.serializers import (
|
|
CreateContractSerializer,
|
|
ListContractSerializer,
|
|
RetrieveContractSerializer,
|
|
UpdateContractSerializer,
|
|
DestroyContractSerializer,
|
|
|
|
ListContractAttachedFileSerializer,
|
|
RetrieveContractOwnerSerializer
|
|
)
|
|
|
|
|
|
@extend_schema(tags=["Contract"])
|
|
class ContractView(BaseViewSetMixin, ModelViewSet):
|
|
queryset = ContractModel.objects.all()
|
|
serializer_class = ListContractSerializer
|
|
permission_classes = [AllowAny]
|
|
|
|
action_permission_classes = { # type: ignore
|
|
"list": [IsAdminUser],
|
|
"retrieve": [IsAdminUser],
|
|
"create": [IsAdminUser],
|
|
"update": [IsAdminUser],
|
|
"destroy": [IsAdminUser],
|
|
"list_file": [AllowAny],
|
|
"list_owner": [AllowAny],
|
|
}
|
|
action_serializer_class = { # type: ignore
|
|
"list": ListContractSerializer,
|
|
"retrieve": RetrieveContractSerializer,
|
|
"create": CreateContractSerializer,
|
|
"update": UpdateContractSerializer,
|
|
"destroy": DestroyContractSerializer,
|
|
"list_file": ListContractAttachedFileSerializer,
|
|
"list_owner": RetrieveContractOwnerSerializer,
|
|
}
|
|
|
|
@extend_schema(
|
|
summary="Get List Of Files",
|
|
description="Get List Of Files"
|
|
)
|
|
@action(url_path="files", detail=True, methods=["GET"])
|
|
def list_file(
|
|
self,
|
|
request: HttpRequest,
|
|
pk: uuid.UUID,
|
|
*args: object,
|
|
**kwargs: object
|
|
) -> Response:
|
|
contract = get_object_or_404(ContractModel, pk=pk)
|
|
files = ContractAttachedFileModel.objects.filter(contract=contract)
|
|
ser = self.get_serializer(instance=files, many=True) # type: ignore
|
|
return Response(data=ser.data, status=status.HTTP_200_OK)
|
|
|
|
@extend_schema(
|
|
summary="Get List Of Owners",
|
|
description="Get list of owners"
|
|
)
|
|
@action(url_path="owners", detail=True, methods=["GET"])
|
|
def list_owner(
|
|
self,
|
|
request: HttpRequest,
|
|
pk: uuid.UUID,
|
|
*args: object,
|
|
**kwargs: object
|
|
) -> Response:
|
|
contract = get_object_or_404(ContractModel, pk=pk)
|
|
owners = ContractOwnerModel.objects.filter(contract=contract)
|
|
ser = self.get_serializer(instance=owners, many=True) # type: ignore
|
|
return Response(ser.data, status.HTTP_200_OK)
|
|
|
|
|
|
class ContractDetailView(APIView):
|
|
permission_classes = [AllowAny]
|
|
|
|
@extend_schema(
|
|
summary="Uploads a file for contract attached files",
|
|
description="Creates a file for contract attached files.",
|
|
)
|
|
def get(
|
|
self,
|
|
request: HttpRequest,
|
|
owner_id: uuid.UUID,
|
|
*args: object,
|
|
**kwargs: object
|
|
) -> Response:
|
|
contract = ContractModel.objects.filter(owners__id=owner_id)[0]
|
|
ser = RetrieveContractSerializer(instance=contract)
|
|
return Response(ser.data, status=status.HTTP_200_OK)
|
|
|
|
|
|
class ListFolderContractsView(APIView):
|
|
permission_classes = [IsAdminUser]
|
|
|
|
def get(
|
|
self,
|
|
request: HttpRequest,
|
|
pk: uuid.UUID,
|
|
*args: object,
|
|
**kwargs: object
|
|
) -> Response:
|
|
contracts = ContractModel.objects.filter(folders__id=pk)
|
|
ser = ListContractSerializer(instance=contracts, many=True)
|
|
return Response(ser.data, status.HTTP_200_OK)
|