diff options
author | Javier Hernandez <jhernandez@soleta.eu> | 2024-01-10 11:23:37 +0100 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2024-01-10 11:31:15 +0100 |
commit | 2509cacc005832b37e181f6a6a4795f5c7e1fff9 (patch) | |
tree | a8ccc3610fa773e4ea0d34a6af6cfc0be0483216 | |
parent | 695fcfe6255fd53b19b416e8375b48d2dc5595e4 (diff) |
views: Add delete folder
Add functionality to allow the user to delete a folder.
Show a confirmation page to the user listing the content of the folder
that is about to delete
-rw-r--r-- | ogcp/forms/action_forms.py | 1 | ||||
-rw-r--r-- | ogcp/templates/actions/folder_delete.html | 29 | ||||
-rw-r--r-- | ogcp/templates/macros.html | 6 | ||||
-rw-r--r-- | ogcp/templates/scopes.html | 2 | ||||
-rw-r--r-- | ogcp/views.py | 49 |
5 files changed, 87 insertions, 0 deletions
diff --git a/ogcp/forms/action_forms.py b/ogcp/forms/action_forms.py index 9110913..cbdaaaa 100644 --- a/ogcp/forms/action_forms.py +++ b/ogcp/forms/action_forms.py @@ -106,6 +106,7 @@ class RepoForm(FlaskForm): submit = SubmitField(label=_l('Submit')) class FolderForm(FlaskForm): + folder_id = HiddenField() server = HiddenField() room = HiddenField() center = HiddenField() diff --git a/ogcp/templates/actions/folder_delete.html b/ogcp/templates/actions/folder_delete.html new file mode 100644 index 0000000..78fc136 --- /dev/null +++ b/ogcp/templates/actions/folder_delete.html @@ -0,0 +1,29 @@ +{% extends 'scopes.html' %} +{% import "bootstrap/wtf.html" as wtf %} + +{% set sidebar_state = 'disabled' %} +{% set btn_back = true %} + +{% block nav_folder %} active{% endblock %} +{% block nav_folder_delete %} active{% endblock %} +{% block content %} + +<h1 class="m-5">{{_('Delete folder')}}</h1> + +<table class="table"> + <tbody> + {% for content in folder_content %} + <tr> + <th scope="row">{{ content['type'] }}</th> + <td>{{ content['name'] }}</td> + </tr> + {% endfor %} + </tbody> +</table> + +{{ wtf.quick_form(form, + method='post', + button_map={'submit': 'danger'}, + extra_classes="mx-5") }} + +{% endblock %} diff --git a/ogcp/templates/macros.html b/ogcp/templates/macros.html index fb7c311..e78cb5b 100644 --- a/ogcp/templates/macros.html +++ b/ogcp/templates/macros.html @@ -47,6 +47,12 @@ {% if state %}style="filter: grayscale(100%);" onclick="return false;"{% endif %} {% if scope.get("selected", False) %}checked{% endif %} name="scope-room" /> + {% elif scope["type"] == "folder" %} + <input class="form-check-input" type="checkbox" form="scopesForm" + value="{{ scope["id"] }}" + {% if state %}style="filter: grayscale(100%);" onclick="return false;"{% endif %} + {% if scope.get("selected", False) %}checked{% endif %} + name="folder" /> {% elif " ".join(scope["ip"]) %} <input class="form-check-input" type="checkbox" form="scopesForm" value="{{ " ".join(scope["ip"]) }}" data-parent-room="{{ parent_room }}" diff --git a/ogcp/templates/scopes.html b/ogcp/templates/scopes.html index ecc8433..23b3d3c 100644 --- a/ogcp/templates/scopes.html +++ b/ogcp/templates/scopes.html @@ -64,6 +64,8 @@ <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"> <input class="btn btn-light dropdown-item {% block nav_folder_add %}{% endblock %}" type="submit" value="{{ _('Add folder') }}" form="scopesForm" formaction="{{ url_for('action_folder_add') }}" formmethod="get"> + <input class="btn btn-light dropdown-item {% block nav_folder_delete %}{% endblock %}" type="submit" value="{{ _('Delete folder') }}" + form="scopesForm" formaction="{{ url_for('action_folder_delete') }}" formmethod="get"> </div> </div> diff --git a/ogcp/views.py b/ogcp/views.py index bd179cd..1203ffd 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -35,6 +35,7 @@ import hashlib import json import os import re +from collections import deque FS_CODES = { 0: 'DISK', @@ -990,6 +991,54 @@ def action_client_update(): return render_template('actions/client_details.html', form=form, parent="scopes.html", scopes=scopes) +def find_folder(folder_id, scopes): + scopes = deque([scopes['scope']]) + while scopes: + scope_ls = scopes.popleft() + if not scope_ls: + continue + else: + for scope in scope_ls: + if scope['type'] == 'folder' and scope['id'] == folder_id: + return scope + else: + scopes.append(scope['scope']) + return None + +@app.route('/action/folder/delete', methods=['GET', 'POST']) +@login_required +def action_folder_delete(): + form = FolderForm(request.form) + if request.method == 'POST': + payload = {"id": int(form.folder_id.data)} + server = get_server_from_ip_port(form.server.data) + r = server.post('/folder/delete', payload) + if r.status_code != requests.codes.ok: + flash(('ogServer: error deleting folder'), + category='error') + else: + flash(('Folder deleted successfully'), category='info') + return redirect(url_for("scopes")) + else: + params = request.args.to_dict() + folder_id = params.get('folder') + if not folder_id: + flash(('Please, select a folder'), category='error') + return redirect(url_for('scopes')) + form.folder_id.data = folder_id + form.server.data = params['scope-server'] + form.submit.render_kw = {"formaction": url_for('action_folder_delete')} + + scopes, _ = get_scopes() + folder = find_folder(int(folder_id), scopes) + if not folder: + flash(('Folder was not found'), category='info') + return redirect(url_for("scopes")) + form.name.data = folder['name'] + form.name.render_kw = {'readonly': True} + return render_template('actions/folder_delete.html', form=form, + parent="scopes.html", scopes=scopes, folder_content=folder['scope']) + @app.route('/action/folder/add', methods=['GET']) @login_required def action_folder_add(): |