summaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/web_ui/prompts.py
blob: b5e052da1f2a79a48530325b47e5b8be5aefb028 (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
# 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 of this
# code in a proprietary program, I am not going to enforce this in
# court.

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__)


def deserialized_request_details(salt: str) -> t.Mapping[str, str]:
    serializer = URLSafeSerializer(
        _app.get_haketilo_state().get_secret(),
        salt = salt
    )

    return serializer.loads(flask.request.args['details'])


@bp.route('/auto_install_error', methods=['GET'])
def auto_install_error_prompt(errors: t.Mapping[str, bool] = {}) \
    -> werkzeug.Response:
    try:
        details = deserialized_request_details('auto_install_error')
    except:
        return flask.redirect(flask.url_for('home'))

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

        display_info = payload_ref.get_display_info()

        if not display_info.has_problems:
            return flask.redirect(details['next_url'])

        html = flask.render_template(
            'prompts/auto_install_error.html.jinja',
            display_info = display_info,
            **errors
        )
        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:
    try:
        details = deserialized_request_details('auto_install_error')
    except:
        return flask.redirect(flask.url_for('home'), code=303)

    form_data = flask.request.form
    action = form_data['action']

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

    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:
        assert action == 'retry_install'
        return auto_install_error_prompt({'repo_communication_error': True})
    except st.FileInstallationError:
        assert action == 'retry_install'
        return auto_install_error_prompt({'file_installation_error': True})
    except st.MissingItemError:
        flask.abort(404)

    return flask.redirect(details['next_url'])


@bp.route('/package_suggestion', methods=['GET'])
def package_suggestion_prompt(errors: t.Mapping[str, bool] = {}) \
    -> werkzeug.Response:
    try:
        details = deserialized_request_details('package_suggestion')
    except:
        return flask.redirect(flask.url_for('home'))

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

        display_info = payload_ref.get_display_info()

        if display_info.mapping_info.active != st.ActiveStatus.AUTO:
            return flask.redirect(details['next_url'])

        html = flask.render_template(
            'prompts/package_suggestion.html.jinja',
            display_info = display_info,
            **errors
        )
        return flask.make_response(html, 200)
    except st.MissingItemError:
        flask.abort(404)

@bp.route('/package_suggestion', methods=['POST'])
def package_suggestion_prompt_post() -> werkzeug.Response:
    try:
        details = deserialized_request_details('package_suggestion')
    except:
        return flask.redirect(flask.url_for('home'))

    form_data = flask.request.form
    action = form_data['action']

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

    state = _app.get_haketilo_state()

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

        if action == 'disable_mapping':
            mapping_ver_ref.update_mapping_status(st.EnabledStatus.DISABLED)
        elif action == 'enable_mapping':
            mapping_ver_ref.update_mapping_status(
                enabled = st.EnabledStatus.ENABLED,
                frozen  = st.FrozenStatus.EXACT_VERSION
            )
        else:
            raise ValueError()
    except st.RepoCommunicationError:
        assert action == 'enable_mapping'
        return package_suggestion_prompt({'repo_communication_error': True})
    except st.FileInstallationError:
        assert action == 'enable_mapping'
        return package_suggestion_prompt({'file_installation_error': True})
    except st.MissingItemError:
        flask.abort(404)

    return flask.redirect(details['next_url'])