Newer
Older

Jorrit Schaap
committed
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, render

Jorrit Schaap
committed
from lofar.sas.tmss.tmss.tmssapp import models

Jorrit Schaap
committed
from lofar.common.json_utils import get_default_json_object_for_schema
from lofar.sas.tmss.tmss.tmssapp.adapters.parset import convert_to_parset
from drf_yasg.utils import swagger_auto_schema
from rest_framework.permissions import AllowAny
from rest_framework.decorators import authentication_classes, permission_classes
from django.apps import apps

Jorrit Schaap
committed
Jörn Künsemöller
committed
from datetime import datetime
import dateutil.parser
from lofar.sas.tmss.tmss.tmssapp.conversions import local_sidereal_time_for_utc_and_station, local_sidereal_time_for_utc_and_longitude

Jorrit Schaap
committed
def subtask_template_default_specification(request, subtask_template_pk:int):

Jorrit Schaap
committed
subtask_template = get_object_or_404(models.SubtaskTemplate, pk=subtask_template_pk)

Jorrit Schaap
committed
spec = get_default_json_object_for_schema(subtask_template.schema)
return JsonResponse(spec)

Jorrit Schaap
committed
def task_template_default_specification(request, task_template_pk:int):
task_template = get_object_or_404(models.TaskTemplate, pk=task_template_pk)
spec = get_default_json_object_for_schema(task_template.schema)
return JsonResponse(spec)
def subtask_parset(request, subtask_pk:int):

Jorrit Schaap
committed
subtask = get_object_or_404(models.Subtask, pk=subtask_pk)
parset = convert_to_parset(subtask)
return HttpResponse(str(parset), content_type='text/plain')
Jörn Künsemöller
committed
return render(request, os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), '../../frontend','tmss_webapp/build/index.html'))
#return render(request, "../../../frontend/frontend_poc/build/index.html")
Jörn Künsemöller
committed
def task_specify_observation(request, pk=None):
task = get_object_or_404(models.TaskDraft, pk=pk)
return HttpResponse("response", content_type='text/plain')
# Allow everybody to GET our publicly available template-json-schema's
@permission_classes([AllowAny])
@authentication_classes([AllowAny])
@swagger_auto_schema(responses={200: 'Get the JSON schema from the template with the requested <template>, <name> and <version>',
404: 'the schema with requested <template>, <name> and <version> is not available'},
operation_description="Get the JSON schema for the given <template> with the given <name> and <version> as application/json content response.")
def get_template_json_schema(request, template:str, name:str, version:str):
template_model = apps.get_model("tmssapp", template)
template_instance = get_object_or_404(template_model, name=name, version=version)
schema = template_instance.schema
response = JsonResponse(schema, json_dumps_params={"indent":2})
# config Access-Control. Our schemas use $ref url's to other schemas, mainly pointing to our own common schemas with base definitions.
# We instruct the client to allow fetching those.
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "GET, OPTIONS"
return response

Jorrit Schaap
committed
Jörn Künsemöller
committed
def utc(request):
return HttpResponse(datetime.utcnow().isoformat(), content_type='text/plain')
def lst(request):
# Handling optional parameters via django paths in urls.py is a pain, we access them on the request directly instead.
timestamp = request.GET.get('timestamp', None)
station = request.GET.get('station', None)
longitude = request.GET.get('longitude', None)
# conversions
if timestamp:
timestamp = dateutil.parser.parse(timestamp) # isot to datetime
if longitude:
longitude = float(longitude)
if station:
lst_lon = local_sidereal_time_for_utc_and_station(timestamp, station)
elif longitude:
lst_lon = local_sidereal_time_for_utc_and_longitude(timestamp, longitude)
else:
# fall back to defaults
lst_lon = local_sidereal_time_for_utc_and_station(timestamp)
# todo: do we want to return a dict, so users can make sure their parameters were parsed correctly instead?
return HttpResponse(str(lst_lon), content_type='text/plain')