Source code for beat.web.libraries.serializers

#!/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 beat.core.library

from rest_framework import serializers

from ..code.serializers import CodeSerializer
from ..code.serializers import CodeCreationSerializer
from ..code.serializers import CodeModSerializer

from ..algorithms.models import Algorithm
from ..backend.serializers import EnvironmentInfoSerializer

from .models import Library


# ----------------------------------------------------------


[docs]class LibraryCreationSerializer(CodeCreationSerializer):
[docs] class Meta(CodeCreationSerializer.Meta): model = Library beat_core_class = beat.core.library.Library
# ----------------------------------------------------------
[docs]class LibraryModSerializer(CodeModSerializer):
[docs] class Meta(CodeModSerializer.Meta): model = Library beat_core_class = beat.core.library.Library
# ----------------------------------------------------------
[docs]class ReferenceSerializer(serializers.ModelSerializer): name = serializers.CharField(source="fullname") accessibility = serializers.SerializerMethodField() is_owner = serializers.SerializerMethodField()
[docs] class Meta: fields = ["name", "short_description", "accessibility", "is_owner"]
[docs] def get_accessibility(self, obj): return obj.get_sharing_display().lower()
[docs] def get_is_owner(self, obj): return obj.author == self.context.get("user")
# ----------------------------------------------------------
[docs]class LibraryReferenceSerializer(ReferenceSerializer):
[docs] class Meta(ReferenceSerializer.Meta): model = Library
# ----------------------------------------------------------
[docs]class AlgorithmReferenceSerializer(ReferenceSerializer):
[docs] class Meta(ReferenceSerializer.Meta): model = Algorithm
# ----------------------------------------------------------
[docs]class LibrarySerializer(CodeSerializer): referenced_libraries = LibraryReferenceSerializer(many=True) referencing_libraries = LibraryReferenceSerializer(many=True, source="referencing") referencing_algorithms = AlgorithmReferenceSerializer( many=True, source="used_by_algorithms" ) environments = EnvironmentInfoSerializer(many=True)
[docs] class Meta(CodeSerializer.Meta): model = Library
# ----------------------------------------------------------
[docs]class FullLibrarySerializer(LibrarySerializer):
[docs] class Meta(LibrarySerializer.Meta): default_fields = ( LibrarySerializer.Meta.default_fields + LibrarySerializer.Meta.extra_fields )