Commit ebbd67f8 authored by Alvin Noe Ladines's avatar Alvin Noe Ladines
Browse files

Implemented files and users endpoints in mirror

parent 5d4424f1
Pipeline #71034 passed with stages
in 14 minutes and 53 seconds
......@@ -16,12 +16,14 @@
The mirror API of the nomad@FAIRDI APIs. Allows to export upload metadata.
'''
from flask import request
from flask import request, send_file
from flask_restplus import Resource, abort, fields
from nomad import processing as proc
from nomad.datamodel import Dataset
from nomad.doi import DOI
from nomad.files import PublicUploadFiles
from nomad import infrastructure
from .api import api
from .auth import authenticate
......@@ -115,3 +117,73 @@ class MirrorUploadResource(Resource):
'dois': dois,
'upload_files_path': upload.upload_files.os_path
}, 200
_mirror_files_parser = api.parser()
_mirror_files_parser.add_argument(
'prefix', type='str', help='File to download archive or raw', location='args')
@upload_route(ns, '/files')
class MirrorFilesResource(Resource):
@api.doc('download_files_mirror')
@api.expect(_mirror_files_parser, validate=True)
@api.response(400, 'Invalid requests, e.g. wrong owner type or bad search parameters')
@api.response(404, 'The upload or calculation does not exist')
@authenticate(admin_only=True)
def get(self, upload_id):
'''
Download archive and raw files for mirrors
'''
try:
args = request.args
prefix = args.get('prefix')
assert prefix in ('archive', 'raw')
except Exception:
abort(400, message='bad parameter types')
try:
upload_files = PublicUploadFiles(upload_id)
ext = 'json'
if prefix == 'raw':
ext = 'plain'
fileobj = upload_files.get_zip_file(prefix, 'public', ext)
if not fileobj.exists():
raise KeyError
return send_file(
open(fileobj.os_path, 'rb'),
mimetype='application/zip',
as_attachment=True,
cache_timeout=0,
attachment_filename=fileobj.os_path)
except KeyError:
abort(404, message='Upload %d does not exist' % upload_id)
except Exception:
abort(400, message='File sending failed.')
@ns.route('/users')
class MirrorUsersResource(Resource):
@api.doc('downlod_users_mirror')
@api.response(400, 'Unsuccessful userlist query')
@authenticate(admin_only=True)
def get(self):
'''
Download user list for mirrors
'''
try:
users = infrastructure.keycloak.search_user(query='')
result = dict()
for user in users:
credentials = user.m_to_dict()
credentials.pop('email', None)
result[user.username] = credentials
return result, 200
except Exception:
abort(400, message='Failed to fetch users')
......@@ -1702,6 +1702,18 @@ class TestMirror:
else:
assert 'dois' not in data
def test_files(self, api, published, admin_user_auth, no_warn):
url = '/mirror/files/%s?prefix=archive' % published.upload_id
rv = api.get(url, headers=admin_user_auth)
assert rv.status_code == 200
assert rv.data is not None
def test_users(self, api, published, admin_user_auth, no_warn):
url = '/mirror/users'
rv = api.get(url, headers=admin_user_auth)
assert rv.status_code == 200
assert rv.data is not None
class TestDataset:
......
Supports Markdown
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