aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/oauth2/data.py
blob: 16e5f60c16ab27b9e080c102ce15f7f1a5cfc831 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""Handle linking data to groups."""
import sys
import json
import uuid
from datetime import datetime
from urllib.parse import urljoin

from redis import Redis
from flask import (
    flash, request, jsonify, url_for, redirect, Response, Blueprint,
    current_app as app)

from gn2.wqflask.oauth2.request_utils import with_flash_error

from gn2.jobs import jobs
from .ui import render_ui
from .request_utils import process_error
from .client import oauth2_get, oauth2_post, authserver_uri

data = Blueprint("data", __name__)

def __search_mrna__(query, template, **kwargs):
    species_name = kwargs["species_name"]
    search_uri = urljoin(authserver_uri(), "auth/data/search")
    datasets = oauth2_get(
        "auth/data/search",
        json = {
            "query": query,
            "dataset_type": "mrna",
            "species_name": species_name,
            "selected": __selected_datasets__()
        }).either(
            lambda err: {"datasets_error": process_error(err)},
            lambda datasets: {"datasets": datasets})
    return render_ui(template, search_uri=search_uri, **datasets, **kwargs)

def __selected_datasets__():
    if bool(request.json):
        return request.json.get(
            "selected",
            request.args.get("selected",
                             request.form.get("selected", [])))
    return request.args.get("selected",
                            request.form.get("selected", []))

def __search_genotypes__(query, template, **kwargs):
    species_name = kwargs["species_name"]
    search_uri = urljoin(authserver_uri(), "auth/data/search")
    datasets = oauth2_get(
        "auth/data/search",
        json = {
            "query": query,
            "dataset_type": "genotype",
            "species_name": species_name,
            "selected": __selected_datasets__()
        }).either(
            lambda err: {"datasets_error": process_error(err)},
            lambda datasets: {"datasets": datasets})
    return render_ui(template, search_uri=search_uri, **datasets, **kwargs)

def __search_phenotypes__(query, template, **kwargs):
    from gn2.utility.tools import GN_SERVER_URL
    page = int(request.args.get("page", 1))
    per_page = int(request.args.get("per_page", 50))
    selected_traits = request.form.getlist("selected_traits")
    def __search_success__(search_results):
        job_id = uuid.UUID(search_results["job_id"])
        return render_ui(
            template, traits=[], per_page=per_page, query=query,
            selected_traits=selected_traits, search_results=search_results,
            search_endpoint=urljoin(
                request.host_url, "oauth2/data/phenotype/search"),
            auth_server_url=authserver_uri(),
            pheno_results_template=urljoin(
                authserver_uri(), "auth/data/search/phenotype/<jobid>"),
            results_endpoint=urljoin(
                authserver_uri(),
                f"auth/data/search/phenotype/{job_id}"),
            **kwargs)
    return oauth2_get("auth/data/search", json={
        "dataset_type": "phenotype",
        "species_name": kwargs["species_name"],
        "per_page": per_page,
        "page": page,
        "gn3_server_uri": GN_SERVER_URL
    }).either(
        with_flash_error(redirect(url_for('oauth2.data.list_data'))),
        __search_success__)

@data.route("/genotype/search", methods=["POST"])
def json_search_genotypes() -> Response:
    def __handle_error__(err):
        error = process_error(err)
        return jsonify(error), error["status_code"]
    
    return oauth2_get(
        "auth/data/search",
        json = {
            "query": request.json["query"],
            "dataset_type": "genotype",
            "species_name": request.json["species_name"],
            "selected": __selected_datasets__()
        }).either(
            __handle_error__,
            lambda datasets: jsonify(datasets))

@data.route("/mrna/search", methods=["POST"])
def json_search_mrna() -> Response:
    def __handle_error__(err):
        error = process_error(err)
        return jsonify(error), error["status_code"]

    return oauth2_get(
        "auth/data/search",
        json = {
            "query": request.json["query"],
            "dataset_type": "mrna",
            "species_name": request.json["species_name"],
            "selected": __selected_datasets__()
        }).either(
            __handle_error__,
            lambda datasets: jsonify(datasets))

@data.route("/phenotype/search", methods=["POST"])
def json_search_phenotypes() -> Response:
    """Search for phenotypes."""
    from gn2.utility.tools import GN_SERVER_URL
    form = request.json
    def __handle_error__(err):
        error = process_error(err)
        return jsonify(error), error["status_code"]

    return oauth2_get(
        "auth/data/search",
        json={
            "dataset_type": "phenotype",
            "species_name": form["species_name"],
            "query": form.get("query", ""),
            "per_page": int(form.get("per_page", 50)),
            "page": int(form.get("page", 1)),
            "auth_server_uri": authserver_uri(),
            "gn3_server_uri": GN_SERVER_URL,
            "selected_traits": form.get("selected_traits", [])
        }).either(__handle_error__, jsonify)

@data.route("/<string:species_name>/<string:dataset_type>/list",
            methods=["GET", "POST"])
def list_data_by_species_and_dataset(
        species_name: str, dataset_type: str) -> Response:
    templates = {
        "mrna": "oauth2/data-list-mrna.html",
        "genotype": "oauth2/data-list-genotype.html",
        "phenotype": "oauth2/data-list-phenotype.html"
    }
    search_fns = {
        "mrna": __search_mrna__,
        "genotype": __search_genotypes__,
        "phenotype": __search_phenotypes__
    }
    groups = oauth2_get("auth/group/list").either(
        lambda err: {"groups_error": process_error(err)},
        lambda grps: {"groups": grps})
    query = request.args.get("query", "")
    return search_fns[dataset_type](
        query, templates[dataset_type], **groups, species_name=species_name,
        dataset_type=dataset_type)

@data.route("/list", methods=["GET", "POST"])
def list_data():
    """List ungrouped data."""
    def __render__(**kwargs):
        roles = kwargs.get("roles", [])
        user_privileges = tuple(
                privilege["privilege_id"] for role in roles
                for privilege in role["privileges"])
        return render_ui(
            "oauth2/data-list.html",
            groups=kwargs.get("groups", []),
            data_items=kwargs.get("data_items", []),
            user_privileges=user_privileges,
            **{key:val for key,val in kwargs.items()
               if key not in ("groups", "data_items", "user_privileges")})

    groups = oauth2_get("auth/group/list").either(
        lambda err: {"groups_error": process_error(err)},
        lambda grp: {"groups": grp})
    roles = oauth2_get("auth/system/roles").either(
        lambda err: {"roles_error": process_error(err)},
        lambda roles: {"roles": roles})
    species = oauth2_get("auth/data/species").either(
        lambda err: {"species_error": process_error(err)},
        lambda species: {"species": species})

    if request.method == "GET":
        return __render__(**{**groups, **roles, **species})

    species_name = request.form["species_name"]
    dataset_type = request.form["dataset_type"]
    if dataset_type not in ("mrna", "genotype", "phenotype"):
        flash("InvalidDatasetType: An invalid dataset type was provided",
              "alert-danger")
        return __render__(**{**groups, **roles, **species})

    return redirect(url_for(
        "oauth2.data.list_data_by_species_and_dataset",
        species_name=species_name, dataset_type=dataset_type))

@data.route("/link", methods=["POST"])
def link_data():
    """Link the selected data to a specific group."""
    def __error__(err, form_data):
        error = process_error(err)
        flash(f"{error['error']}: {error['error_description']}", "alert-danger")
        return redirect(url_for("oauth2.data.list_data", **form_data), code=307)
    def __success__(success, form_data):
        flash("Data successfully linked!", "alert-success")
        return redirect(url_for("oauth2.data.list_data", **form_data), code=307)

    form = request.form
    try:
        keys = ("dataset_type", "group_id")
        assert all(item in form for item in keys)
        assert all(bool(form[item]) for item in keys)
        state_data = {
            "dataset_type": form["dataset_type"],
            "offset": form.get("offset", 0)}
        dataset_ids = form.getlist("dataset_ids")
        if len(dataset_ids) == 0:
            flash("You must select at least one item to link", "alert-danger")
            return redirect(url_for(
                "oauth2.data.list_data", **state_data))
        return oauth2_post(
            "auth/group/data/link",
            data={
                "dataset_type": form["dataset_type"],
                "dataset_ids": dataset_ids,
                "group_id": form["group_id"]
            }).either(lambda err: __error__(err, state_data),
                      lambda success: __success__(success, state_data))
    except AssertionError as aserr:
        flash("You must provide all the expected data.", "alert-danger")
        return redirect(url_for("oauth2.data.list_data"))

@data.route("/link/genotype", methods=["POST"])
def link_genotype_data():
    """Link genotype data to a group."""
    form = request.form
    link_source_url = redirect(url_for("oauth2.data.list_data"))
    if bool(form.get("species_name")):
        link_source_url = redirect(url_for(
            "oauth2.data.list_data_by_species_and_dataset",
            species_name=form["species_name"], dataset_type="genotype"))

    def __link_error__(err):
        flash(f"{err['error']}: {err['error_description']}", "alert-danger")
        return link_source_url

    def __link_success__(success):
        flash(success["description"], "alert-success")
        return link_source_url

    return oauth2_post("auth/data/link/genotype", json={
        "species_name": form.get("species_name"),
        "group_id": form.get("group_id"),
        "selected": tuple(json.loads(dataset) for dataset
                                   in form.getlist("selected"))
    }).either(lambda err: __link_error__(process_error(err)), __link_success__)


@data.route("/link/mrna", methods=["POST"])
def link_mrna_data():
    """Link mrna data to a group."""
    form = request.form
    link_source_url = redirect(url_for("oauth2.data.list_data"))
    if bool(form.get("species_name")):
        link_source_url = redirect(url_for(
            "oauth2.data.list_data_by_species_and_dataset",
            species_name=form["species_name"], dataset_type="mrna"))

    def __link_error__(err):
        error = process_error(err)
        flash(f"{err['error']}: {err['error_description']}", "alert-danger")
        return link_source_url

    def __link_success__(success):
        flash(success["description"], "alert-success")
        return link_source_url

    return oauth2_post("auth/data/link/mrna", json={
        "species_name": form.get("species_name"),
        "group_id": form.get("group_id"),
        "selected": tuple(json.loads(dataset) for dataset
                                   in form.getlist("selected"))
    }).either(lambda err: __link_error__(process_error(err)), __link_success__)

@data.route("/link/phenotype", methods=["POST"])
def link_phenotype_data():
    """Link phenotype data to a group."""
    form = request.form
    link_source_url = redirect(url_for("oauth2.data.list_data"))
    if bool(form.get("species_name")):
        link_source_url = redirect(url_for(
            "oauth2.data.list_data_by_species_and_dataset",
            species_name=form["species_name"], dataset_type="phenotype"))

    def __link_error__(err):
        error = process_error(err)
        flash(f"{error['error']}: {error['error_description']}", "alert-danger")
        return link_source_url

    def __link_success__(success):
        flash(success["description"], "alert-success")
        return link_source_url

    return oauth2_post("auth/data/link/phenotype", json={
        "species_name": form.get("species_name"),
        "group_id": form.get("group_id"),
        "selected": tuple(
            json.loads(trait) for trait in form.getlist("selected"))}).either(
                __link_error__, __link_success__)