#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
import simplejson as json
from django.utils.encoding import smart_text
from rest_framework import serializers
import beat.core.plotter
import beat.core.plotterparameter
from ..code.serializers import CodeCreationSerializer
from ..code.serializers import CodeSerializer
from ..common import fields as beat_fields
from ..common.serializers import ContributionCreationSerializer
from ..common.serializers import ContributionSerializer
from ..common.serializers import DynamicFieldsSerializer
from ..libraries.serializers import LibraryReferenceSerializer
from .models import DefaultPlotter
from .models import Plotter
from .models import PlotterParameter
[docs]class PlotterSerializer(ContributionSerializer):
dataformat = serializers.CharField(source="dataformat.fullname")
[docs]class PlotterParameterSerializer(ContributionSerializer):
[docs]class DefaultPlotterSerializer(DynamicFieldsSerializer):
dataformat = serializers.CharField(source="dataformat.fullname")
plotter = serializers.CharField(source="plotter.fullname")
parameter = serializers.CharField(source="parameter.fullname")
# ----------------------------------------------------------
[docs]class PlotterCreationSerializer(CodeCreationSerializer):
# ----------------------------------------------------------
[docs]class PlotterAllSerializer(CodeSerializer):
dataformat = serializers.SerializerMethodField()
declaration_file = serializers.SerializerMethodField()
description_file = serializers.SerializerMethodField()
source_code_file = serializers.SerializerMethodField()
referenced_libraries = LibraryReferenceSerializer(many=True)
# ----------------------------------------------------------
[docs]class FullPlotterSerializer(PlotterAllSerializer):
# ----------------------------------------------------------
[docs]class PlotterParameterCreationFailedException(Exception):
pass
[docs]class PlotterParameterCreationSerializer(ContributionCreationSerializer):
data = beat_fields.JSONField(required=False)
[docs] def create(self, validated_data):
plotterparameter = None
if "name" not in validated_data:
raise serializers.ValidationError("No name provided")
if PlotterParameter.objects.filter(
author=self.context["request"].user,
name=validated_data["name"],
version=validated_data["version"],
).exists():
raise serializers.ValidationError(
"A plotterparameter with this name already exists"
)
if "plotter" not in self.data:
raise serializers.ValidationError("No plotter provided")
try:
Plotter.objects.get(id=self.data["plotter"])
except Exception:
raise serializers.ValidationError("Required plotter does not exist")
if "data" not in validated_data:
validated_data["data"] = {}
# Only create new version for latest version
if "previous_version" in validated_data:
if (
validated_data["previous_version"].version
< validated_data["version"] - 1
):
raise serializers.ValidationError(
"A new version for this plotterparameter version already exist"
)
# add description/short_description to new version
validated_data["short_description"] = validated_data[
"previous_version"
].short_description
validated_data["description"] = validated_data[
"previous_version"
].description
# Create fork
if "fork_of" in validated_data:
# add description/short_description to new version
validated_data["short_description"] = validated_data[
"fork_of"
].short_description
validated_data["description"] = validated_data["fork_of"].description
plotterparameter = PlotterParameter.objects.create(**validated_data)
if plotterparameter is None:
raise PlotterParameterCreationFailedException()
return plotterparameter
# ----------------------------------------------------------
[docs]class PlotterParameterAllSerializer(ContributionSerializer):
data = serializers.SerializerMethodField()
description = serializers.SerializerMethodField()
plotter = serializers.SerializerMethodField()
# def get_referencing_experiments(self, obj):
# user = self.context.get('user')
# experiments = obj.experiments.for_user(user, True).order_by('-creation_date')
# serializer = ExperimentSerializer(experiments, many=True)
# referencing_experiments = serializer.data
# # Put the pending experiments first
# ordered_result = filter(lambda x: x['creation_date'] is None, referencing_experiments)
# ordered_result += filter(lambda x: x['creation_date'] is not None, referencing_experiments)
# return ordered_result
# def get_new_experiment_url(self, obj):
# return obj.get_new_experiment_url()
# ----------------------------------------------------------
[docs]class FullPlotterParameterSerializer(PlotterParameterAllSerializer):
plotters = serializers.SerializerMethodField()
[docs] def get_description(self, obj):
return smart_text(
obj.description, encoding="utf-8", strings_only=False, errors="strict"
)
[docs] def get_short_description(self, obj):
return smart_text(
obj.short_description, encoding="utf-8", strings_only=False, errors="strict"
)
[docs] def get_data(self, obj):
return json.loads(obj.data)
[docs] def get_plotter(self, obj):
if obj.plotter is not None:
return obj.plotter.fullname()
else:
return "undefined plotter"
[docs] def get_plotters(self, obj):
all_plotters = Plotter.objects.all()
results = {}
for plotter in all_plotters.iterator():
serializer = FullPlotterSerializer(
plotter,
context=self.context,
fields=[
"id",
"accessibility",
"modifiable",
"deletable",
"is_owner",
"name",
"fork_of",
"last_version",
"previous_version",
"short_description",
"description",
"version",
"creation_date",
"data",
"sample_data",
"declaration",
],
)
results[plotter.fullname()] = serializer.data
return results
# def get_plotter(self, obj):
# return obj.author.username
# "accessibility": "public",
# "modifiable": true,
# "deletable": true,
# "is_owner": false,
# "name": "plot/bar/1",
# "fork_of": null,
# "last_version": true,
# "previous_version": null,
# "short_description": "Default parameters for bar plots",
# "description": "Raw content",
# "version": 1,
# "creation_date": "2015-09-03T16:55:47.620000",