Add patch endpoint for documents

This commit is contained in:
Keannu Bernasol 2024-12-04 15:39:39 +08:00
parent 7584f09b62
commit 9289166c0e
5 changed files with 34 additions and 7 deletions

View file

@ -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),
),
]

View file

@ -7,7 +7,7 @@ class Document(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
document_type = models.CharField( 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) number_pages = models.IntegerField(null=False, blank=False)
ocr_metadata = models.TextField(null=True, blank=True) ocr_metadata = models.TextField(null=True, blank=True)

View file

@ -3,19 +3,14 @@ from .models import Document
class DocumentUpdateSerializer(serializers.ModelSerializer): class DocumentUpdateSerializer(serializers.ModelSerializer):
# For Head to edit document info
file = serializers.FileField(required=False)
class Meta: class Meta:
model = Document model = Document
fields = [ fields = [
"name", "name",
"file",
"document_type", "document_type",
"number_pages", "number_pages",
"date_uploaded",
] ]
read_only_fields = ["id"]
class DocumentUploadSerializer(serializers.ModelSerializer): class DocumentUploadSerializer(serializers.ModelSerializer):

View file

@ -4,10 +4,12 @@ from .views import (
DocumentDeleteView, DocumentDeleteView,
DocumentListView, DocumentListView,
DocumentStaffListView, DocumentStaffListView,
DocumentUpdateView
) )
urlpatterns = [ urlpatterns = [
path("upload/", DocumentUploadView.as_view()), path("upload/", DocumentUploadView.as_view()),
path("update/<int:pk>/", DocumentUpdateView.as_view()),
path("delete/<int:pk>/", DocumentDeleteView.as_view()), path("delete/<int:pk>/", DocumentDeleteView.as_view()),
path("list/", DocumentListView.as_view()), path("list/", DocumentListView.as_view()),
path("list/staff/", DocumentStaffListView.as_view()), path("list/staff/", DocumentStaffListView.as_view()),

View file

@ -4,13 +4,25 @@ from .serializers import (
DocumentFileSerializer, DocumentFileSerializer,
DocumentUploadSerializer, DocumentUploadSerializer,
DocumentDeleteSerializer, DocumentDeleteSerializer,
DocumentUpdateSerializer
) )
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework.pagination import PageNumberPagination from rest_framework.pagination import PageNumberPagination
from accounts.permissions import IsStaff from accounts.permissions import IsStaff, IsHead
from .models import Document 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): class DocumentUploadView(generics.CreateAPIView):
""" """
Used by staff to upload documents. Used by staff to upload documents.