summaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/web_ui/items.py
blob: 808fb6da8fa5103d7785e25ef5bf0ea1e10c1d1e (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# SPDX-License-Identifier: GPL-3.0-or-later

# Proxy web UI package/library management.
#
# 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 unquote

import flask
import werkzeug

from ... import item_infos
from .. import state as st
from . import _app


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

@bp.route('/packages')
def packages() -> werkzeug.Response:
    store = _app.get_haketilo_state().mapping_store()

    html = flask.render_template(
        'items/packages.html.jinja',
        display_infos = store.get_display_infos()
    )
    return flask.make_response(html, 200)

@bp.route('/libraries')
def libraries() -> werkzeug.Response:
    store = _app.get_haketilo_state().resource_store()

    html = flask.render_template(
        'items/libraries.html.jinja',
        display_infos = store.get_display_infos()
    )
    return flask.make_response(html, 200)

def item_store(state: st.HaketiloState, item_type: item_infos.ItemType) \
    -> t.Union[st.MappingStore, st.ResourceStore]:
        if item_type == item_infos.ItemType.RESOURCE:
            return state.resource_store()
        else:
            return state.mapping_store()

def show_item(
        item_id:   str,
        item_type: item_infos.ItemType,
        errors:    t.Mapping[str, bool] = {}
) -> werkzeug.Response:
    try:
        store = item_store(_app.get_haketilo_state(), item_type)
        display_info = store.get(str(item_id)).get_display_info()

        html = flask.render_template(
            f'items/{item_type.alt_name}_view.html.jinja',
            display_info = display_info,
            **errors
        )
        return flask.make_response(html, 200)
    except st.MissingItemError:
        flask.abort(404)


@bp.route('/libraries/view/<string:item_id>')
def show_library(item_id: str) -> werkzeug.Response:
    return show_item(item_id, item_infos.ItemType.RESOURCE)

@bp.route('/packages/view/<string:item_id>')
def show_package(item_id: str) -> werkzeug.Response:
    return show_item(item_id, item_infos.ItemType.MAPPING)

def alter_item(item_id: str, item_type: item_infos.ItemType) \
    -> werkzeug.Response:
    form_data = flask.request.form
    action = form_data['action']

    try:
        store = item_store(_app.get_haketilo_state(), item_type)
        item_ref = store.get(item_id)

        if action == 'disable_item':
            assert isinstance(item_ref, st.MappingRef)
            item_ref.update_status(st.EnabledStatus.DISABLED)
        elif action == 'unenable_item':
            assert isinstance(item_ref, st.MappingRef)
            item_ref.update_status(st.EnabledStatus.NO_MARK)
        elif action in ('enable_item', 'unfreeze_item'):
            assert isinstance(item_ref, st.MappingRef)
            item_ref.update_status(
                enabled = st.EnabledStatus.ENABLED,
                frozen  = st.FrozenStatus.NOT_FROZEN,
            )
        elif action == 'freeze_to_repo':
            assert isinstance(item_ref, st.MappingRef)
            item_ref.update_status(
                enabled = st.EnabledStatus.ENABLED,
                frozen  = st.FrozenStatus.REPOSITORY,
            )
        elif action == 'freeze_to_version':
            assert isinstance(item_ref, st.MappingRef)
            item_ref.update_status(
                enabled = st.EnabledStatus.ENABLED,
                frozen  = st.FrozenStatus.EXACT_VERSION,
            )
        else:
            raise ValueError()
    except st.RepoCommunicationError:
        return show_item(item_id, item_type, {'repo_communication_error': True})
    except st.FileInstallationError:
        return show_item(item_id, item_type, {'file_installation_error': True})
    except st.ImpossibleSituation:
        errors = {'impossible_situation_error': True}
        return show_item(item_id, item_type, errors)
    except st.MissingItemError:
        flask.abort(404)

    return flask.redirect(
        flask.url_for(f'.show_{item_type.alt_name}', item_id=item_id)
    )

@bp.route('/libraries/view/<string:item_id>', methods=['POST'])
def alter_library(item_id: str) -> werkzeug.Response:
    return alter_item(item_id, item_infos.ItemType.RESOURCE)

@bp.route('/packages/view/<string:item_id>', methods=['POST'])
def alter_package(item_id: str) -> werkzeug.Response:
    return alter_item(item_id, item_infos.ItemType.MAPPING)


ItemVersionDisplayInfo = t.Union[
    st.MappingVersionDisplayInfo,
    st.ResourceVersionDisplayInfo
]

def item_version_store(
        state:     st.HaketiloState,
        item_type: item_infos.ItemType
) -> t.Union[st.MappingVersionStore, st.ResourceVersionStore]:
        if item_type == item_infos.ItemType.RESOURCE:
            return state.resource_version_store()
        else:
            return state.mapping_version_store()

def show_item_version(
        item_version_id: str,
        item_type:          item_infos.ItemType,
        errors:             t.Mapping[str, bool] = {}
) -> werkzeug.Response:
    state = _app.get_haketilo_state()

    try:
        store = item_version_store(state, item_type)
        version_ref = store.get(item_version_id)
        display_info = version_ref.get_item_display_info()

        this_info:   t.Optional[ItemVersionDisplayInfo] = None

        for info in display_info.all_versions:
            if info.ref == version_ref:
                this_info = info

        assert this_info is not None

        html = flask.render_template(
            f'items/{item_type.alt_name}_viewversion.html.jinja',
            display_info         = display_info,
            version_display_info = this_info,
            **errors
        )
        return flask.make_response(html, 200)
    except st.MissingItemError:
        flask.abort(404)

@bp.route('/libraries/viewversion/<string:item_version_id>')
def show_library_version(item_version_id: str) -> werkzeug.Response:
    return show_item_version(item_version_id, item_infos.ItemType.RESOURCE)

@bp.route('/packages/viewversion/<string:item_version_id>')
def show_package_version(item_version_id: str) -> werkzeug.Response:
    return show_item_version(item_version_id, item_infos.ItemType.MAPPING)

def alter_item_version(item_version_id: str, item_type: item_infos.ItemType) \
    -> werkzeug.Response:
    form_data = flask.request.form
    action = form_data['action']

    try:
        store = item_version_store(_app.get_haketilo_state(), item_type)
        item_version_ref = store.get(item_version_id)

        if action == 'disable_item':
            assert isinstance(item_version_ref, st.MappingVersionRef)
            item_version_ref.update_mapping_status(st.EnabledStatus.DISABLED)
        elif action == 'unenable_item':
            assert isinstance(item_version_ref, st.MappingVersionRef)
            item_version_ref.update_mapping_status(st.EnabledStatus.NO_MARK)
        elif action in ('enable_item_version', 'freeze_to_version'):
            assert isinstance(item_version_ref, st.MappingVersionRef)
            item_version_ref.update_mapping_status(
                enabled = st.EnabledStatus.ENABLED,
                frozen  = st.FrozenStatus.EXACT_VERSION,
            )
        elif action == 'unfreeze_item':
            assert isinstance(item_version_ref, st.MappingVersionRef)
            item_version_ref.update_mapping_status(
                enabled = st.EnabledStatus.ENABLED,
                frozen  = st.FrozenStatus.NOT_FROZEN,
            )
        elif action == 'freeze_to_repo':
            assert isinstance(item_version_ref, st.MappingVersionRef)
            item_version_ref.update_mapping_status(
                enabled = st.EnabledStatus.ENABLED,
                frozen  = st.FrozenStatus.REPOSITORY,
            )
        elif action == 'install_item_version':
            item_version_ref.install()
        elif action == 'uninstall_item_version':
            item_version_ref_after = item_version_ref.uninstall()
            if item_version_ref_after is None:
                url = flask.url_for(f'.{item_type.alt_name_plural}')
                return flask.redirect(url)
            else:
                return show_item_version(item_version_id, item_type)
        else:
            raise ValueError()
    except st.RepoCommunicationError:
        return show_item_version(
            item_version_id = item_version_id,
            item_type       = item_type,
            errors          = {'repo_communication_error': True}
        )
    except st.FileInstallationError:
        return show_item_version(
            item_version_id = item_version_id,
            item_type       = item_type,
            errors          = {'file_installation_error': True}
        )
    except st.ImpossibleSituation:
        return show_item_version(
            item_version_id = item_version_id,
            item_type       = item_type,
            errors          = {'impossible_situation_error': True}
        )
    except st.MissingItemError:
        flask.abort(404)

    return flask.redirect(
        flask.url_for(
            f'.show_{item_type.alt_name}_version',
            item_version_id = item_version_id
        )
    )

@bp.route('/libraries/viewversion/<string:item_version_id>', methods=['POST'])
def alter_library_version(item_version_id: str) -> werkzeug.Response:
    return alter_item_version(item_version_id, item_infos.ItemType.RESOURCE)

@bp.route('/packages/viewversion/<string:item_version_id>', methods=['POST'])
def alter_package_version(item_version_id: str) -> werkzeug.Response:
    return alter_item_version(item_version_id, item_infos.ItemType.MAPPING)

def show_file(
        item_version_id: str,
        item_type:       item_infos.ItemType,
        file_type:       str,
        name:            str,
) -> werkzeug.Response:
    if file_type not in ('license', 'web_resource'):
        flask.abort(404)

    try:
        store = item_version_store(_app.get_haketilo_state(), item_type)
        item_version_ref = store.get(item_version_id)

        try:
            if file_type == 'license':
                file_data = item_version_ref.get_license_file(name)
            else:
                assert isinstance(item_version_ref, st.ResourceVersionRef)
                file_data = item_version_ref.get_resource_file(name)

            return werkzeug.Response(
                file_data.contents,
                mimetype = file_data.mime_type
            )
        except st.MissingItemError:
            if file_type == 'license':
                url = item_version_ref.get_upstream_license_file_url(name)
            else:
                assert isinstance(item_version_ref, st.ResourceVersionRef)
                url = item_version_ref.get_upstream_resource_file_url(name)

            return flask.redirect(url)

    except st.MissingItemError:
        flask.abort(404)

@bp.route('/packages/viewversion/<string:item_version_id>/<string:file_type>/<path:name>')
def show_mapping_file(item_version_id: str, file_type: str, name: str) \
    -> werkzeug.Response:
    item_type = item_infos.ItemType.MAPPING
    return show_file(item_version_id, item_type, file_type, name)

@bp.route('/libraries/viewversion/<string:item_version_id>/<string:file_type>/<path:name>')
def show_resource_file(item_version_id: str, file_type: str, name: str) \
    -> werkzeug.Response:
    item_type = item_infos.ItemType.RESOURCE
    return show_file(item_version_id, item_type, file_type, name)

@bp.route('/libraries/viewdep/<string:item_version_id>/<string:dep_identifier>')
def show_library_dep(item_version_id: str, dep_identifier: str) \
    -> werkzeug.Response:
    state = _app.get_haketilo_state()

    try:
        store = state.resource_version_store()
        dep_id = store.get(item_version_id).get_dependency(dep_identifier).id
        url = flask.url_for('.show_library_version', item_version_id=dep_id)
    except st.MissingItemError:
        try:
            versionless_store = state.resource_store()
            item_ref = versionless_store.get_by_identifier(dep_identifier)
            url = flask.url_for('.show_library', item_id=item_ref.id)
        except st.MissingItemError:
            flask.abort(404)

    return flask.redirect(url)

@bp.route('/<string:item_type>/viewrequired/<string:item_version_id>/<string:required_identifier>')
def show_required_mapping(
        item_type:           str,
        item_version_id:     str,
        required_identifier: str
) -> werkzeug.Response:
    state = _app.get_haketilo_state()

    if item_type not in ('package', 'library'):
        flask.abort(404)

    found = False

    if item_type == 'package':
        try:
            ref = state.mapping_version_store().get(item_version_id)
            mapping_ver_id = ref.get_required_mapping(required_identifier).id
            url = flask.url_for(
                '.show_package_version',
                item_version_id = mapping_ver_id
            )
            found = True
        except st.MissingItemError:
            pass

    if not found:
        try:
            versionless_store = state.mapping_store()
            mapping_ref = versionless_store\
                .get_by_identifier(required_identifier)
            url = flask.url_for('.show_package', item_id=mapping_ref.id)
        except st.MissingItemError:
            flask.abort(404)

    return flask.redirect(url)

@bp.route('/package/viewpayload/<string:item_version_id>/<string:pattern>/<string:lib_identifier>')
def show_payload(item_version_id: str, pattern: str, lib_identifier: str) \
    -> werkzeug.Response:
    state = _app.get_haketilo_state()

    try:
        ref = state.mapping_version_store().get(item_version_id)

        try:
            resource_ver_ref = \
                ref.get_payload_resource(unquote(pattern), lib_identifier)
            url = flask.url_for(
                '.show_library_version',
                item_version_id = resource_ver_ref.id
            )
        except st.MissingItemError:
            resource_ref = \
                state.resource_store().get_by_identifier(lib_identifier)
            url = flask.url_for('.show_library', item_id=resource_ref.id)
    except st.MissingItemError:
        flask.abort(404)

    return flask.redirect(url)