From 192a28ca8d21901f1bb2c0374ddd5bc33de0f2ee Mon Sep 17 00:00:00 2001 From: Samuel Gaist <samuel.gaist@idiap.ch> Date: Wed, 22 Apr 2020 11:52:08 +0200 Subject: [PATCH] [code][api] Use mod serializer in place of custom put impementation --- beat/web/code/api.py | 157 ------------------------------------------- 1 file changed, 157 deletions(-) diff --git a/beat/web/code/api.py b/beat/web/code/api.py index 1bce025e4..1a1a978c1 100755 --- a/beat/web/code/api.py +++ b/beat/web/code/api.py @@ -25,25 +25,17 @@ # # ############################################################################### -from django.utils import six -from django.shortcuts import get_object_or_404 -from django.core.exceptions import ValidationError from rest_framework import generics from rest_framework.response import Response -from rest_framework.exceptions import PermissionDenied, ParseError -from rest_framework import serializers from ..common.responses import ForbiddenResponse from ..common.api import ShareView, RetrieveUpdateDestroyContributionView -from ..common.utils import validate_restructuredtext, ensure_html from ..common.serializers import DiffSerializer from ..code.models import Code from .serializers import CodeSharingSerializer, CodeSerializer -import simplejson as json - class ShareCodeView(ShareView): serializer_class = CodeSharingSerializer @@ -102,131 +94,6 @@ class RetrieveUpdateDestroyCodeView(RetrieveUpdateDestroyContributionView): model = Code serializer_class = CodeSerializer - def do_update(self, request, author_name, object_name, version=None): - if version is None: - raise ValidationError({"version": "A version number must be provided"}) - - try: - data = request.data - except ParseError as e: - raise serializers.ValidationError({"data": str(e)}) - else: - if not data: - raise serializers.ValidationError({"data": "Empty"}) - - if "short_description" in data: - if not (isinstance(data["short_description"], six.string_types)): - raise ValidationError( - {"short_description": "Invalid short_description data"} - ) - short_description = data["short_description"] - else: - short_description = None - - if "description" in data: - if not (isinstance(data["description"], six.string_types)): - raise serializers.ValidationError( - {"description": "Invalid description data"} - ) - description = data["description"] - try: - validate_restructuredtext(description) - except ValidationError as errors: - raise serializers.ValidationError( - {"description": [error for error in errors]} - ) - else: - description = None - - if "declaration" in data: - if isinstance(data["declaration"], dict): - json_declaration = data["declaration"] - declaration = json.dumps(json_declaration, indent=4) - elif isinstance(data["declaration"], six.string_types): - declaration = data["declaration"] - try: - json_declaration = json.loads(declaration) - except Exception: - raise serializers.ValidationError( - {"declaration": "Invalid declaration data"} - ) - else: - raise serializers.ValidationError( - {"declaration": "Invalid declaration data"} - ) - - if "description" in json_declaration: - if short_description is not None: - raise serializers.ValidationError( - { - "short_description": "A short description is already provided in the declaration" - } - ) - - short_description = json_declaration["description"] - elif short_description is not None: - json_declaration["description"] = short_description - declaration = json.dumps(json_declaration, indent=4) - else: - declaration = None - json_declaration = None - - if (short_description is not None) and ( - len(short_description) - > self.model._meta.get_field("short_description").max_length - ): - raise ValidationError({"short_description": "Short description too long"}) - - if "code" in data: - if not (isinstance(data["code"], six.string_types)): - raise ValidationError({"code": "Invalid code data"}) - code = data["code"] - else: - code = None - - # Retrieve the object - db_object = get_object_or_404( - self.model, - author__username__iexact=author_name, - name__iexact=object_name, - version=version, - ) - - # Check that the object can still be modified (if applicable, the - # documentation can always be modified) - if ((declaration is not None) or (code is not None)) and not ( - db_object.modifiable() - ): - raise PermissionDenied( - "The {} isn't modifiable anymore (either shared with someone else, or needed by an attestation)".format( - db_object.model_name() - ) - ) - - # Modification of the documentation - if (short_description is not None) and (declaration is None): - tmp_declaration = db_object.declaration - tmp_declaration["description"] = short_description - db_object.declaration = tmp_declaration - - if description is not None: - db_object.description = description - - # Modification of the declaration - modified = False - if declaration is not None: - db_object.declaration = declaration - modified = True - - # Modification of the source code - if code is not None: - db_object.source_code = code - modified = True - - db_object.save() - - return modified, db_object - def get(self, request, *args, **kwargs): db_object = self.get_object() @@ -256,27 +123,3 @@ class RetrieveUpdateDestroyCodeView(RetrieveUpdateDestroyContributionView): serializer = self.get_serializer(db_object, fields=fields_to_return) return Response(serializer.data) - - def put(self, request, author_name, object_name, version=None): - (modified, db_object) = self.do_update( - request, author_name, object_name, version - ) - - # Available fields (not returned by default): - # - html_description - if "fields" in request.GET: - fields_to_return = request.GET["fields"].split(",") - else: - return Response(status=204) - - result = {} - - # Retrieve the description in HTML format - if "html_description" in fields_to_return: - description = db_object.description - if len(description) > 0: - result["html_description"] = ensure_html(description) - else: - result["html_description"] = "" - - return Response(result) -- GitLab