Skip to content
Snippets Groups Projects
Commit 71082882 authored by Samuel GAIST's avatar Samuel GAIST Committed by Samuel GAIST
Browse files

[databases][serializers] Pre-commit cleanup

parent 08c286fb
No related branches found
No related tags found
No related merge requests found
...@@ -37,6 +37,7 @@ from .exceptions import DatabaseCreationError ...@@ -37,6 +37,7 @@ from .exceptions import DatabaseCreationError
import beat.core.database import beat.core.database
class DatabaseSerializer(VersionableSerializer): class DatabaseSerializer(VersionableSerializer):
protocols = serializers.SerializerMethodField() protocols = serializers.SerializerMethodField()
description = serializers.SerializerMethodField() description = serializers.SerializerMethodField()
...@@ -44,41 +45,42 @@ class DatabaseSerializer(VersionableSerializer): ...@@ -44,41 +45,42 @@ class DatabaseSerializer(VersionableSerializer):
class Meta(VersionableSerializer.Meta): class Meta(VersionableSerializer.Meta):
model = Database model = Database
exclude = ['description_file', 'declaration_file', 'source_code_file'] exclude = ["description_file", "declaration_file", "source_code_file"]
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(DatabaseSerializer, self).__init__(*args, **kwargs) super(DatabaseSerializer, self).__init__(*args, **kwargs)
self.experiments_stats = {} self.experiments_stats = {}
def get_description(self, obj): def get_description(self, obj):
return obj.description.decode('utf8') return obj.description.decode("utf8")
def get_protocols(self, obj): def get_protocols(self, obj):
protocols_options = self.context['request'].query_params.get('protocols_options', []) protocols_options = self.context["request"].query_params.get(
include_datasets = 'datasets' in protocols_options "protocols_options", []
include_outputs = 'outputs' in protocols_options )
include_datasets = "datasets" in protocols_options
include_outputs = "outputs" in protocols_options
entries = [] entries = []
for protocol in obj.protocols.all(): for protocol in obj.protocols.all():
protocol_entry = { protocol_entry = {"name": protocol.name}
'name': protocol.name,
}
if include_datasets: if include_datasets:
protocol_entry['datasets'] = [] protocol_entry["datasets"] = []
for dataset in protocol.sets.order_by('name'): for dataset in protocol.sets.order_by("name"):
dataset_entry = { dataset_entry = {"name": dataset.name}
'name': dataset.name,
}
if include_outputs: if include_outputs:
dataset_entry['outputs'] = map(lambda x: { dataset_entry["outputs"] = map(
'name': x.name, lambda x: {
'dataformat': x.dataformat.fullname(), "name": x.name,
}, dataset.template.outputs.order_by('name')) "dataformat": x.dataformat.fullname(),
},
dataset.template.outputs.order_by("name"),
)
protocol_entry['datasets'].append(dataset_entry) protocol_entry["datasets"].append(dataset_entry)
entries.append(protocol_entry) entries.append(protocol_entry)
return entries return entries
...@@ -92,18 +94,25 @@ class DatabaseCreationSerializer(serializers.ModelSerializer): ...@@ -92,18 +94,25 @@ class DatabaseCreationSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Database model = Database
fields = ['name', 'short_description', 'description', fields = [
'declaration', 'code', 'previous_version'] "name",
"short_description",
"description",
"declaration",
"code",
"previous_version",
]
beat_core_class = beat.core.database beat_core_class = beat.core.database
def validate(self, data): def validate(self, data):
user = self.context.get('user') user = self.context.get("user")
name = self.Meta.model.sanitize_name(data['name']) name = self.Meta.model.sanitize_name(data["name"])
data['name'] = name data["name"] = name
if 'previous_version' in data: if "previous_version" in data:
previous_version_id = self.Meta.beat_core_class.Storage(settings.PREFIX, previous_version_id = self.Meta.beat_core_class.Storage(
data['previous_version']) settings.PREFIX, data["previous_version"]
)
else: else:
previous_version_id = None previous_version_id = None
...@@ -111,33 +120,39 @@ class DatabaseCreationSerializer(serializers.ModelSerializer): ...@@ -111,33 +120,39 @@ class DatabaseCreationSerializer(serializers.ModelSerializer):
if previous_version_id is not None: if previous_version_id is not None:
try: try:
previous_version = self.Meta.model.objects.get( previous_version = self.Meta.model.objects.get(
name=previous_version_id.name, name=previous_version_id.name, version=previous_version_id.version
version=previous_version_id.version) )
except: except Exception:
raise serializers.ValidationError("Database '%s' not found" % \ raise serializers.ValidationError(
previous_version_id.fullname) "Database '%s' not found" % previous_version_id.fullname
)
is_accessible = previous_version.accessibility_for(user) is_accessible = previous_version.accessibility_for(user)
if not is_accessible[0]: if not is_accessible[0]:
raise serializers.ValidationError('No access allowed') raise serializers.ValidationError("No access allowed")
data['previous_version'] = previous_version data["previous_version"] = previous_version
# Determine the version number # Determine the version number
last_version = None last_version = None
if previous_version_id is not None: if previous_version_id is not None:
if (previous_version_id.name == name): if previous_version_id.name == name:
last_version = self.Meta.model.objects.filter(name=name).order_by('-version')[0] last_version = self.Meta.model.objects.filter(name=name).order_by(
"-version"
)[0]
if last_version is None: if last_version is None:
if self.Meta.model.objects.filter(name=name).count() > 0: if self.Meta.model.objects.filter(name=name).count() > 0:
raise serializers.ValidationError('This {} name already exists'.format(self.Meta.model.__name__.lower())) 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) data["version"] = last_version.version + 1 if last_version is not None else 1
return data return data
def create(self, validated_data): def create(self, validated_data):
(db_object, errors) = self.Meta.model.objects.create_database(**validated_data) (db_object, errors) = self.Meta.model.objects.create_database(**validated_data)
if errors: if errors:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment