166 lines
6.1 KiB
Python
166 lines
6.1 KiB
Python
import uuid
|
|
|
|
from drf_spectacular.utils import extend_schema
|
|
from rest_framework.permissions import AllowAny, IsAdminUser, IsAuthenticated # type: ignore
|
|
from rest_framework.viewsets import ModelViewSet # type: ignore
|
|
from rest_framework.views import APIView # type: ignore
|
|
from rest_framework.generics import GenericAPIView # type: ignore
|
|
from core.utils.views import BaseApiViewMixin
|
|
|
|
from rest_framework.request import HttpRequest # type: ignore
|
|
from rest_framework.response import Response # 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
|
|
)
|
|
|
|
|
|
###################################################################################
|
|
# @view-set | ALL - /contracts
|
|
###################################################################################
|
|
@extend_schema(tags=["Contracts"])
|
|
class ContractViewSet(BaseViewSetMixin, ModelViewSet):
|
|
queryset = ContractModel.objects.all()
|
|
serializer_class = ListContractSerializer
|
|
permission_classes = [AllowAny]
|
|
|
|
action_permission_classes = { # type: ignore
|
|
"list": [IsAdminUser],
|
|
"retrieve": [IsAdminUser],
|
|
"create": [AllowAny],
|
|
"update": [IsAdminUser],
|
|
"destroy": [IsAdminUser],
|
|
}
|
|
action_serializer_class = { # type: ignore
|
|
"list": ListContractSerializer,
|
|
"retrieve": RetrieveContractSerializer,
|
|
"create": CreateContractSerializer,
|
|
"update": UpdateContractSerializer,
|
|
"destroy": DestroyContractSerializer,
|
|
}
|
|
|
|
def create(
|
|
self,
|
|
request: HttpRequest,
|
|
*args: object,
|
|
**kwargs: object,
|
|
) -> Response:
|
|
#! TODO: checkout if user has access to create new contract.
|
|
return super().create(request, *args, **kwargs) # type: ignore
|
|
|
|
|
|
###################################################################################
|
|
# @api-view | GET - /contracts/{id}/owners
|
|
###################################################################################
|
|
@extend_schema(tags=["Contract Owners"])
|
|
class ContractOwnerApiView(BaseApiViewMixin, GenericAPIView): # type: ignore
|
|
queryset = ContractModel.objects.all()
|
|
permission_classes = [AllowAny]
|
|
|
|
method_permission_classes = {
|
|
"get": [AllowAny]
|
|
}
|
|
method_serializer_class = {
|
|
"get": RetrieveContractOwnerSerializer,
|
|
}
|
|
|
|
@extend_schema(
|
|
summary="Get List Of Owners",
|
|
description="Get list of owners"
|
|
)
|
|
def get(self, *args: object, **kwargs: object) -> Response:
|
|
contract = self.get_object()
|
|
owners = ContractOwnerModel.objects.filter(contract=contract)
|
|
ser = self.get_serializer(instance=owners, many=True)
|
|
return Response(ser.data, status.HTTP_200_OK)
|
|
|
|
|
|
###################################################################################
|
|
# @api-view | GET - /contracts/{id}/files
|
|
###################################################################################
|
|
@extend_schema(tags=["Contract Attached Files"])
|
|
class ContractAttachedFileApiView(BaseApiViewMixin, GenericAPIView): # type: ignore
|
|
queryset = ContractModel.objects.all()
|
|
permission_classes = [AllowAny]
|
|
|
|
method_permission_classes = {
|
|
"get": [AllowAny]
|
|
}
|
|
method_serializer_class = {
|
|
"get": ListContractAttachedFileSerializer,
|
|
}
|
|
|
|
@extend_schema(
|
|
summary="Get List Of Files",
|
|
description="Get List Of Files"
|
|
)
|
|
def get(self, *args: object, **kwargs: object) -> Response:
|
|
contract = self.get_object()
|
|
files = ContractAttachedFileModel.objects.filter(contract=contract)
|
|
ser = self.get_serializer(instance=files, many=True)
|
|
return Response(data=ser.data, status=status.HTTP_200_OK)
|
|
|
|
|
|
###################################################################################
|
|
# @api-view | GET - /contract-owners/{id}/contract
|
|
###################################################################################
|
|
@extend_schema(tags=["Contract Owners"])
|
|
class ContractDetailApiView(BaseApiViewMixin, GenericAPIView): # type: ignore
|
|
queryset = ContractOwnerModel.objects.all()
|
|
permission_classes = [AllowAny]
|
|
|
|
method_permission_classes = {
|
|
"get": [AllowAny]
|
|
}
|
|
method_serializer_class = {
|
|
"get": RetrieveContractSerializer
|
|
}
|
|
|
|
@extend_schema(
|
|
summary="Uploads a file for contract attached files",
|
|
description="Creates a file for contract attached files.",
|
|
)
|
|
def get(self, request: HttpRequest, pk: uuid.UUID, *args: object, **kwargs: object) -> Response:
|
|
contract = ContractModel.objects.filter(owners__id=pk)[0]
|
|
ser = self.get_serializer(instance=contract) # type: ignore
|
|
return Response(ser.data, status=status.HTTP_200_OK)
|
|
|
|
|
|
###################################################################################
|
|
# @api-view | GET - /folders/{id}/contracts
|
|
###################################################################################
|
|
@extend_schema(tags=["Contracts"])
|
|
class ListFolderContractsApiView(BaseApiViewMixin, GenericAPIView): # type: ignore
|
|
permission_classes = [AllowAny]
|
|
queryset = ContractOwnerModel.objects.all()
|
|
|
|
method_permission_classes = {
|
|
"get": [AllowAny]
|
|
}
|
|
method_serializer_class = {
|
|
"get": ListContractSerializer
|
|
}
|
|
|
|
@extend_schema(
|
|
summary="Get Contracts related to folders",
|
|
description="Get Contracts related to folders",
|
|
)
|
|
def get(self, request: HttpRequest, pk: uuid.UUID, *args: object, **kwargs: object) -> Response:
|
|
contracts = ContractModel.objects.filter(folders__id=pk)
|
|
ser = self.get_serializer(instance=contracts, many=True) # type: ignore
|
|
return Response(ser.data, status.HTTP_200_OK)
|