aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/web_ui/prompts.py
blob: b546e47c30854851b9fc6bfef48f26eb02183da1 (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
# SPDX-License-Identifier: GPL-3.0-or-later

# Proxy web UI pages that may be shown to the user without manual navigation to
# Haketilo meta-site.
#
# This file is part of Hydrilla&Haketilo.
#
# Copyright (C) 2022 Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use this code
# in a proprietary program, I am not going to enforce this in court.

# Enable using with Python 3.7.
from __future__ import annotations

import typing as t

from urllib.parse import urlencode

from itsdangerous.url_safe import URLSafeSerializer
import flask
import werkzeug

from .. import state as st
from . import _app


bp = flask.Blueprint('prompts', __package__)

@bp.route('/auto_install_error', methods=['GET'])
def auto_install_error_prompt() -> werkzeug.Response:
    state = _app.get_haketilo_state()

    serializer = URLSafeSerializer(
        state.get_secret(),
        salt = 'auto_install_error'
    )

    try:
        details: t.Mapping[str, str] = \
            serializer.loads(flask.request.args['details'])
    except:
        return flask.redirect(flask.url_for('home'))

    try:
        payload_ref = state.payload_store().get(details['payload_id'])

        display_info = payload_ref.get_display_info()

        html = flask.render_template(
            'prompts/auto_install_error.html.jinja',
            display_info = display_info,
            next_url     = details['next_url']
        )
        return flask.make_response(html, 200)
    except st.MissingItemError:
        flask.abort(404)

@bp.route('/auto_install_error', methods=['POST'])
def auto_install_error_prompt_post() -> werkzeug.Response:
    form_data = flask.request.form
    action = form_data['action']

    mapping_ver_id = str(int(form_data['mapping_ver_id']))
    payload_id     = str(int(form_data['payload_id']))
    next_url       = form_data['next_url']

    state = _app.get_haketilo_state()

    try:
        mapping_ver_store = state.mapping_version_store()
        mapping_ver_ref = mapping_ver_store.get(mapping_ver_id)

        payload_store = _app.get_haketilo_state().payload_store()
        payload_ref = payload_store.get(payload_id)

        if action == 'disable_mapping':
            mapping_ver_ref.update_mapping_status(st.EnabledStatus.DISABLED)
        elif action == 'retry_install':
            payload_ref.ensure_items_installed()
        else:
            raise ValueError()
    except (st.RepoCommunicationError, st.FileInstallationError):
        params = {'payload_id': payload_id, 'next_url': next_url}

        serializer = URLSafeSerializer(
            state.get_secret(),
            salt = 'auto_install_error'
        )
        query = urlencode({'details': params})
        redirect_url = flask.url_for(
            '.auto_install_error_prompt',
            details = serializer.dumps(params)
        )

        return flask.redirect(redirect_url)
    except st.MissingItemError:
        flask.abort(404)

    return flask.redirect(next_url)