Skip to content

Commit

Permalink
Fix Upload spec
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshhari committed Dec 28, 2024
1 parent f57cdcb commit 102179e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
31 changes: 30 additions & 1 deletion care/emr/api/viewsets/file_upload.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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())
3 changes: 1 addition & 2 deletions care/emr/resources/file_upload/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class FileUploadBaseSpec(EMRResource):


class FileUploadUpdateSpec(FileUploadBaseSpec):
upload_completed: bool

pass

class FileUploadCreateSpec(FileUploadBaseSpec):
original_name: str
Expand Down

0 comments on commit 102179e

Please sign in to comment.