Skip to content

Commit

Permalink
gzip models during download (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien Gasser authored Feb 25, 2020
1 parent d9adc01 commit 7a58c60
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 14 additions & 0 deletions backend/substrapp/views/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import tempfile
import logging
from django.http import Http404
from functools import wraps
from django.middleware.gzip import GZipMiddleware
from rest_framework import status, mixins
from rest_framework.decorators import action
from rest_framework.response import Response
Expand Down Expand Up @@ -116,6 +118,17 @@ def details(self, request, *args, **kwargs):
return Response(data, status=status.HTTP_200_OK)


def gzip_action(func):
gz = GZipMiddleware()

@wraps(func)
def wrapper(self, request, *args, **kwargs):
resp = func(self, request, *args, **kwargs)
return gz.process_response(request, resp)

return wrapper


class ModelPermissionViewSet(PermissionMixin,
GenericViewSet):

Expand All @@ -137,6 +150,7 @@ def has_access(self, user, asset):

return permissions['public'] or node_id in permissions['authorizedIDs']

@gzip_action
@action(detail=True)
def file(self, request, *args, **kwargs):
return self.download_local_file(request, django_field='file')
8 changes: 4 additions & 4 deletions backend/substrapp/views/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os


from django.http import FileResponse
from django.http import FileResponse, HttpResponse
from rest_framework.authentication import BasicAuthentication, TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
Expand Down Expand Up @@ -106,11 +106,11 @@ def download_local_file(self, request, django_field, ledger_field=None):
try:
asset = get_object_from_ledger(pk, self.ledger_query_call)
except LedgerError as e:
return Response({'message': str(e.msg)}, status=e.status)
return HttpResponse({'message': str(e.msg)}, status=e.status)

if not self.has_access(request.user, asset):
return Response({'message': 'Unauthorized'},
status=status.HTTP_403_FORBIDDEN)
return HttpResponse({'message': 'Unauthorized'},
status=status.HTTP_403_FORBIDDEN)

if not ledger_field:
ledger_field = django_field
Expand Down

0 comments on commit 7a58c60

Please sign in to comment.