From 302b732e7beab00a8f0e25ac49116bba4ebed363 Mon Sep 17 00:00:00 2001 From: Jorrit Schaap <schaap@astron.nl> Date: Mon, 25 May 2020 15:51:12 +0200 Subject: [PATCH] TMSS-171: minor logging changes. Return TaskBluepring from create method, and return it in the REST Response along with the url of the new instance in the response header --- SAS/TMSS/src/tmss/tmssapp/tasks.py | 21 ++++++---------- .../tmss/tmssapp/viewsets/specification.py | 25 +++++++++++++------ 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/SAS/TMSS/src/tmss/tmssapp/tasks.py b/SAS/TMSS/src/tmss/tmssapp/tasks.py index 39165a41f36..02344834044 100644 --- a/SAS/TMSS/src/tmss/tmssapp/tasks.py +++ b/SAS/TMSS/src/tmss/tmssapp/tasks.py @@ -14,7 +14,7 @@ import logging logger = logging.getLogger(__name__) -def run_specify_observation(task_draft: models.TaskDraft): +def create_task_blueprint_from_task_draft_and_instantiate_subtasks_from_template(task_draft: models.TaskDraft): """ Create a task_blueprint from the task_draft For every subtask specified in task blueprint: @@ -23,30 +23,25 @@ def run_specify_observation(task_draft: models.TaskDraft): - link subtask inputs to predecessor outputs - set subtask to DEFINED """ - logger.debug("run_specify_observation...") - task_blueprint = create_taskblueprint_from_taskdraft(task_draft) - results_str = "# BLUEPRINT TASK ID=%d GENERATED FROM TASK DRAFT ID=%d\n" % (task_blueprint.id, task_draft.pk) + logger.debug("create_task_blueprint_from_task_draft_and_instantiate_subtasks_from_template(task_draft.id=%s)...", task_draft.pk) + task_blueprint = create_task_blueprint_from_task_draft(task_draft) obs_subtask = create_subtask_observation_control(task_blueprint) pipe_subtask = create_subtask_pipeline_control(task_blueprint) connect_observation_subtask_to_preprocessing_subtask(obs_subtask, pipe_subtask) - results_str += "# SUBTASKS %d and %d GENERATED FROM THE TASK BLUEPRINT ID=%d\n" %\ - (obs_subtask.id, pipe_subtask.id, task_blueprint.id) - # and the next does not work either..... # qa_file_subtask = create_observation_to_qafile_subtask(obs_subtask) # qa_plots_subtask = create_qafile_to_qaplots_subtask(qa_file_subtask) - logger.info(results_str) - return results_str + return task_blueprint -def create_taskblueprint_from_taskdraft(task_draft: models.TaskDraft): +def create_task_blueprint_from_task_draft(task_draft: models.TaskDraft): """ Create a task_blueprint from the task_draft :raises Exception if instantiate fails. """ - logger.debug("create_taskblueprint_from_taskdraft") + logger.debug("create_task_blueprint_from_task_draft(task_draft.id=%s)", task_draft.pk) # Get scheduling unit blueprint from scheduling unit draft, but that is a multi object relation # so which one is related to this task_draft? @@ -72,8 +67,8 @@ def create_taskblueprint_from_taskdraft(task_draft: models.TaskDraft): specifications_doc=task_draft.specifications_doc, specifications_template=task_draft.specifications_template ) - # Add blueprint id as relation in task_draftmodels - print("Return the blueprint id " + str(task_blueprint.id)) + + logger.info("create_task_blueprint_from_task_draft(task_draft.id=%s) created task_blueprint: %s", task_draft.pk, task_blueprint.pk) return task_blueprint diff --git a/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py b/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py index 05fc9d9d8a5..a9b4fb28016 100644 --- a/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py +++ b/SAS/TMSS/src/tmss/tmssapp/viewsets/specification.py @@ -3,9 +3,12 @@ This file contains the viewsets (based on the elsewhere defined data models and """ from django.shortcuts import get_object_or_404 -from django.http import HttpResponse, JsonResponse +from django.http import JsonResponse +from django.utils.cache import add_never_cache_headers from django.contrib.auth.models import User from rest_framework.viewsets import ReadOnlyModelViewSet +from rest_framework import status +from rest_framework.response import Response from rest_framework.decorators import permission_classes from rest_framework.permissions import IsAuthenticatedOrReadOnly, DjangoModelPermissions @@ -20,7 +23,7 @@ from lofar.sas.tmss.tmss.tmssapp import serializers from datetime import datetime from lofar.common.json_utils import get_default_json_object_for_schema from lofar.common.datetimeutils import formatDatetime -from lofar.sas.tmss.tmss.tmssapp.tasks import run_specify_observation +from lofar.sas.tmss.tmss.tmssapp.tasks import create_task_blueprint_from_task_draft_and_instantiate_subtasks_from_template # This is required for keeping a user reference as ForeignKey in other models @@ -250,13 +253,21 @@ class TaskDraftViewSetJSONeditorOnline(LOFARViewSet): else: return models.TaskDraft.objects.all() - @swagger_auto_schema(responses={200: 'Created task blueprint', + @swagger_auto_schema(responses={201: 'Created task blueprint, see Location in Response header', 403: 'forbidden'}, operation_description="Carve this draft task specification in stone, and make an (uneditable) blueprint out of it.") @action(methods=['get'], detail=True, url_name="create_task_blueprint") def create_task_blueprint(self, request, pk=None): - task = get_object_or_404(models.TaskDraft, pk=pk) - results_str = run_specify_observation(task) - # Just for demo purpose show some result - return HttpResponse(results_str, content_type='text/plain') + task_draft = get_object_or_404(models.TaskDraft, pk=pk) + task_blueprint = create_task_blueprint_from_task_draft_and_instantiate_subtasks_from_template(task_draft) + + # url path magic to construct the new task_blueprint_path url + task_draft_path = request._request.path + base_path = task_draft_path[:task_draft_path.find('/task_draft')] + task_blueprint_path = '%s/task_blueprint/%s/' % (base_path, task_blueprint.id,) + + # return a response with the new serialized TaskBlueprint, and a Location to the new instance in the header + return Response(serializers.TaskBlueprintSerializerJSONeditorOnline(task_blueprint, context={'request':request}).data, + status=status.HTTP_201_CREATED, + headers={'Location': task_blueprint_path}) -- GitLab