Skip to content
Snippets Groups Projects
Commit f394c23b authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-379: added REST API method to do a lookup of stations by group

parent f6375431
No related branches found
No related tags found
2 merge requests!260syncing cob-master with master again,!247Resolve TMSS-379 "Input"
import os
from django.http import HttpResponse, JsonResponse
from django.http import HttpResponse, JsonResponse, Http404
from django.shortcuts import get_object_or_404, render
from lofar.sas.tmss.tmss.tmssapp import models
from lofar.common.json_utils import get_default_json_object_for_schema
......@@ -60,6 +60,30 @@ def get_template_json_schema(request, template:str, name:str, version:str):
return response
# Allow everybody to GET our publicly available station group lookups
@permission_classes([AllowAny])
@authentication_classes([AllowAny])
@swagger_auto_schema(responses={200: 'A JSON object with two properties: group:<the_group_name>, stations:<the_list_of_stations>',
404: 'No such group or template available'},
operation_description="Get a JSON list of stations for the given <station_group> name the the group definitions in the common_schema_template given by <template_name> and <template_version>")
def get_stations_in_group(request, station_group:str, template_name:str, template_version:str):
station_schema_template = get_object_or_404(models.CommonSchemaTemplate, name=template_name, version=template_version)
station_schema = station_schema_template.schema
if 'station_group' not in station_schema.get('definitions', {}):
raise Http404('The JSON schema in template %s version %s has no station_group definitions' % (template_name, template_version))
groups = station_schema['definitions']['station_group']['anyOf']
try:
selected_group = next(g for g in groups if g['title'].lower() == station_group.lower())
except StopIteration:
raise Http404('No station_group with name "%s" found in the JSON schema. template=%s version=%s' % (station_group, template_name, template_version))
stations = selected_group['properties']['stations']['enum'][0]
return JsonResponse({'group': station_group,
'stations': stations})
def utc(request):
return HttpResponse(datetime.utcnow().isoformat(), content_type='text/plain')
......
......@@ -64,6 +64,8 @@ urlpatterns = [
path('redoc/', swagger_schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
path('schemas/<str:template>/<str:name>/<str:version>', views.get_template_json_schema, name='get_template_json_schema'), #TODO: how to make trailing slash optional?
path('schemas/<str:template>/<str:name>/<str:version>/', views.get_template_json_schema, name='get_template_json_schema'),
path('station_groups/<str:station_group>/<str:template_name>/<str:template_version>', views.get_stations_in_group, name='get_stations_in_group'), #TODO: how to make trailing slash optional?
path('station_groups/<str:station_group>/<str:template_name>/<str:template_version>/', views.get_stations_in_group, name='get_stations_in_group'),
path(r'util/utc', views.utc, name="system-utc"),
path(r'util/lst', views.lst, name="conversion-lst"),
path('__debug__/', include(debug_toolbar.urls)),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment