diff options
author | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-12-04 16:36:04 +0100 |
---|---|---|
committer | Alejandro Sirgo Rica <asirgo@soleta.eu> | 2024-12-04 16:36:04 +0100 |
commit | edd44da64c4660a7b3eb195cdf11eae88d95c85a (patch) | |
tree | 01c7750753334835425a141114635c6840a2d5ff | |
parent | f02c899e3ed0642148e3ff3121456dc28885fbfe (diff) |
ogcp: add Lives category to the main navbar
Add Lives view and show the lives installed in each server in the
sidebar.
Add view to set a new default live image in the Lives view.
-rw-r--r-- | ogcp/forms/action_forms.py | 1 | ||||
-rw-r--r-- | ogcp/templates/actions/live_default.html | 21 | ||||
-rw-r--r-- | ogcp/templates/base.html | 3 | ||||
-rw-r--r-- | ogcp/templates/lives.html | 57 | ||||
-rw-r--r-- | ogcp/views.py | 57 |
5 files changed, 138 insertions, 1 deletions
diff --git a/ogcp/forms/action_forms.py b/ogcp/forms/action_forms.py index acd63bd..4d5d52a 100644 --- a/ogcp/forms/action_forms.py +++ b/ogcp/forms/action_forms.py @@ -191,6 +191,7 @@ class BootModeForm(FlaskForm): class OgliveForm(FlaskForm): ips = HiddenField() + server = HiddenField() oglive = SelectField(label=_l('ogLive')) ok = SubmitField(label=_l('Submit')) diff --git a/ogcp/templates/actions/live_default.html b/ogcp/templates/actions/live_default.html new file mode 100644 index 0000000..5cd6858 --- /dev/null +++ b/ogcp/templates/actions/live_default.html @@ -0,0 +1,21 @@ +{% extends 'lives.html' %} +{% import "bootstrap/wtf.html" as wtf %} + +{% set sidebar_state = 'disabled' %} +{% set btn_back = true %} + +{% block content %} + +<h2 class="mx-5 subhead-heading"> + {{ _('Set default ogLive') }} +</h2> + +<p class="mx-5">{{ _('Default live: %(default_live)s', default_live=default_live) }}</p> + +{{ wtf.quick_form(form, + action=url_for('action_live_default'), + method='post', + button_map={'ok': 'primary'}, + extra_classes="m-5") }} + +{% endblock %}
\ No newline at end of file diff --git a/ogcp/templates/base.html b/ogcp/templates/base.html index 2bdadfc..5efe462 100644 --- a/ogcp/templates/base.html +++ b/ogcp/templates/base.html @@ -43,6 +43,9 @@ <li class="nav-item {% block nav_servers %}{% endblock %}"> <a class="nav-link" href="{{ url_for('manage_servers') }}">{{ _('Servers') }}</a> </li> + <li class="nav-item {% block nav_lives %}{% endblock %}"> + <a class="nav-link" href="{{ url_for('manage_lives') }}">{{ _('Lives') }}</a> + </li> <li class="nav-item {% block nav_users %}{% endblock %}"> <a class="nav-link" href="{{ url_for('users') }}">{{ _('Users') }}</a> </li> diff --git a/ogcp/templates/lives.html b/ogcp/templates/lives.html new file mode 100644 index 0000000..7aaf9c9 --- /dev/null +++ b/ogcp/templates/lives.html @@ -0,0 +1,57 @@ +{% extends 'base.html' %} + +{% block nav_lives %}active{% endblock %} + +{% block container %} + <form id="livesForm"> + <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/> + </form> + {{ super() }} +</form> + +<script> + // Launch the javascript on document ready, so all the global functions exists + // in the scope + document.addEventListener('readystatechange', () => { + if (document.readyState === 'complete') { + keepTreeState('#servers', 'lives'); + } + }); +</script> +{% endblock %} + +{% block sidebar %} + <ul id="servers" class="nav ogcp-nav flex-column nav-pills"> + {% for lives_data in oglive_list %} + <li class="nav-item"> + {% set server_ip_port = lives_data["server"].ip ~ ":" ~ lives_data["server"].port %} + <input id="{{ server_ip_port }}" class="form-check-input" type="checkbox" form="livesForm" + {% if sidebar_state %}style="filter: grayscale(100%);" onclick="return false;"{% endif %} + value="{{ server_ip_port }}" name="server" + {% if loop.index == 1 %}checked{% endif %}></input> + <a class="nav-link" data-toggle="collapse" href="#server{{loop.index}}"> + {{ lives_data["server"]["name"] }} + </a> + <ul class="nav flex-column collapse" id="server{{loop.index}}"> + {% for oglive in lives_data["json"]["oglive"] %} + <li class="nav-item"> + <a>{{ oglive["directory"] }}</a> + </li> + {% endfor %} + </ul> + </li> + {% endfor %} + </ul> +{% endblock %} + +{% block commands %} +{% if current_user.is_authenticated %} + <input class="btn btn-light {% block nav_live_default %}{% endblock %}" type="submit" value="{{ _('Set default') }}" + form="livesForm" formaction="{{ url_for('action_live_default') }}" formmethod="get"> + {% if btn_back %} + <button class="btn btn-danger ml-3" type="button" id="backButton" onclick="history.back()"> + {{ _("Back") }} + </button> + {% endif %} +{% endif %} +{% endblock %} diff --git a/ogcp/views.py b/ogcp/views.py index 5d08f28..f298213 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -3484,7 +3484,6 @@ def action_repo_info(): def manage_servers(): return render_template('servers.html', servers=servers) - @app.route('/server/update', methods=['GET']) @login_required def server_update_get(): @@ -3638,6 +3637,62 @@ def server_delete_post(): category='error') return redirect(url_for('manage_servers')) +@app.route('/lives/', methods=['GET']) +@login_required +def manage_lives(): + oglive_list = multi_request('get', '/oglive/list') + return render_template('lives.html', oglive_list=oglive_list, servers=servers) + +@app.route('/action/live/default', methods=['GET', 'POST']) +@login_required +def action_live_default(): + form = OgliveForm(request.form) + if request.method == 'POST': + try: + server = get_server_from_ip_port(form.server.data) + except KeyError: + flash(_('Please, select one server'), category='error') + return redirect(url_for('manage_lives')) + payload = {'name': form.oglive.data} + r = server.post('/oglive/default', payload) + if not r: + return ogserver_down('manage_lives') + if r.status_code == requests.codes.ok: + flash(_('Set default ogLive request sent successfully'), + category='info') + else: + flash(_('Ogserver replied with status code not ok'), + category='error') + return redirect(url_for('manage_lives')) + else: + params = request.args.to_dict() + try: + server = get_server_from_ip_port(params['server']) + except KeyError: + flash(_('Please, select one server'), category='error') + return redirect(url_for('manage_lives')) + r = server.get('/oglive/list') + if not r: + return ogserver_down('commands') + if r.status_code != requests.codes.ok: + return ogserver_error('commands') + + oglive_list = r.json()['oglive'] + + available_oglives = [] + for oglive in oglive_list: + live_entry = (oglive.get('directory'), oglive.get('directory')) + available_oglives.append(live_entry) + + default_idx = int(r.json()['default']) + default_live = oglive_list[default_idx].get('directory') + + form.oglive.choices = available_oglives + form.server.data = params['server'] + form.ok.render_kw = {'formaction': url_for('action_live_default')} + + oglive_list = multi_request('get', '/oglive/list') + return render_template('actions/live_default.html', oglive_list=oglive_list, form=form, default_live=default_live) @app.route('/users/', methods=['GET']) @login_required |