repo.py 5.52 KB
Newer Older
Markus Scheidgen's avatar
Markus Scheidgen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
The repository API of the nomad@FAIRDI APIs. Currently allows to resolve repository
meta-data.
"""

from flask_restplus import Resource, abort, fields
21
22
from flask import request, g
from elasticsearch_dsl import Q
Markus Scheidgen's avatar
Markus Scheidgen committed
23

24
from nomad.files import UploadFiles, Restricted
25
from nomad import search
Markus Scheidgen's avatar
Markus Scheidgen committed
26
27

from .app import api
28
from .auth import login_if_available, create_authorization_predicate
Markus Scheidgen's avatar
Markus Scheidgen committed
29
30
from .common import pagination_model, pagination_request_parser, calc_route

31
ns = api.namespace('repo', description='Access repository metadata.')
Markus Scheidgen's avatar
Markus Scheidgen committed
32
33
34
35
36


@calc_route(ns)
class RepoCalcResource(Resource):
    @api.response(404, 'The upload or calculation does not exist')
37
    @api.response(401, 'Not authorized to access the calculation')
38
    @api.response(200, 'Metadata send', fields.Raw)
39
    @api.doc('get_repo_calc')
40
    @login_if_available
41
    def get(self, upload_id, calc_id):
Markus Scheidgen's avatar
Markus Scheidgen committed
42
43
44
45
        """
        Get calculation metadata in repository form.

        Repository metadata only entails the quanties shown in the repository.
46
        Calcs are references via *upload_id*, *calc_id* pairs.
Markus Scheidgen's avatar
Markus Scheidgen committed
47
        """
48
        # TODO use elastic search instead of the files
Markus Scheidgen's avatar
Markus Scheidgen committed
49
        # TODO add missing user metadata (from elastic or repo db)
50
51
52
53
        upload_files = UploadFiles.get(upload_id, create_authorization_predicate(upload_id, calc_id))
        if upload_files is None:
            abort(404, message='There is no upload %s' % upload_id)

Markus Scheidgen's avatar
Markus Scheidgen committed
54
        try:
55
56
57
58
            return upload_files.metadata.get(calc_id), 200
        except Restricted:
            abort(401, message='Not authorized to access %s/%s.' % (upload_id, calc_id))
        except KeyError:
59
            abort(404, message='There is no calculation for %s/%s' % (upload_id, calc_id))
Markus Scheidgen's avatar
Markus Scheidgen committed
60
61
62
63


repo_calcs_model = api.model('RepoCalculations', {
    'pagination': fields.Nested(pagination_model),
64
65
66
67
68
69
    'results': fields.List(fields.Raw, description=(
        'A list of search results. Each result is a dict with quantitie names as key and '
        'values as values')),
    'aggregations': fields.Raw(description=(
        'A dict with all aggregations. Each aggregation is dictionary with the amount as '
        'value and quantity value as key.'))
Markus Scheidgen's avatar
Markus Scheidgen committed
70
71
72
73
74
75
76
})

repo_request_parser = pagination_request_parser.copy()
repo_request_parser.add_argument(
    'owner', type=str,
    help='Specify which calcs to return: ``all``, ``user``, ``staging``, default is ``all``')

77
78
79
80
for search_quantity in search.search_quantities.keys():
    _, _, description = search.search_quantities[search_quantity]
    repo_request_parser.add_argument(search_quantity, type=str, help=description)

Markus Scheidgen's avatar
Markus Scheidgen committed
81
82
83

@ns.route('/')
class RepoCalcsResource(Resource):
84
    @api.doc('get_calcs')
85
    @api.response(400, 'Invalid requests, e.g. wrong owner type or bad quantities')
Markus Scheidgen's avatar
Markus Scheidgen committed
86
87
88
89
90
    @api.expect(repo_request_parser, validate=True)
    @api.marshal_with(repo_calcs_model, skip_none=True, code=200, description='Metadata send')
    @login_if_available
    def get(self):
        """
91
92
93
94
95
96
97
98
        Search for calculations in the repository from, paginated.

        The ``owner`` parameter determines the overall entries to search through.
        You can use the various quantities to search/filter for. For some of the
        indexed quantities this endpoint returns aggregation information. This means
        you will be given a list of all possible values and the number of entries
        that have the certain value. You can also use these aggregations on an empty
        search to determine the possible values.
99
        """
100
        page = int(request.args.get('page', 0))
101
102
103
104
        per_page = int(request.args.get('per_page', 10))
        owner = request.args.get('owner', 'all')

        try:
105
            assert page >= 0
106
107
108
109
110
111
112
113
            assert per_page > 0
        except AssertionError:
            abort(400, message='invalid pagination')

        if owner == 'all':
            if g.user is None:
                q = Q('term', published=True)
            else:
114
                q = Q('term', published=True) | Q('term', owners__user_id=g.user.user_id)
115
116
117
118
        elif owner == 'user':
            if g.user is None:
                abort(401, message='Authentication required for owner value user.')

119
            q = Q('term', owners__user_id=g.user.user_id)
120
121
122
        elif owner == 'staging':
            if g.user is None:
                abort(401, message='Authentication required for owner value user.')
123
            q = Q('term', published=False) & Q('term', owners__user_id=g.user.user_id)
124
125
126
        else:
            abort(400, message='Invalid owner value. Valid values are all|user|staging, default is all')

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        data = dict(**request.args)
        data.pop('owner', None)
        data.pop('page', None)
        data.pop('per_page', None)

        try:
            total, results, aggregations = search.aggregate_search(
                page=page, per_page=per_page, q=q, **data)
        except KeyError as e:
            abort(400, str(e))

        return dict(
            pagination=dict(total=total, page=page, per_page=per_page),
            results=results,
            aggregations=aggregations), 200