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 ...@@ -41,8 +41,7 @@ from .models import validate_database
@receiver(models.signals.post_delete, sender=Database) @receiver(models.signals.post_delete, sender=Database)
def auto_delete_file_on_delete(sender, instance, **kwargs): 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: if instance.declaration_file:
instance.declaration_file.delete(save=False) instance.declaration_file.delete(save=False)
...@@ -118,25 +117,15 @@ def refresh_protocols(sender, instance, **kwargs): ...@@ -118,25 +117,15 @@ def refresh_protocols(sender, instance, **kwargs):
tset_name = json_protocol["template"] + "__" + set_attr["template"] tset_name = json_protocol["template"] + "__" + set_attr["template"]
dataset_template = DatabaseSetTemplate.objects.filter(name=tset_name) dataset_template, _ = DatabaseSetTemplate.objects.get_or_create(
if not dataset_template: # create name=tset_name
dataset_template = DatabaseSetTemplate(name=tset_name) )
dataset_template.save()
else:
dataset_template = dataset_template[0]
# Create the database set # Create the database set
dataset = DatabaseSet.objects.filter( dataset, _ = DatabaseSet.objects.get_or_create(
name=set_attr["name"], template=dataset_template, protocol=protocol, name=set_attr["name"], template=dataset_template, protocol=protocol
) )
if not dataset: # create
dataset = DatabaseSet.objects.create(
name=set_attr["name"],
template=dataset_template,
protocol=protocol,
)
# Create the database set template output # Create the database set template output
for output_name, format_name in set_attr["outputs"].items(): for output_name, format_name in set_attr["outputs"].items():
if len(format_name.split("/")) != 3: if len(format_name.split("/")) != 3:
...@@ -146,54 +135,24 @@ def refresh_protocols(sender, instance, **kwargs): ...@@ -146,54 +135,24 @@ def refresh_protocols(sender, instance, **kwargs):
"value `%s' is not valid" % (format_name,) "value `%s' is not valid" % (format_name,)
) )
(author, name, version) = format_name.split("/") (author, name, version) = format_name.split("/")
dataformats = DataFormat.objects.filter( dataformat = DataFormat.objects.get(
author__username=author, name=name, version=version, author__username=author, name=name, version=version,
) )
# TODO: Remove this when validation works (see comments) (
if len(dataformats) != 1: database_template_output,
raise SyntaxError( _,
"Could not find dataformat named `%s' to set" ) = DatabaseSetTemplateOutput.objects.get_or_create(
"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(
name=output_name, name=output_name,
template=dataset_template, 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 # Create the database set output
dataset_output = DatabaseSetOutput.objects.filter( DatabaseSetOutput.objects.get_or_create(
template=database_template_output, set=dataset, template=database_template_output, set=dataset,
) )
if not dataset_output: # create
dataset_output = DatabaseSetOutput(
template=database_template_output, set=dataset,
)
dataset_output.save()
except Exception: except Exception:
instance.delete() instance.delete()
raise raise
......
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