From cb9e03c30da69333f4ad9e7df3afb47d5122bb61 Mon Sep 17 00:00:00 2001 From: Jorrit Schaap <schaap@astron.nl> Date: Fri, 2 Jul 2021 14:20:43 +0200 Subject: [PATCH] TMSS-745: added convenience methods to get either the full template document with all defaults, or with just the items in the parameters list --- .../src/tmss/tmssapp/models/specification.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py index a0120347165..7c3ec762a0d 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py @@ -21,6 +21,8 @@ from pprint import pformat from lofar.sas.tmss.tmss.exceptions import TMSSException from lofar.sas.tmss.tmss.exceptions import BlueprintCreationException, TMSSException from django.db.models import Count +from copy import deepcopy + # # I/O # @@ -216,6 +218,47 @@ class SchedulingUnitObservingStrategyTemplate(NamedCommon): super().save(force_insert, force_update, using, update_fields) + @property + def template_doc_complete_with_defaults(self) -> dict: + template_doc = deepcopy(self.template) + + # loop over all tasks, and add the defaults to each task given the task's specifications_template + for task_name, task_doc in list(template_doc['tasks'].items()): + task_specifications_template = TaskTemplate.objects.get(name=task_doc['specifications_template']) + template_doc['tasks'][task_name] = add_defaults_to_json_object_for_schema(task_doc, task_specifications_template.schema, + cache=TemplateSchemaMixin._schema_cache, max_cache_age=TemplateSchemaMixin._MAX_SCHEMA_CACHE_AGE) + + # add the default constraints given the scheduling_constraints_template + constraints_template = SchedulingConstraintsTemplate.objects.get(name=template_doc.get('scheduling_constraints_template')) + constraints_doc = add_defaults_to_json_object_for_schema(template_doc.get('scheduling_constraints_doc', {}), constraints_template.schema, + cache=TemplateSchemaMixin._schema_cache, max_cache_age=TemplateSchemaMixin._MAX_SCHEMA_CACHE_AGE) + template_doc['scheduling_constraints_doc'] = constraints_doc + + return template_doc + + @property + def template_doc_with_just_the_parameters(self) -> dict: + parameter_pointers = sum([p['refs'] for p in self.template['parameters']], []) + + def remove_non_parameter_items(doc, parent_path:str): + if isinstance(doc, dict): + for key, value in list(doc.items()): + path = parent_path+'/'+key + if any([pointer==path for pointer in parameter_pointers]): + # keep this key/value as it is exactly one of the pointer paths + pass + elif not any([pointer.startswith(path) for pointer in parameter_pointers]): + del doc[key] + else: + remove_non_parameter_items(value, path) + elif isinstance(doc, list): + for cntr, item in enumerate(doc): + remove_non_parameter_items(item, '%s/%d' % (parent_path, cntr)) + + template_doc = self.template_doc_complete_with_defaults + remove_non_parameter_items(template_doc, "#") + + return template_doc class SchedulingUnitTemplate(Template): pass -- GitLab