From 2509cacc005832b37e181f6a6a4795f5c7e1fff9 Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Wed, 10 Jan 2024 11:23:37 +0100 Subject: 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 --- ogcp/forms/action_forms.py | 1 + ogcp/templates/actions/folder_delete.html | 29 ++++++++++++++++++ ogcp/templates/macros.html | 6 ++++ ogcp/templates/scopes.html | 2 ++ ogcp/views.py | 49 +++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 ogcp/templates/actions/folder_delete.html (limited to 'ogcp') 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 %} + +

{{_('Delete folder')}}

+ + + + {% for content in folder_content %} + + + + + {% endfor %} + +
{{ content['type'] }}{{ content['name'] }}
+ +{{ 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" %} + {% elif " ".join(scope["ip"]) %} + 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(): -- cgit v1.2.3-18-g5258