drop v1 API and rename id columns
This commit is contained in:
parent
6968c38e68
commit
aaa11c3b60
21 changed files with 179 additions and 686 deletions
|
@ -1,27 +0,0 @@
|
|||
from rest_framework import serializers, viewsets, routers
|
||||
|
||||
from files.models import File
|
||||
|
||||
|
||||
class FileSerializer(serializers.ModelSerializer):
|
||||
data = serializers.CharField(max_length=1000000, write_only=True)
|
||||
|
||||
class Meta:
|
||||
model = File
|
||||
fields = ['hash', 'data']
|
||||
read_only_fields = ['hash']
|
||||
|
||||
|
||||
class FileViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = FileSerializer
|
||||
queryset = File.objects.all()
|
||||
lookup_field = 'hash'
|
||||
permission_classes = []
|
||||
authentication_classes = []
|
||||
|
||||
|
||||
router = routers.SimpleRouter(trailing_slash=False)
|
||||
router.register(r'files', FileViewSet, basename='files')
|
||||
router.register(r'file', FileViewSet, basename='files')
|
||||
|
||||
urlpatterns = router.urls
|
|
@ -1,65 +0,0 @@
|
|||
import os
|
||||
from django.http import HttpResponse
|
||||
from django.urls import path
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import api_view, permission_classes, authentication_classes
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.settings import MEDIA_ROOT
|
||||
from files.models import File
|
||||
|
||||
|
||||
@swagger_auto_schema(method='GET', auto_schema=None)
|
||||
@api_view(['GET'])
|
||||
@permission_classes([])
|
||||
@authentication_classes([])
|
||||
def media_urls(request, hash):
|
||||
try:
|
||||
file = File.objects.get(hash=hash)
|
||||
hash_path = file.file
|
||||
return HttpResponse(status=status.HTTP_200_OK,
|
||||
content_type=file.mime_type,
|
||||
headers={
|
||||
'X-Accel-Redirect': f'/redirect_media/{hash_path}',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}) # TODO Expires and Cache-Control
|
||||
|
||||
except File.DoesNotExist:
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
@swagger_auto_schema(method='GET', auto_schema=None)
|
||||
@api_view(['GET'])
|
||||
@permission_classes([])
|
||||
@authentication_classes([])
|
||||
def thumbnail_urls(request, hash):
|
||||
size = 200
|
||||
try:
|
||||
file = File.objects.get(hash=hash)
|
||||
hash_path = file.file
|
||||
if not os.path.exists(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}'):
|
||||
from PIL import Image
|
||||
image = Image.open(file.file)
|
||||
image.thumbnail((size, size))
|
||||
rgb_image = image.convert('RGB')
|
||||
thumb_dir = os.path.dirname(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}')
|
||||
if not os.path.exists(thumb_dir):
|
||||
os.makedirs(thumb_dir)
|
||||
rgb_image.save(MEDIA_ROOT + f'/thumbnails/{size}/{hash_path}', 'jpeg', quality=90)
|
||||
|
||||
return HttpResponse(status=status.HTTP_200_OK,
|
||||
content_type="image/jpeg",
|
||||
headers={
|
||||
'X-Accel-Redirect': f'/redirect_thumbnail/{size}/{hash_path}',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}) # TODO Expires and Cache-Control
|
||||
|
||||
except File.DoesNotExist:
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('thumbs/<path:hash>', thumbnail_urls),
|
||||
path('images/<path:hash>', media_urls),
|
||||
]
|
|
@ -1,68 +0,0 @@
|
|||
from django.test import TestCase, Client
|
||||
from django.core.files.base import ContentFile
|
||||
|
||||
from files.models import File
|
||||
from inventory.models import Event, Container, Item
|
||||
|
||||
client = Client()
|
||||
|
||||
|
||||
class FileTestCase(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.event = Event.objects.create(slug='EVENT', name='Event')
|
||||
self.box = Container.objects.create(name='BOX')
|
||||
|
||||
def test_create_file_raw(self):
|
||||
from hashlib import sha256
|
||||
content = b"foo"
|
||||
chash = sha256(content).hexdigest()
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
file = File.objects.create(file=ContentFile(b"foo"), mime_type='text/plain', hash=chash, item=item)
|
||||
file.save()
|
||||
self.assertEqual(1, len(File.objects.all()))
|
||||
self.assertEqual(content, File.objects.all()[0].file.read())
|
||||
self.assertEqual(chash, File.objects.all()[0].hash)
|
||||
|
||||
def test_list_files(self):
|
||||
import base64
|
||||
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = client.get('/api/1/files')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()[0]['hash'], item.hash)
|
||||
self.assertEqual(len(response.json()[0]['hash']), 64)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
self.assertEqual(File.objects.all()[0].file.read(), b"foo")
|
||||
|
||||
def test_one_file(self):
|
||||
import base64
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = client.get(f'/api/1/file/{item.hash}')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['hash'], item.hash)
|
||||
self.assertEqual(len(response.json()['hash']), 64)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
self.assertEqual(File.objects.all()[0].file.read(), b"foo")
|
||||
|
||||
def test_create_file(self):
|
||||
import base64
|
||||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
response = client.post('/api/1/file',
|
||||
{'data': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(len(response.json()['hash']), 64)
|
||||
self.assertEqual(len(File.objects.all()), 1)
|
||||
self.assertEqual(File.objects.all()[0].file.read(), b"foo")
|
||||
|
||||
def test_delete_file(self):
|
||||
import base64
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
file = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"bar").decode('utf-8'))
|
||||
self.assertEqual(len(File.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/file/{file.hash}')
|
||||
self.assertEqual(response.status_code, 204)
|
Loading…
Add table
Add a link
Reference in a new issue