Source code for beat.web.databases.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/.           #
#                                                                             #
###############################################################################

from django.conf import settings

from rest_framework import serializers

from ..common.serializers import VersionableSerializer
from ..common.fields import JSONSerializerField

from .models import Database
from .exceptions import DatabaseCreationError

import beat.core.database

[docs]class DatabaseSerializer(VersionableSerializer): protocols = serializers.SerializerMethodField() description = serializers.SerializerMethodField() declaration = JSONSerializerField()
[docs] class Meta(VersionableSerializer.Meta): model = Database exclude = ['description_file', 'declaration_file', 'source_code_file']
def __init__(self, *args, **kwargs): super(DatabaseSerializer, self).__init__(*args, **kwargs) self.experiments_stats = {}
[docs] def get_description(self, obj): return obj.description.decode('utf8')
[docs] def get_protocols(self, obj): protocols_options = self.context['request'].query_params.get('protocols_options', []) include_datasets = 'datasets' in protocols_options include_outputs = 'outputs' in protocols_options entries = [] for protocol in obj.protocols.all(): protocol_entry = { 'name': protocol.name, } if include_datasets: protocol_entry['datasets'] = [] for dataset in protocol.sets.order_by('name'): dataset_entry = { 'name': dataset.name, } if include_outputs: dataset_entry['outputs'] = map(lambda x: { 'name': x.name, 'dataformat': x.dataformat.fullname(), }, dataset.template.outputs.order_by('name')) protocol_entry['datasets'].append(dataset_entry) entries.append(protocol_entry) return entries
[docs]class DatabaseCreationSerializer(serializers.ModelSerializer): code = serializers.CharField(required=False) declaration = JSONSerializerField(required=False) description = serializers.CharField(required=False, allow_blank=True) previous_version = serializers.CharField(required=False)
[docs] class Meta: model = Database fields = ['name', 'short_description', 'description', 'declaration', 'code', 'previous_version'] beat_core_class = beat.core.database
[docs] def validate(self, data): user = self.context.get('user') name = self.Meta.model.sanitize_name(data['name']) data['name'] = name if 'previous_version' in data: previous_version_id = self.Meta.beat_core_class.Storage(settings.PREFIX, data['previous_version']) else: previous_version_id = None # Retrieve the previous version (if applicable) if previous_version_id is not None: try: previous_version = self.Meta.model.objects.get( name=previous_version_id.name, version=previous_version_id.version) except: raise serializers.ValidationError("Database '%s' not found" % \ previous_version_id.fullname) is_accessible = previous_version.accessibility_for(user) if not is_accessible[0]: raise serializers.ValidationError('No access allowed') data['previous_version'] = previous_version # Determine the version number last_version = None if previous_version_id is not None: if (previous_version_id.name == name): last_version = self.Meta.model.objects.filter(name=name).order_by('-version')[0] if last_version is None: if self.Meta.model.objects.filter(name=name).count() > 0: raise serializers.ValidationError('This {} name already exists'.format(self.Meta.model.__name__.lower())) data['version'] = (last_version.version + 1 if last_version is not None else 1) return data
[docs] def create(self, validated_data): (db_object, errors) = self.Meta.model.objects.create_database(**validated_data) if errors: raise DatabaseCreationError(errors) return db_object