diff --git a/care/emr/api/viewsets/file_upload.py b/care/emr/api/viewsets/file_upload.py index 57e459ed57..a430fd4e18 100644 --- a/care/emr/api/viewsets/file_upload.py +++ b/care/emr/api/viewsets/file_upload.py @@ -1,13 +1,17 @@ +from pydantic import BaseModel +from rest_framework.decorators import action from rest_framework.generics import get_object_or_404 +from rest_framework.response import Response from care.emr.api.viewsets.base import EMRModelViewSet from care.emr.models import FileUpload from care.emr.resources.file_upload.spec import ( FileUploadCreateSpec, FileUploadRetrieveSpec, - FileUploadUpdateSpec, + FileUploadUpdateSpec, FileUploadListSpec, ) +from django.utils import timezone def file_authorizer(file_type, associating_id, permission): return @@ -18,6 +22,7 @@ class FileUploadViewSet(EMRModelViewSet): pydantic_model = FileUploadCreateSpec pydantic_retrieve_model = FileUploadRetrieveSpec pydantic_update_model = FileUploadUpdateSpec + pydantic_read_model = FileUploadListSpec def authorize_create(self, instance): pass @@ -45,3 +50,27 @@ def get_queryset(self): obj = get_object_or_404(FileUpload, external_id=self.kwargs["external_id"]) file_authorizer(obj.file_type, obj.associating_id, "read") return super().get_queryset() + + + @action(detail=True, methods=["POST"]) + def mark_upload_completed(self, request,*args, **kwargs): + obj = self.get_object() + file_authorizer(obj.file_type, obj.associating_id, "write") + obj.upload_completed = True + obj.save(update_fields=["upload_completed"]) + return Response(FileUploadListSpec.serialize(obj).to_json()) + + class ArchiveRequestSpec(BaseModel): + archive_reason : str + + @action(detail=True, methods=["POST"]) + def archive(self, request,*args, **kwargs): + obj = self.get_object() + request_data = self.ArchiveRequestSpec(**request.data) + file_authorizer(obj.file_type, obj.associating_id, "write") + obj.is_archived = True + obj.archive_reason = request_data.archive_reason + obj.archived_datetime = timezone.now() + obj.archived_by = request.user + obj.save(update_fields=["is_archived" , "archive_reason" , "archived_datetime" , "archived_by"]) + return Response(FileUploadListSpec.serialize(obj).to_json()) diff --git a/care/emr/resources/file_upload/spec.py b/care/emr/resources/file_upload/spec.py index 305abe7ca0..75744bbe30 100644 --- a/care/emr/resources/file_upload/spec.py +++ b/care/emr/resources/file_upload/spec.py @@ -28,8 +28,7 @@ class FileUploadBaseSpec(EMRResource): class FileUploadUpdateSpec(FileUploadBaseSpec): - upload_completed: bool - + pass class FileUploadCreateSpec(FileUploadBaseSpec): original_name: str