aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/edit.py
blob: e09bee9c55ec50eec9fe39f404337618b711a48e (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
import requests

from urllib.parse import urljoin

from gn2.wqflask.oauth2.client import oauth2_get

from flask import (Blueprint,
                   flash,
                   redirect,
                   render_template,
                   request)

from gn2.wqflask.oauth2.checks import require_oauth2
from gn2.wqflask.oauth2.checks import require_oauth2_edit_resource_access


metadata = Blueprint("metadata", __name__)


@metadata.route("/edit")
@require_oauth2_edit_resource_access
@require_oauth2
def metadata_edit():
    """Endpoint that provides editing functionality for datasets."""
    from gn2.utility.tools import GN3_LOCAL_URL
    _name = request.args.get("name")
    match request.args.get("type"):
        case "dcat:Dataset":
            _metadata = requests.get(
                urljoin(
                    GN3_LOCAL_URL,
                    f"api/metadata/datasets/{ _name }"
                )
            ).json()
            _section = request.args.get("section")
            return render_template(
                "metadata/editor.html",
                name=_name,
                metadata=_metadata,
                section=_section,
                edit=_metadata.get(_section),
            )
        case _:
            return redirect(f"/datasets/{_name}")


@metadata.route("/save", methods=["POST"])
@require_oauth2_edit_resource_access
@require_oauth2
def save():
    """Save dataset edits in git."""
    from gn2.utility.tools import GN3_LOCAL_URL
    # Call an endpoint to GN3 with special headers
    name = request.form.get('label')
    metadata = requests.get(
                urljoin(
                    GN3_LOCAL_URL,
                    f"api/metadata/datasets/{name}")).json()
    headers = oauth2_get(
        f"auth/resource/authorisation/{metadata.get('label')}",
        jsonify_p=True
    ).either(
        lambda err: {},
        lambda val: {"Authorization": val.headers.get("Authorization", "")}
    )
    response = requests.post(
        urljoin(GN3_LOCAL_URL, "api/metadata/datasets/edit"),
        data=request.form,
        headers=headers)
    if response.status_code == 201:
        flash("Unable to update data", "alert-danger")
    else:
        flash("Successfully updated data", "success")
    # Make a request to GN3 save endpoint
    return redirect(f"/datasets/{name}")