Commit c578694a authored by Samuel GAIST's avatar Samuel GAIST

[utils][commands][install] Install protocoltemplates

parent 25d3882e
...@@ -71,6 +71,7 @@ import beat.core.dataformat ...@@ -71,6 +71,7 @@ import beat.core.dataformat
import beat.core.experiment import beat.core.experiment
import beat.core.library import beat.core.library
import beat.core.plotter import beat.core.plotter
import beat.core.protocoltemplate
import beat.core.toolchain import beat.core.toolchain
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -275,6 +276,79 @@ def upload_dataformat(prefix, name, data): ...@@ -275,6 +276,79 @@ def upload_dataformat(prefix, name, data):
return True return True
def upload_protocoltemplate(prefix, name, data, new_only=False):
"""Uploads the protocol template to the platform
Parameters:
prefix (str): The prefix where the object template is sitting
name (str): The canonical name of the object
data (dict): A dictionary with template substitutions
Returns:
bool: Indicates if the operation was successful
"""
storage = beat.core.protocoltemplate.Storage(prefix, name)
if not storage.exists():
return False # should be ignored
declaration = detemplatize(storage.json.load(), data)
obj = simplejson.loads(declaration, object_pairs_hook=collections.OrderedDict)
description = storage.doc.load() if storage.doc.exists() else ""
if "root_folder" in data:
obj["root_folder"] = data["root_folder"]
declaration = simplejson.dumps(obj, indent=4)
from ....common.models import Shareable
from ....protocoltemplates.models import ProtocolTemplate
protocol_template = ProtocolTemplate.objects.filter(
name=storage.name, version=int(storage.version)
)
if not protocol_template:
(protocol_template, errors) = ProtocolTemplate.objects.create_protocoltemplate(
name=storage.name,
declaration=declaration,
short_description=obj.get("description", ""),
description=description,
version=int(storage.version),
)
if protocol_template is None:
logger.warn("Did not add template protocol `%s', because: %s", name, errors)
return False
else:
logger.info("Added template protocol `%s'", protocol_template)
elif new_only:
return True
else: # only updates files
protocol_template = protocol_template[0]
protocol_template.short_description = obj.get("description", "")
protocol_template.declaration = declaration
protocol_template.description = description
protocol_template.save()
logger.info("Updated protocol template `%s'", protocol_template)
if not data["private"]:
protocol_template.sharing = Shareable.PUBLIC
protocol_template.save()
logger.info("Set protocol template `%s' as public", protocol_template)
return True
def load_database_folders(filename): def load_database_folders(filename):
"""Simply loads the json file describing the database root folders """Simply loads the json file describing the database root folders
...@@ -874,6 +948,7 @@ def upload_dispatcher(prefix, project, type, name, data): ...@@ -874,6 +948,7 @@ def upload_dispatcher(prefix, project, type, name, data):
valid_types = { valid_types = {
"dataformats": upload_dataformat, "dataformats": upload_dataformat,
"protocoltemplates": upload_protocoltemplate,
"databases": upload_database, "databases": upload_database,
"libraries": upload_library, "libraries": upload_library,
"algorithms": upload_algorithm, "algorithms": upload_algorithm,
...@@ -973,6 +1048,15 @@ def install_contributions(source_prefix, project, template_data, db_root_file=No ...@@ -973,6 +1048,15 @@ def install_contributions(source_prefix, project, template_data, db_root_file=No
link_contribution_versions(DataFormat) link_contribution_versions(DataFormat)
# Template protocols
from ....protocoltemplates.models import ProtocolTemplate
for object_ in list_objects(source_prefix, project, "protocoltemplates", "*.json"):
upload_dispatcher(
source_prefix, project, "protocoltemplates", object_, template_data
)
link_contribution_versions(ProtocolTemplate)
# Reads database root file, if provided # Reads database root file, if provided
db_root = {} db_root = {}
if db_root_file: if db_root_file:
......
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