From 9289166c0e272eade8dda8a3541c389adb4261f5 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Wed, 4 Dec 2024 15:39:39 +0800 Subject: [PATCH] Add patch endpoint for documents --- .../0002_alter_document_document_type.py | 18 ++++++++++++++++++ docmanager_backend/documents/models.py | 2 +- docmanager_backend/documents/serializers.py | 5 ----- docmanager_backend/documents/urls.py | 2 ++ docmanager_backend/documents/views.py | 14 +++++++++++++- 5 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 docmanager_backend/documents/migrations/0002_alter_document_document_type.py diff --git a/docmanager_backend/documents/migrations/0002_alter_document_document_type.py b/docmanager_backend/documents/migrations/0002_alter_document_document_type.py new file mode 100644 index 0000000..e2d1b81 --- /dev/null +++ b/docmanager_backend/documents/migrations/0002_alter_document_document_type.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.3 on 2024-12-04 07:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("documents", "0001_initial"), + ] + + operations = [ + migrations.AlterField( + model_name="document", + name="document_type", + field=models.CharField(max_length=128), + ), + ] diff --git a/docmanager_backend/documents/models.py b/docmanager_backend/documents/models.py index 0616501..484a115 100644 --- a/docmanager_backend/documents/models.py +++ b/docmanager_backend/documents/models.py @@ -7,7 +7,7 @@ class Document(models.Model): name = models.CharField(max_length=100) document_type = models.CharField( - max_length=32, null=False, blank=False + max_length=128, null=False, blank=False ) number_pages = models.IntegerField(null=False, blank=False) ocr_metadata = models.TextField(null=True, blank=True) diff --git a/docmanager_backend/documents/serializers.py b/docmanager_backend/documents/serializers.py index 894d520..6ef47eb 100644 --- a/docmanager_backend/documents/serializers.py +++ b/docmanager_backend/documents/serializers.py @@ -3,19 +3,14 @@ from .models import Document class DocumentUpdateSerializer(serializers.ModelSerializer): - # For Head to edit document info - file = serializers.FileField(required=False) class Meta: model = Document fields = [ "name", - "file", "document_type", "number_pages", - "date_uploaded", ] - read_only_fields = ["id"] class DocumentUploadSerializer(serializers.ModelSerializer): diff --git a/docmanager_backend/documents/urls.py b/docmanager_backend/documents/urls.py index 0438f73..5b21340 100644 --- a/docmanager_backend/documents/urls.py +++ b/docmanager_backend/documents/urls.py @@ -4,10 +4,12 @@ from .views import ( DocumentDeleteView, DocumentListView, DocumentStaffListView, + DocumentUpdateView ) urlpatterns = [ path("upload/", DocumentUploadView.as_view()), + path("update//", DocumentUpdateView.as_view()), path("delete//", DocumentDeleteView.as_view()), path("list/", DocumentListView.as_view()), path("list/staff/", DocumentStaffListView.as_view()), diff --git a/docmanager_backend/documents/views.py b/docmanager_backend/documents/views.py index 60a5e8a..0bcb527 100644 --- a/docmanager_backend/documents/views.py +++ b/docmanager_backend/documents/views.py @@ -4,13 +4,25 @@ from .serializers import ( DocumentFileSerializer, DocumentUploadSerializer, DocumentDeleteSerializer, + DocumentUpdateSerializer ) from rest_framework.permissions import IsAuthenticated from rest_framework.pagination import PageNumberPagination -from accounts.permissions import IsStaff +from accounts.permissions import IsStaff, IsHead from .models import Document +class DocumentUpdateView(generics.UpdateAPIView): + """ + Used by staff to upload documents. + """ + + http_method_names = ["patch"] + serializer_class = DocumentUpdateSerializer + queryset = Document.objects.all() + permission_classes = [IsAuthenticated, IsHead] + + class DocumentUploadView(generics.CreateAPIView): """ Used by staff to upload documents.