Started basic /books route. Got file and image saving. Started route documentation in README.md
parent
e0393611cf
commit
cde12d6b28
|
@ -0,0 +1,73 @@
|
||||||
|
# Alt-Text Backend
|
||||||
|
|
||||||
|
## ROUTES
|
||||||
|
|
||||||
|
### /books
|
||||||
|
|
||||||
|
#### GET
|
||||||
|
|
||||||
|
##### FUNCTION
|
||||||
|
|
||||||
|
Gets a list of books from database
|
||||||
|
|
||||||
|
##### PARAMS
|
||||||
|
|
||||||
|
- query (string) | ensures book titles must have ‘query’ as a substring
|
||||||
|
- skip (int) | skips ‘skip’ amount of books from the result
|
||||||
|
- limit (int) | limits the amount of books returned to ‘limit’
|
||||||
|
|
||||||
|
##### RETURNS
|
||||||
|
|
||||||
|
```
|
||||||
|
[
|
||||||
|
{
|
||||||
|
title: string,
|
||||||
|
description: string,
|
||||||
|
cover: string,
|
||||||
|
},
|
||||||
|
...
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
#### POST
|
||||||
|
|
||||||
|
##### FUNCTION
|
||||||
|
|
||||||
|
Adds a book to the database and starts initial processing
|
||||||
|
|
||||||
|
##### FIELDS
|
||||||
|
|
||||||
|
- title (string) | desired title for new book
|
||||||
|
- description (string) | desired description for new book
|
||||||
|
- cover (image_file) | desired cover image for new book
|
||||||
|
- file (file[.zip]) | book file (html and images bundled)
|
||||||
|
|
||||||
|
##### RETURNS
|
||||||
|
|
||||||
|
```
|
||||||
|
{
|
||||||
|
title: string,
|
||||||
|
description: string,
|
||||||
|
cover: string,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### /books/:bookid
|
||||||
|
|
||||||
|
#### GET
|
||||||
|
|
||||||
|
#### PATCH
|
||||||
|
|
||||||
|
#### DELETE
|
||||||
|
|
||||||
|
### /books/:bookid/images
|
||||||
|
|
||||||
|
#### GET
|
||||||
|
|
||||||
|
#### PATCH
|
||||||
|
|
||||||
|
### /books/:bookid/images/:imagesrc
|
||||||
|
|
||||||
|
#### GET
|
||||||
|
|
||||||
|
#### PATCH
|
Binary file not shown.
Binary file not shown.
|
@ -14,13 +14,13 @@ Including another URLconf
|
||||||
1. Import the include() function: from django.urls import include, path
|
1. Import the include() function: from django.urls import include, path
|
||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from .views import BooksView, ImagesView
|
from .views import BooksView # , ImagesView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("admin/", admin.site.urls),
|
# path("admin/", admin.site.urls),
|
||||||
path("api-auth/", include("rest_framework.urls")),
|
# path("api-auth/", include("rest_framework.urls")),
|
||||||
path("api/books", BooksView.as_view()),
|
path("api/books", BooksView.as_view()),
|
||||||
path("api/images", ImagesView.as_view()),
|
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,55 +1,84 @@
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import status
|
from rest_framework import status, permissions, serializers
|
||||||
from rest_framework import permissions
|
from rest_framework.exceptions import ValidationError
|
||||||
|
from rest_framework.parsers import FormParser, MultiPartParser
|
||||||
from rest_framework import serializers
|
from django.core.files.storage import default_storage
|
||||||
|
from django.core.files.base import ContentFile
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
|
||||||
class BookSerializer(serializers.Serializer):
|
class BookSerializer(serializers.Serializer):
|
||||||
bookdata = serializers.CharField()
|
title = serializers.CharField()
|
||||||
|
description = serializers.CharField()
|
||||||
|
cover = serializers.ImageField()
|
||||||
class ImageSerializer(serializers.Serializer):
|
file = serializers.FileField()
|
||||||
imagedata = serializers.CharField()
|
|
||||||
beforeContext = serializers.CharField(required=False)
|
|
||||||
afterContext = serializers.CharField(required=False)
|
|
||||||
|
|
||||||
|
|
||||||
class BooksView(APIView):
|
class BooksView(APIView):
|
||||||
|
parser_classes = (FormParser, MultiPartParser)
|
||||||
|
serializer_class = BookSerializer
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
data = {"books": "this is a book"}
|
# TODO: get's list of books given limits
|
||||||
|
data = {"books": []}
|
||||||
return Response(data, status=status.HTTP_200_OK)
|
return Response(data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
serializer = BookSerializer(data=request.data)
|
# validate request data
|
||||||
if serializer.is_valid():
|
serializer = self.serializer_class(data=request.data)
|
||||||
|
if not serializer.is_valid():
|
||||||
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
validated_data = serializer.validated_data
|
validated_data = serializer.validated_data
|
||||||
|
|
||||||
|
# perform initial book processing
|
||||||
|
file = validated_data["file"]
|
||||||
|
if not file.name.endswith(".zip"):
|
||||||
return Response(
|
return Response(
|
||||||
{"book": validated_data.get("bookdata")},
|
{"file": ["file must be a zip"]}, status=status.HTTP_400_BAD_REQUEST
|
||||||
|
)
|
||||||
|
id = uuid4()
|
||||||
|
books_path = "./books/"
|
||||||
|
default_storage.save(f"{books_path}{str(id)}.zip", ContentFile(file.read()))
|
||||||
|
|
||||||
|
# save cover image
|
||||||
|
covers_path = "./covers/"
|
||||||
|
default_storage.save(
|
||||||
|
f"{covers_path}{str(id)}.{validated_data['cover'].name.split('.')[-1]}",
|
||||||
|
ContentFile(validated_data["cover"].read()),
|
||||||
|
)
|
||||||
|
|
||||||
|
return Response(
|
||||||
|
{
|
||||||
|
"book": validated_data.get("title"),
|
||||||
|
"description": validated_data.get("description"),
|
||||||
|
},
|
||||||
status=status.HTTP_201_CREATED,
|
status=status.HTTP_201_CREATED,
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
||||||
|
|
||||||
|
|
||||||
class ImagesView(APIView):
|
# class ImageSerializer(serializers.Serializer):
|
||||||
def get(self, request, *args, **kwargs):
|
# imagedata = serializers.CharField()
|
||||||
data = {"images": "this is an image"}
|
# beforeContext = serializers.CharField(required=False)
|
||||||
return Response(data, status=status.HTTP_200_OK)
|
# afterContext = serializers.CharField(required=False)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
# class ImagesView(APIView):
|
||||||
serializer = ImageSerializer(data=request.data)
|
# def get(self, request, *args, **kwargs):
|
||||||
if serializer.is_valid():
|
# data = {"images": "this is an image"}
|
||||||
validated_data = serializer.validated_data
|
# return Response(data, status=status.HTTP_200_OK)
|
||||||
res = {"book": validated_data.get("imagedata")}
|
|
||||||
if validated_data.get("beforeContext"):
|
# def post(self, request, *args, **kwargs):
|
||||||
res["beforeContext"] = validated_data.get("beforeContext")
|
# serializer = ImageSerializer(data=request.data)
|
||||||
if validated_data.get("afterContext"):
|
# if serializer.is_valid():
|
||||||
res["afterContext"] = validated_data.get("afterContext")
|
# validated_data = serializer.validated_data
|
||||||
return Response(
|
# res = {"book": validated_data.get("imagedata")}
|
||||||
res,
|
# if validated_data.get("beforeContext"):
|
||||||
status=status.HTTP_201_CREATED,
|
# res["beforeContext"] = validated_data.get("beforeContext")
|
||||||
)
|
# if validated_data.get("afterContext"):
|
||||||
else:
|
# res["afterContext"] = validated_data.get("afterContext")
|
||||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
# return Response(
|
||||||
|
# res,
|
||||||
|
# status=status.HTTP_201_CREATED,
|
||||||
|
# )
|
||||||
|
# else:
|
||||||
|
# return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
Loading…
Reference in New Issue