Commit 239bb6a2 authored by Samuel GAIST's avatar Samuel GAIST

[databases][signals] Simplify the creation logic

Most of the logic is the usual filter, check if
exists and if not create. This can be replaced
by the get_or_create QuerySet method that does
both in one go.
parent a481e45c
......@@ -41,8 +41,7 @@ from .models import validate_database
@receiver(models.signals.post_delete, sender=Database)
def auto_delete_file_on_delete(sender, instance, **kwargs):
"""Deletes file from filesystem when ``Database`` object is deleted.
"""
"""Deletes file from filesystem when ``Database`` object is deleted."""
if instance.declaration_file:
instance.declaration_file.delete(save=False)
......@@ -118,23 +117,13 @@ def refresh_protocols(sender, instance, **kwargs):
tset_name = json_protocol["template"] + "__" + set_attr["template"]
dataset_template = DatabaseSetTemplate.objects.filter(name=tset_name)
if not dataset_template: # create
dataset_template = DatabaseSetTemplate(name=tset_name)
dataset_template.save()
else:
dataset_template = dataset_template[0]
# Create the database set
dataset = DatabaseSet.objects.filter(
name=set_attr["name"], template=dataset_template, protocol=protocol,
dataset_template, _ = DatabaseSetTemplate.objects.get_or_create(
name=tset_name
)
if not dataset: # create
dataset = DatabaseSet.objects.create(
name=set_attr["name"],
template=dataset_template,
protocol=protocol,
# Create the database set
dataset, _ = DatabaseSet.objects.get_or_create(
name=set_attr["name"], template=dataset_template, protocol=protocol
)
# Create the database set template output
......@@ -146,53 +135,23 @@ def refresh_protocols(sender, instance, **kwargs):
"value `%s' is not valid" % (format_name,)
)
(author, name, version) = format_name.split("/")
dataformats = DataFormat.objects.filter(
dataformat = DataFormat.objects.get(
author__username=author, name=name, version=version,
)
# TODO: Remove this when validation works (see comments)
if len(dataformats) != 1:
raise SyntaxError(
"Could not find dataformat named `%s' to set"
"output `%s' of template `%s' for protocol"
"`%s' of database `%s'",
(
format_name,
output_name,
dataset_template.name,
protocol_name,
instance.name,
),
)
return
database_template_output = DatabaseSetTemplateOutput.objects.filter(
database_template_output,
_,
) = DatabaseSetTemplateOutput.objects.get_or_create(
name=output_name,
template=dataset_template,
dataformat=dataformats[0],
dataformat=dataformat,
)
if not database_template_output: # create
database_template_output = DatabaseSetTemplateOutput(
name=output_name,
template=dataset_template,
dataformat=dataformats[0],
)
database_template_output.save()
else:
database_template_output = database_template_output[0]
# Create the database set output
dataset_output = DatabaseSetOutput.objects.filter(
template=database_template_output, set=dataset,
)
if not dataset_output: # create
dataset_output = DatabaseSetOutput(
DatabaseSetOutput.objects.get_or_create(
template=database_template_output, set=dataset,
)
dataset_output.save()
except Exception:
instance.delete()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment