initial commit
This commit is contained in:
4
core/apps/contracts/views/__init__.py
Normal file
4
core/apps/contracts/views/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .attached_files import * # noqa
|
||||
from .contracts import * # noqa
|
||||
from .file_contents import * # noqa
|
||||
from .owners import * # noqa
|
||||
35
core/apps/contracts/views/attached_files.py
Normal file
35
core/apps/contracts/views/attached_files.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from django_core.mixins import BaseViewSetMixin
|
||||
from drf_spectacular.utils import extend_schema
|
||||
from rest_framework.permissions import AllowAny, IsAdminUser
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
|
||||
from core.apps.contracts.models import ContractAttachedFileModel
|
||||
from core.apps.contracts.serializers.attached_files import (
|
||||
CreateContractAttachedFileSerializer,
|
||||
ListContractAttachedFileSerializer,
|
||||
RetrieveContractAttachedFileSerializer,
|
||||
UpdateContractAttachedFileSerializer,
|
||||
DestroyContractAttachedFileSerializer,
|
||||
)
|
||||
|
||||
|
||||
@extend_schema(tags=["ContractAttachedFile"])
|
||||
class ContractAttachedFileView(BaseViewSetMixin, ModelViewSet):
|
||||
queryset = ContractAttachedFileModel.objects.all()
|
||||
serializer_class = ListContractAttachedFileSerializer
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
action_permission_classes = {
|
||||
"list": [IsAdminUser],
|
||||
"retrieve": [IsAdminUser],
|
||||
"create": [IsAdminUser],
|
||||
"update": [IsAdminUser],
|
||||
"destroy": [IsAdminUser],
|
||||
}
|
||||
action_serializer_class = {
|
||||
"list": ListContractAttachedFileSerializer,
|
||||
"retrieve": RetrieveContractAttachedFileSerializer,
|
||||
"create": CreateContractAttachedFileSerializer,
|
||||
"update": UpdateContractAttachedFileSerializer,
|
||||
"destroy": DestroyContractAttachedFileSerializer,
|
||||
}
|
||||
123
core/apps/contracts/views/contracts.py
Normal file
123
core/apps/contracts/views/contracts.py
Normal file
@@ -0,0 +1,123 @@
|
||||
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)
|
||||
35
core/apps/contracts/views/file_contents.py
Normal file
35
core/apps/contracts/views/file_contents.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from django_core.mixins import BaseViewSetMixin
|
||||
from drf_spectacular.utils import extend_schema
|
||||
from rest_framework.permissions import AllowAny, IsAdminUser
|
||||
from rest_framework.viewsets import ModelViewSet
|
||||
|
||||
from core.apps.contracts.models import ContractFileContentModel
|
||||
from core.apps.contracts.serializers.file_contents import (
|
||||
CreateContractFileContentSerializer,
|
||||
ListContractFileContentSerializer,
|
||||
RetrieveContractFileContentSerializer,
|
||||
UpdateContractFileContentSerializer,
|
||||
DestroyContractFileContentSerializer
|
||||
)
|
||||
|
||||
|
||||
@extend_schema(tags=["ContractFileContent"])
|
||||
class ContractFileContentView(BaseViewSetMixin, ModelViewSet):
|
||||
queryset = ContractFileContentModel.objects.all()
|
||||
serializer_class = ListContractFileContentSerializer
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
action_permission_classes = {
|
||||
"list": [IsAdminUser],
|
||||
"retrieve": [IsAdminUser],
|
||||
"create": [IsAdminUser],
|
||||
"update": [IsAdminUser],
|
||||
"destroy": [IsAdminUser],
|
||||
}
|
||||
action_serializer_class = {
|
||||
"list": ListContractFileContentSerializer,
|
||||
"retrieve": RetrieveContractFileContentSerializer,
|
||||
"create": CreateContractFileContentSerializer,
|
||||
"update": UpdateContractFileContentSerializer,
|
||||
"destroy": DestroyContractFileContentSerializer,
|
||||
}
|
||||
171
core/apps/contracts/views/owners.py
Normal file
171
core/apps/contracts/views/owners.py
Normal file
@@ -0,0 +1,171 @@
|
||||
import uuid
|
||||
from typing import cast
|
||||
|
||||
from django_core.mixins import BaseViewSetMixin # type: ignore
|
||||
from django.utils.translation import gettext as _
|
||||
from drf_spectacular.utils import extend_schema, OpenApiResponse
|
||||
from rest_framework.exceptions import PermissionDenied # type: ignore
|
||||
from rest_framework.decorators import action # type: ignore
|
||||
from rest_framework.permissions import AllowAny, IsAdminUser # type: ignore
|
||||
from rest_framework.viewsets import ModelViewSet # type: ignore
|
||||
from rest_framework.request import HttpRequest # type: ignore
|
||||
from rest_framework.response import Response # type: ignore
|
||||
from rest_framework import status # type: ignore
|
||||
from rest_framework.views import APIView # type: ignore
|
||||
from rest_framework.parsers import MultiPartParser, FormParser # type: ignore
|
||||
from rest_framework.generics import get_object_or_404 # type: ignore
|
||||
|
||||
from core.apps.contracts.models import (
|
||||
ContractOwnerModel,
|
||||
ContractAttachedFileModel
|
||||
)
|
||||
from core.apps.contracts.serializers import (
|
||||
CreateContractOwnerSerializer,
|
||||
ListContractOwnerSerializer,
|
||||
RetrieveContractOwnerSerializer,
|
||||
UpdateContractOwnerSerializer,
|
||||
DestroyContractOwnerSerializer,
|
||||
|
||||
RetrieveContractAttachedFileSerializer,
|
||||
CreateContractAttachedFileSerializer,
|
||||
CreateContractFileContentFromOwnerSerializer,
|
||||
)
|
||||
|
||||
|
||||
@extend_schema(tags=["ContractOwner"])
|
||||
class ContractOwnerView(BaseViewSetMixin, ModelViewSet):
|
||||
queryset = ContractOwnerModel.objects.all()
|
||||
serializer_class = ListContractOwnerSerializer
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
action_permission_classes = {
|
||||
"list": [IsAdminUser],
|
||||
"retrieve": [IsAdminUser],
|
||||
"create": [IsAdminUser],
|
||||
"update": [IsAdminUser],
|
||||
"destroy": [IsAdminUser],
|
||||
}
|
||||
action_serializer_class = { # type: ignore
|
||||
"list": ListContractOwnerSerializer,
|
||||
"retrieve": RetrieveContractOwnerSerializer,
|
||||
"create": CreateContractOwnerSerializer,
|
||||
"update": UpdateContractOwnerSerializer,
|
||||
"destroy": DestroyContractOwnerSerializer,
|
||||
}
|
||||
|
||||
|
||||
class ContractOwnerFileViewSet(BaseViewSetMixin, ModelViewSet):
|
||||
queryset = ContractOwnerModel.objects.all()
|
||||
serializer_class = RetrieveContractAttachedFileSerializer
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
# action_permission_classes = {
|
||||
# "create_file": [AllowAny],
|
||||
# "list_file": [AllowAny]
|
||||
# }
|
||||
action_serializer_class = { # type: ignore
|
||||
"list_file": RetrieveContractAttachedFileSerializer,
|
||||
"create_file": CreateContractAttachedFileSerializer,
|
||||
}
|
||||
|
||||
@extend_schema(
|
||||
summary="Contract Files Related to Owner",
|
||||
description="Contract Files Related to Owner",
|
||||
)
|
||||
@action(url_path="files", methods=["GET"], detail=True)
|
||||
def list_file(
|
||||
self,
|
||||
request: HttpRequest,
|
||||
*args: object,
|
||||
**kwargs: object,
|
||||
) -> Response:
|
||||
owner = cast(ContractOwnerModel, self.get_object())
|
||||
files = ContractAttachedFileModel.objects.filter(
|
||||
contract__owners=owner, contents__owner=owner
|
||||
).select_related("contents")
|
||||
serializer = self.get_serializer(instance=files, many=True)
|
||||
return Response(serializer.data, status.HTTP_200_OK)
|
||||
|
||||
@extend_schema(
|
||||
summary="Create Contract Files Related to Owner",
|
||||
description="Create Contract Files Related to Owner"
|
||||
)
|
||||
@action(url_path="files", methods=["GET"], detail=True)
|
||||
def create_file(
|
||||
self,
|
||||
request: HttpRequest,
|
||||
*args: object,
|
||||
**kwargs: object,
|
||||
) -> Response:
|
||||
owner = cast(
|
||||
ContractOwnerModel,
|
||||
self.get_queryset().select_related("contract")
|
||||
)
|
||||
if not owner.contract.allow_add_files:
|
||||
raise PermissionDenied(_("Attaching new files was restricted for this contract."))
|
||||
ser = self.get_serializer(data=request.data)
|
||||
ser.is_valid(raise_exception=True)
|
||||
ser.save() # type: ignore
|
||||
return Response(ser.data, status.HTTP_201_CREATED)
|
||||
|
||||
|
||||
class ContractAttachedFileDeleteView(APIView):
|
||||
permission_classes = [AllowAny]
|
||||
|
||||
@extend_schema(
|
||||
# request=ContractFileDeleteRequestSerializer,
|
||||
responses={204: OpenApiResponse(description="File successfully deleted.")},
|
||||
summary="Delete a file from contract",
|
||||
description="Deletes a contract-attached file if contract allows file deletion.",
|
||||
)
|
||||
def delete(
|
||||
self,
|
||||
request: HttpRequest,
|
||||
owner_id: uuid.UUID,
|
||||
file_id: uuid.UUID,
|
||||
*args: object,
|
||||
**kwargs: object
|
||||
) -> Response:
|
||||
owner = get_object_or_404(
|
||||
ContractOwnerModel.objects.all().select_related("contract"), pk=owner_id
|
||||
)
|
||||
if not owner.contract.allow_delete_files:
|
||||
raise PermissionDenied(_("Deleting attached files was restricted for this contract"))
|
||||
file = get_object_or_404(
|
||||
ContractAttachedFileModel.objects.all().select_related("contract"),
|
||||
pk=file_id
|
||||
)
|
||||
if owner.contract.pk != file.contract.pk:
|
||||
raise PermissionDenied(_("Contract have no such file attached"))
|
||||
file.delete()
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
|
||||
class UploadFileContentView(APIView):
|
||||
permission_classes = [AllowAny]
|
||||
parser_classes = [MultiPartParser, FormParser] # type: ignore
|
||||
|
||||
@extend_schema(
|
||||
summary="Uploads a file for contract attached files",
|
||||
description="Creates a file for contract attached files.",
|
||||
request=CreateContractFileContentFromOwnerSerializer,
|
||||
responses={201: CreateContractFileContentFromOwnerSerializer},
|
||||
)
|
||||
def post(
|
||||
self,
|
||||
request: HttpRequest,
|
||||
owner_id: uuid.UUID,
|
||||
file_id: uuid.UUID,
|
||||
*args: object,
|
||||
**kwargs: object
|
||||
) -> Response:
|
||||
serializer = CreateContractFileContentFromOwnerSerializer(
|
||||
data=request.data, # type: ignore
|
||||
context={
|
||||
"file_id": file_id,
|
||||
"contract_owner_id": owner_id,
|
||||
}
|
||||
)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save() # type: ignore
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
Reference in New Issue
Block a user