From c098273ca850e43f9852cec95c424e42a5f2854f Mon Sep 17 00:00:00 2001 From: Samuel Gaist <samuel.gaist@idiap.ch> Date: Mon, 20 Apr 2020 15:29:11 +0200 Subject: [PATCH] [common][serializers] Add declaration and version validation --- beat/web/common/serializers.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/beat/web/common/serializers.py b/beat/web/common/serializers.py index af1d85d3f..cd52ad970 100644 --- a/beat/web/common/serializers.py +++ b/beat/web/common/serializers.py @@ -25,6 +25,9 @@ # # ############################################################################### +import copy + +from django.conf import settings from django.contrib.auth.models import User from django.utils import six @@ -333,20 +336,30 @@ class ContributionCreationSerializer(serializers.ModelSerializer): description = description.decode("string_escape") return description - def validate(self, data): - user = self.context.get("user") - name = self.Meta.model.sanitize_name(data["name"]) - data["name"] = name - version = data.get("version") + def validate_declaration(self, declaration): + decl = copy.deepcopy(declaration) + obj = self.Meta.beat_core_class(prefix=settings.PREFIX, data=decl) + if not obj.valid: + raise serializers.ValidationError(obj.errors) + return declaration + def validate_version(self, version): # If version is not one then it's necessarily a new version # forks start at one - if version > 1 and "previous_version" not in data: + if version > 1 and "previous_version" not in self.initial_data: + name = self.initial_data["name"] raise serializers.ValidationError( "{} {} version {} incomplete history data posted".format( self.Meta.model.__name__.lower(), name, version ) ) + return version + + def validate(self, data): + user = self.context.get("user") + name = self.Meta.model.sanitize_name(data["name"]) + data["name"] = name + version = data.get("version") if self.Meta.model.objects.filter( author=user, name=name, version=version -- GitLab