Skip to content
Snippets Groups Projects
Commit 905eef8d authored by Jörn Künsemöller's avatar Jörn Künsemöller
Browse files

TMSS-459: Fix Swagger issue where function based views are not included

parent 1220ca5e
No related branches found
No related tags found
1 merge request!284Resolve TMSS-259
......@@ -12,10 +12,13 @@ from rest_framework.permissions import AllowAny
from rest_framework.decorators import authentication_classes, permission_classes
from django.apps import apps
from rest_framework.decorators import api_view
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, timestamps_and_stations_to_sun_rise_and_set
# Note: Decorate with @api_view to get this picked up by Swagger
def subtask_template_default_specification(request, subtask_template_pk:int):
subtask_template = get_object_or_404(models.SubtaskTemplate, pk=subtask_template_pk)
spec = get_default_json_object_for_schema(subtask_template.schema)
......@@ -46,9 +49,11 @@ def task_specify_observation(request, pk=None):
# 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>',
@swagger_auto_schema(method='GET',
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.")
@api_view(['GET'])
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)
......@@ -65,9 +70,11 @@ def get_template_json_schema(request, template:str, name:str, version:str):
# 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>',
@swagger_auto_schema(method='GET',
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>")
@api_view(['GET'])
def get_stations_in_group(request, template_name:str, template_version:str, station_group:str):
station_schema_template = get_object_or_404(models.CommonSchemaTemplate, name=template_name, version=template_version)
station_schema = station_schema_template.schema
......@@ -88,22 +95,26 @@ def get_stations_in_group(request, template_name:str, template_version:str, stat
@permission_classes([AllowAny])
@authentication_classes([AllowAny])
@swagger_auto_schema(responses={200: 'An isoformat timestamp of the current UTC clock of the system'},
@swagger_auto_schema(method='GET',
responses={200: 'An isoformat timestamp of the current UTC clock of the system'},
operation_description="Get the current system time in UTC")
@api_view(['GET'])
def utc(request):
return HttpResponse(datetime.utcnow().isoformat(), content_type='text/plain')
@permission_classes([AllowAny])
@authentication_classes([AllowAny])
@swagger_auto_schema(responses={200: 'The LST time in hms format at the given UTC time and station or longitude'},
@swagger_auto_schema(method='GET',
responses={200: 'The LST time in hms format at the given UTC time and station or longitude'},
operation_description="Get LST time for UTC time and station or longitude",
manual_parameters=[Parameter(name='station', required=False, type='string', in_='query',
description="A station names (defaults to CS002)"),
Parameter(name='timestamp', required=False, type='string', in_='query',
description="A timestamp in isoformat (defaults to utcnow)"),
Parameter(name='longitude', required=False, type='float', in_='query',
description="A longitude")
Parameter(name='longitude', required=False, type='string', in_='query',
description="A longitude as float")
])
@api_view(['GET'])
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)
......@@ -130,12 +141,15 @@ def lst(request):
@permission_classes([AllowAny])
@authentication_classes([AllowAny])
@swagger_auto_schema(responses={200: 'A JSON object with sunrise, sunset, day and night of the given stations at the given timestamps'},
operation_description="Get sunrise, sunset, day and night for stations and timestamps",
@swagger_auto_schema(method='GET',
responses={200: 'A JSON object with sunrise, sunset, day and night of the given stations at the given timestamps'},
operation_description="Get sunrise, sunset, day and night for stations and timestamps.\n\n"
"Example request: /api/util/sun_rise_and_set?stations=CS002,CS005&timestamps=2020-05-01,2020-09-09T11-11-00",
manual_parameters=[Parameter(name='stations', required=False, type='string', in_='query',
description="comma-separated list of station names"),
Parameter(name='timestamps', required=False, type='string', in_='query',
description="comma-separated list of isoformat timestamps")])
@api_view(['GET'])
def get_sun_rise_and_set(request):
"""
returns sunrise and sunset at the given stations and timestamps, or today at LOFAR core if none specified.
......@@ -149,7 +163,7 @@ def get_sun_rise_and_set(request):
timestamps = timestamps.split(',')
timestamps = [dateutil.parser.parse(timestamp) for timestamp in timestamps] # isot to datetime
if stations is None:
stations = ['CS002']
stations = ["CS002"]
else:
stations = stations.split(',')
......
......@@ -54,6 +54,7 @@ swagger_schema_view = get_schema_view(
# permission_classes=(permissions.AllowAny,),
)
# use re_path(r'<...>/?') to make trailing slash optional, double entries confuse Swagger
urlpatterns = [
path('admin/', admin.site.urls),
path('logout/', LogoutView.as_view(), name='logout'),
......@@ -61,13 +62,11 @@ urlpatterns = [
re_path(r'^swagger(?P<format>\.json|\.yaml)$', swagger_schema_view.without_ui(cache_timeout=0), name='schema-json'),
path('swagger/', swagger_schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
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:template_name>/<str:template_version>/<str:station_group>', views.get_stations_in_group, name='get_stations_in_group'), #TODO: how to make trailing slash optional?
path('station_groups/<str:template_name>/<str:template_version>/<str:station_group>/', views.get_stations_in_group, name='get_stations_in_group'),
path('util/sun_rise_and_set', views.get_sun_rise_and_set, name='get_sun_rise_and_set'),
path(r'util/utc', views.utc, name="system-utc"),
path(r'util/lst', views.lst, name="conversion-lst"),
re_path('schemas/<str:template>/<str:name>/<str:version>/?', views.get_template_json_schema, name='get_template_json_schema'),
re_path('station_groups/<str:template_name>/<str:template_version>/<str:station_group>/?', views.get_stations_in_group, name='get_stations_in_group'),
re_path('util/sun_rise_and_set/?', views.get_sun_rise_and_set, name='get_sun_rise_and_set'),
re_path('util/utc/?', views.utc, name="system-utc"),
re_path('util/lst/?', views.lst, name="conversion-lst"),
]
if os.environ.get('SHOW_DJANGO_DEBUG_TOOLBAR', False):
......
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