From 77f788c47964df1613cf214e449abced7e6cda20 Mon Sep 17 00:00:00 2001 From: Samuel Gaist <samuel.gaist@idiap.ch> Date: Wed, 22 Apr 2020 11:56:00 +0200 Subject: [PATCH] [algorithms][api] Replace put with serializer --- beat/web/algorithms/api.py | 74 +++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/beat/web/algorithms/api.py b/beat/web/algorithms/api.py index d85922e0f..71b1f08c0 100755 --- a/beat/web/algorithms/api.py +++ b/beat/web/algorithms/api.py @@ -29,26 +29,28 @@ from django.http import Http404 from django.http import HttpResponse from django.http import HttpResponseForbidden from django.http import HttpResponseBadRequest +from django.http import HttpResponseNotAllowed from django.shortcuts import get_object_or_404 -from django.conf import settings - -import os from .models import Algorithm from .serializers import AlgorithmSerializer from .serializers import FullAlgorithmSerializer from .serializers import AlgorithmCreationSerializer +from .serializers import AlgorithmModSerializer from ..code.api import ShareCodeView, RetrieveUpdateDestroyCodeView from ..code.serializers import CodeDiffSerializer -from ..common.api import (CheckContributionNameView, ListContributionView, - ListCreateContributionView) +from ..common.api import ( + CheckContributionNameView, + ListContributionView, + ListCreateContributionView, +) from ..code.api import DiffView -#---------------------------------------------------------- +# ---------------------------------------------------------- class CheckAlgorithmNameView(CheckContributionNameView): @@ -56,10 +58,11 @@ class CheckAlgorithmNameView(CheckContributionNameView): This view sanitizes an algorithm name and checks whether it is already used. """ + model = Algorithm -#---------------------------------------------------------- +# ---------------------------------------------------------- class ShareAlgorithmView(ShareCodeView): @@ -67,21 +70,23 @@ class ShareAlgorithmView(ShareCodeView): This view allows to share an algorithm with other users and/or teams """ + model = Algorithm -#---------------------------------------------------------- +# ---------------------------------------------------------- class ListAlgorithmsView(ListContributionView): """ List all available algorithms """ + model = Algorithm serializer_class = AlgorithmSerializer -#---------------------------------------------------------- +# ---------------------------------------------------------- class ListCreateAlgorithmsView(ListCreateContributionView): @@ -89,13 +94,14 @@ class ListCreateAlgorithmsView(ListCreateContributionView): Read/Write end point that list the algorithms available from a given author and allows the creation of new algorithms """ + model = Algorithm serializer_class = AlgorithmSerializer writing_serializer_class = AlgorithmCreationSerializer - namespace = 'api_algorithms' + namespace = "api_algorithms" -#---------------------------------------------------------- +# ---------------------------------------------------------- class RetrieveUpdateDestroyAlgorithmsView(RetrieveUpdateDestroyCodeView): @@ -105,39 +111,30 @@ class RetrieveUpdateDestroyAlgorithmsView(RetrieveUpdateDestroyCodeView): model = Algorithm serializer_class = FullAlgorithmSerializer + writing_serializer_class = AlgorithmModSerializer - def do_update(self, request, author_name, object_name, version=None): - modified, algorithm = super(RetrieveUpdateDestroyAlgorithmsView, self).do_update(request, author_name, object_name, version) - - if modified: - # Delete existing experiments using the algorithm (code changed) - experiments = list(set(map(lambda x: x.experiment, - algorithm.blocks.iterator()))) - for experiment in experiments: experiment.delete() - return modified, algorithm - - -#---------------------------------------------------------- +# ---------------------------------------------------------- class DiffAlgorithmView(DiffView): """ This view shows the differences between two algorithms """ + model = Algorithm serializer_class = CodeDiffSerializer -#---------------------------------------------------------- +# ---------------------------------------------------------- def binary(request, author_name, object_name, version=None): """Returns the shared library of a binary algorithm """ - if request.method not in ['GET', 'POST']: - return HttpResponseNotAllowed(['GET', 'POST']) + if request.method not in ["GET", "POST"]: + return HttpResponseNotAllowed(["GET", "POST"]) # Retrieves the algorithm if version: @@ -148,8 +145,9 @@ def binary(request, author_name, object_name, version=None): version=int(version), ) else: - algorithm = Algorithm.objects.filter(author__username__iexact=author_name, - name__iexact=object_name).order_by('-version') + algorithm = Algorithm.objects.filter( + author__username__iexact=author_name, name__iexact=object_name + ).order_by("-version") if not algorithm: raise Http404() else: @@ -158,18 +156,22 @@ def binary(request, author_name, object_name, version=None): if not algorithm.is_binary(): raise Http404() - if request.method == 'GET': - (has_access, _, accessibility) = algorithm.accessibility_for(request.user, without_usable=True) + if request.method == "GET": + (has_access, _, accessibility) = algorithm.accessibility_for( + request.user, without_usable=True + ) if not has_access: raise Http404() binary_data = algorithm.source_code - response = HttpResponse(binary_data, content_type='application/octet-stream') + response = HttpResponse(binary_data, content_type="application/octet-stream") - response['Content-Length'] = len(binary_data) - response['Content-Disposition'] = 'attachment; filename=%d.so' % algorithm.version + response["Content-Length"] = len(binary_data) + response["Content-Disposition"] = ( + "attachment; filename=%d.so" % algorithm.version + ) return response @@ -177,12 +179,12 @@ def binary(request, author_name, object_name, version=None): if request.user.is_anonymous() or (request.user.username != author_name): return HttpResponseForbidden() - if 'binary' not in request.FILES: + if "binary" not in request.FILES: return HttpResponseBadRequest() - file = request.FILES['binary'] + file = request.FILES["binary"] - binary_data = b'' + binary_data = b"" for chunk in file.chunks(): binary_data += chunk -- GitLab