diff options
Diffstat (limited to 'src/hydrilla/proxy/web_ui/repos.py')
-rw-r--r-- | src/hydrilla/proxy/web_ui/repos.py | 68 |
1 files changed, 32 insertions, 36 deletions
diff --git a/src/hydrilla/proxy/web_ui/repos.py b/src/hydrilla/proxy/web_ui/repos.py index 166cf53..629e399 100644 --- a/src/hydrilla/proxy/web_ui/repos.py +++ b/src/hydrilla/proxy/web_ui/repos.py @@ -43,7 +43,7 @@ from . import _app bp = flask.Blueprint('repos', __package__) @bp.route('/repos/add', methods=['GET']) -def add_repo_get(errors: t.Mapping[str, bool] = {}) -> werkzeug.Response: +def add_repo(errors: t.Mapping[str, bool] = {}) -> werkzeug.Response: html = flask.render_template('repos__add.html.jinja', **errors) return flask.make_response(html, 200) @@ -51,7 +51,7 @@ def add_repo_get(errors: t.Mapping[str, bool] = {}) -> werkzeug.Response: def add_repo_post() -> werkzeug.Response: form_data = flask.request.form if 'name' not in form_data or 'url' not in form_data: - return add_repo_get() + return add_repo() try: new_repo_ref = _app.get_haketilo_state().repo_store().add( @@ -59,11 +59,11 @@ def add_repo_post() -> werkzeug.Response: url = form_data['url'] ) except st.RepoNameInvalid: - return add_repo_get({'repo_name_invalid': True}) + return add_repo({'repo_name_invalid': True}) except st.RepoNameTaken: - return add_repo_get({'repo_name_taken': True}) + return add_repo({'repo_name_taken': True}) except st.RepoUrlInvalid: - return add_repo_get({'repo_url_invalid': True}) + return add_repo({'repo_url_invalid': True}) return flask.redirect(flask.url_for('.show_repo', repo_id=new_repo_ref.id)) @@ -81,56 +81,52 @@ def repos() -> werkzeug.Response: return flask.make_response(html, 200) @bp.route('/repos/view/<string:repo_id>') -def show_repo(repo_id: str) -> werkzeug.Response: +def show_repo(repo_id: str, errors: t.Mapping[str, bool] = {}) \ + -> werkzeug.Response: try: store = _app.get_haketilo_state().repo_store() display_info = store.get(repo_id).get_display_info() html = flask.render_template( 'repos__show_single.html.jinja', - display_info = display_info + display_info = display_info, + **errors ) return flask.make_response(html, 200) except st.MissingItemError: flask.abort(404) -def sanitize_altered_repo_id(repo_id: str) -> str: +@bp.route('/repos/view/<string:repo_id>', methods=['POST']) +def alter_repo(repo_id: str) -> werkzeug.Response: repo_id = str(int(repo_id)) if repo_id == '1': # Protect local semi-repo. flask.abort(403) - return repo_id + repo_ref = _app.get_haketilo_state().repo_store().get(repo_id) -@bp.route('/repos/update_url/<string:repo_id>', methods=['POST']) -def update_repo_url(repo_id: str) -> werkzeug.Response: - repo_id = sanitize_altered_repo_id(repo_id) - - try: - repo_ref = _app.get_haketilo_state().repo_store().get(repo_id) - repo_ref.update() - except st.MissingItemError: - flask.abort(404) - - return flask.redirect(flask.url_for('.show_repo', repo_id=repo_id)) - -@bp.route('/repos/remove/<string:repo_id>', methods=['POST']) -def remove_repo(repo_id: str): - repo_id = sanitize_altered_repo_id(repo_id) - - try: - _app.get_haketilo_state().repo_store().get(repo_id).remove() - except st.MissingItemError: - flask.abort(404) - - return flask.redirect(flask.url_for('.repos')) - -@bp.route('/repos/refresh/<string:repo_id>', methods=['POST']) -def refresh_repo(repo_id: str): - repo_id = sanitize_altered_repo_id(repo_id) + form_data = flask.request.form + action = form_data['action'] try: - _app.get_haketilo_state().repo_store().get(repo_id).refresh() + if action == 'remove_repo': + repo_ref.remove() + return flask.redirect(flask.url_for('.repos')) + elif action == 'refresh_repo': + repo_ref.refresh() + elif action == 'update_repo_data': + repo_ref.update( + url = form_data.get('url'), + name = form_data.get('name') + ) + else: + raise ValueError() + except st.RepoNameInvalid: + return show_repo(repo_id, {'repo_name_invalid': True}) + except st.RepoNameTaken: + return show_repo(repo_id, {'repo_name_taken': True}) + except st.RepoUrlInvalid: + return show_repo(repo_id, {'repo_url_invalid': True}) except st.MissingItemError: flask.abort(404) |