Commit d06223e5 authored by Yori Fournier's avatar Yori Fournier
Browse files

added the submit_archive_describer method and the submit action

parent ba096dea
submit -> apply
......@@ -28,19 +28,27 @@ class ArchiveDescriberSerializer(serializers.HyperlinkedModelSerializer):
class TapSaveToFileArchiveDescriberSerializer(serializers.ModelSerializer):
submit = serializers.HyperlinkedIdentityField(view_name='submit-archive-describer', format='api')
class Meta:
model = TapSaveToFileArchiveDescriber
fields = ['id', 'owner', 'title',
'interface_type', 'interface_url', 'interface_username', 'interface_token', 'interface_sql_lang', 'interface_sql_queue', 'interface_sql_query',
'action_type', 'action_fileformat', 'action_filename']
'action_type', 'action_fileformat', 'action_filename',
'submit',
]
class TapDownloadFileArchiveDescriberSerializer(serializers.ModelSerializer):
submit = serializers.HyperlinkedIdentityField(view_name='submit-archive-describer', format='api')
class Meta:
model = TapDownloadFileArchiveDescriber
fields = ['id', 'owner', 'title',
'interface_type', 'interface_url', 'interface_username', 'interface_token', 'interface_sql_lang', 'interface_sql_queue', 'interface_sql_query',
'action_type', 'action_url_column', 'action_base_url_file_server']
'action_type', 'action_url_column', 'action_base_url_file_server',
'submit',
]
......@@ -8,6 +8,7 @@ urlpatterns = [
path('users/<int:pk>/archive_describers/', views.user_archive_describer_list, name='user-archive-describers'),
path('archive_describers/', views.archive_describer_list),
path('archive_describers/<int:pk>/', views.archive_describer_detail, name='archive-describer-detail'),
path('archive_describers/<int:pk>/submit', views.trigger_archive_gen, name='submit-archive-describer'),
]
urlpatterns = format_suffix_patterns(urlpatterns)
import os
import pyvo
import requests
import urllib
def get_tap_service(service_url, service_token=None):
# Setup authorisation
tap_session = requests.Session()
if service_token is not None:
tap_session.headers['Authorization'] = service_token
tap_service = pyvo.dal.TAPService(service_url.rstrip('/'), session=tap_session)
return(tap_service)
def submit_sync_tap_query(tap_service, sql_query, sql_opts):
lang = sql_opts.get("sql_lang", None)
if lang is None:
tap_result = tap_service.run_sync(sql_query)
else:
tap_result = tap_service.run_sync(sql_query, language=lang)
table = tap_result.to_table()
return(table)
def submit_tap_query(tap_service, sql_query, sql_opts, query_type="sync"):
if query_type.lower() == "sync":
table = submit_sync_tap_query(tap_service, sql_query, sql_opts)
else:
raise ValueError("The query type should be: sync, async:short, async:medium, async:long, not {}".format(query_type))
return(table)
def download_files(file_urls, target_directory, url_prefix=None):
#
# Download the fits files into local directory
#
# target_directory = './fits/'
# fit_file_base_url = 'https://www.rave-survey.org/files/'
for file_url in file_urls:
# extract name of the file
file_basename = os.path.basename(file_url)
# set the target local file
full_filename = os.path.join(target_directory, file_basename)
# build the url pointing to the file
if url_prefix is not None:
file_url = os.path.join(url_prefix, file_url)
# download and save into target file
print("Downloaded {file} into {target}".format(file=file_basename, target=target_directory))
urllib.request.urlretrieve(file_url, full_filename)
print('\nDone')
return True
def submit_archive_describer(archive_describer):
interface = archive_describer['interface_type']
action = archive_describer['action_type']
if interface == "tap":
service_url = archive_describer['interface_url']
tap_service = get_tap_service(service_url, service_token=None)
#sql_lang = archive_describer['interface_sql_lang']
#sql_queue = archive_describer['interface_sql_queue']
sql_query = archive_describer['interface_sql_query']
# get the optional arguments
sql_opts = {key.replace('interface_', ''): archive_describer[key] for key in set({'interface_sql_lang', 'interface_sql_queue'} & archive_describer.keys())}
print(sql_opts)
table = submit_tap_query(tap_service, sql_query, sql_opts, query_type='sync')
if action == 'save_to_file':
target_dir = './'
filename = archive_describer['action_filename']
print(os.path.join(target_dir, filename))
table.write(os.path.join(target_dir, filename), overwrite=True)
print(table)
return True
......@@ -5,6 +5,8 @@ from rest_framework.parsers import JSONParser
from arch_gen.models import ArchiveDescriber, TapSaveToFileArchiveDescriber, TapDownloadFileArchiveDescriber
from arch_gen.serializers import ArchiveDescriberSerializer, TapSaveToFileArchiveDescriberSerializer, TapDownloadFileArchiveDescriberSerializer
from arch_gen.utils import submit_archive_describer
from arch_gen.permissions import IsOwnerOrReadOnly
# Create your views here.
......@@ -67,7 +69,7 @@ def archive_describer_list(request, format=None):
user = User.objects.filter(username=request.user.username)
data.update({"owner": user[0].id})
serializer = serializer_class(data=data)
serializer = serializer_class(data=data, context={'request': request})
if serializer.is_valid():
serializer.save()
......@@ -97,7 +99,7 @@ def user_archive_describer_list(request, pk, format=None):
user = User.objects.filter(username=request.user.username)
data.update({"owner": user[0].id})
serializer = serializer_class(data=data)
serializer = serializer_class(data=data, context={'request': request})
if serializer.is_valid():
serializer.save()
......@@ -106,6 +108,28 @@ def user_archive_describer_list(request, pk, format=None):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['GET', 'POST'])
@permission_classes([permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly])
def trigger_archive_gen(request, pk, format=None):
try:
abstract_arch_descr = ArchiveDescriber.objects.get(pk=pk)
except ArchiveDescriber.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
arch_descr = get_archive_describer_child(abstract_arch_descr,
abstract_arch_descr.interface_type,
abstract_arch_descr.action_type)
serializer_class = get_archive_describer_serializer_class(abstract_arch_descr.interface_type, abstract_arch_descr.action_type)
serializer = serializer_class(arch_descr, context={'request': request})
print(serializer.data)
submit_archive_describer(serializer.data)
return Response(serializer.data)
@api_view(['GET', 'PUT', 'DELETE'])
@permission_classes([permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly])
def archive_describer_detail(request, pk, format=None):
......@@ -124,12 +148,12 @@ def archive_describer_detail(request, pk, format=None):
if request.method == 'GET':
serializer_class = get_archive_describer_serializer_class(abstract_arch_descr.interface_type, abstract_arch_descr.action_type)
serializer = serializer_class(arch_descr)
serializer = serializer_class(arch_descr, context={'request': request})
return Response(serializer.data)
elif request.method == 'PUT':
serializer_class = get_archive_describer_serializer_class(abstract_arch_descr.interface_type, abstract_arch_descr.action_type)
serializer = serializer_class(arch_descr, data=request.data)
serializer = serializer_class(arch_descr, data=request.data, context={'request': request})
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment