From bccec3111ebcaa512afd81ff9a7a0b9859fc2c42 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 18 Oct 2019 15:21:35 +0200 Subject: [PATCH 01/15] more samba --- dirkules/samba/manager.py | 13 +++++++------ dirkules/samba/templates/samba/add.html | 8 ++++---- dirkules/samba/validation.py | 2 +- dirkules/samba/views.py | 3 ++- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/dirkules/samba/manager.py b/dirkules/samba/manager.py index 4c6baaa..d64fd55 100644 --- a/dirkules/samba/manager.py +++ b/dirkules/samba/manager.py @@ -1,6 +1,6 @@ from dirkules.config import staticDir -from dirkules import db, app_version +from dirkules import db, app_version, app from dirkules.samba.models import SambaGlobal @@ -18,15 +18,16 @@ def generate_smb(): if SambaGlobal.query.first() is None: workgroup = 'WORKGROUP' server_string = '%h dirkules' + app.logger.warning("Samba not configured. Using default fallback.") else: workgroup = SambaGlobal.query.get(1) server_string = SambaGlobal.query.get(2) f = open("smb.conf.txt", "w") samba_global = open(staticDir + "/conf/samba_global.conf", "r") - f.write("# This file was generated by dirkules v{}".format(app_version)) - f.write() - f.write("# Global Config") - f.write("server string = {}".format(server_string)) - f.write("workgroup = {}".format(workgroup)) + f.write("# This file was generated by dirkules v{}\n".format(app_version)) + f.write("") + f.write("# Global Config\n") + f.write("server string = {}\n".format(server_string)) + f.write("workgroup = {}\n".format(workgroup)) f.write(samba_global.read()) f.close() diff --git a/dirkules/samba/templates/samba/add.html b/dirkules/samba/templates/samba/add.html index b41d36b..26e1fef 100644 --- a/dirkules/samba/templates/samba/add.html +++ b/dirkules/samba/templates/samba/add.html @@ -1,13 +1,13 @@ {% extends "base.html" %} -{% block title %}Dashboard{% endblock %} +{% block title %}Freigabe hinzufügen{% endblock %} {% block head %} {{ super() }} {% endblock %} {% block body %}
- Neue Samba Share einrichten {% from "_formhelpers.html" import render_field %}
+ {{ form.hidden_tag() }}

Allgemeine Informationen

{{ render_field(form.name) }}
@@ -17,12 +17,12 @@

Erweiterte Informationen

{{ render_field(form.path) }} - {{ render_field(form.user) }}
+ {{ render_field(form.user) }} {{ render_field(form.create_mask) }} {{ render_field(form.dir_mask) }}
- + {{ render_field(form.submit) }}
+ {% endif %} {% endblock %} {% block body %}
@@ -23,8 +35,25 @@ {% endif %} {{ form.hidden_tag() }} {{ render_field(form.remove_data) }} - {{ render_field(form.okay) }} + {{ render_field(form.submit) }} {% endblock %} + diff --git a/dirkules/samba/views.py b/dirkules/samba/views.py index 7ccf6f6..197f8cf 100644 --- a/dirkules/samba/views.py +++ b/dirkules/samba/views.py @@ -76,6 +76,7 @@ def generate(): @bp_samba.route('/remove', methods=['GET', 'POST']) def remove(): share_id = request.args.get('share') + show_modal = False if share_id is None: flash("Can't remove drive without id.") return redirect(url_for('.index')) @@ -84,10 +85,14 @@ def remove(): form = SambaRemovalForm(request.form) share_id = int(share_id) share = smb_man.get_share_by_id(share_id) - if request.method == 'POST' and form.validate(): - print("LOL") - return redirect(url_for('.index')) - return render_template('samba/remove.html', name=share.name, form=form) + if request.method == 'POST': + if form.validate(): + print("Alles Easy") + return redirect(url_for('.index')) + else: + show_modal = True + print(form.okay.data) + return render_template('samba/remove.html', name=share.name, form=form, show_modal=show_modal) except ValueError: flash("ValueError: id is not an int") except LookupError: From 3cf012283b309f7da57eb4b87aea416e7a617055 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Nov 2019 18:23:43 +0100 Subject: [PATCH 11/15] Improved design of samba index, rewrote flask flash, some validation --- dirkules/samba/manager.py | 15 ----- dirkules/samba/templates/samba/index.html | 28 +++++---- dirkules/samba/templates/samba/remove.html | 44 ++++---------- dirkules/samba/validation.py | 6 +- dirkules/samba/views.py | 33 ++++++----- dirkules/templates/_formhelpers.html | 10 +--- dirkules/templates/base.html | 69 +++++++++++++++++++--- 7 files changed, 116 insertions(+), 89 deletions(-) diff --git a/dirkules/samba/manager.py b/dirkules/samba/manager.py index 6b35f19..6125510 100644 --- a/dirkules/samba/manager.py +++ b/dirkules/samba/manager.py @@ -124,21 +124,6 @@ def generate_smb(): app.logger.info("Generated new samba config.") -def get_share_by_id(share_id): - """ - Returns drive object for given id - :param share_id: id of drive (primary key) - :type share_id: int - :return: Share object - :rtype: Share - """ - share = SambaShare.query.get(share_id) - if share is not None: - return share - else: - raise LookupError - - def disable_share(share): """ Disables a given share object. diff --git a/dirkules/samba/templates/samba/index.html b/dirkules/samba/templates/samba/index.html index 6d839f7..3ef715f 100644 --- a/dirkules/samba/templates/samba/index.html +++ b/dirkules/samba/templates/samba/index.html @@ -21,13 +21,13 @@ Generieren - +
- - + + @@ -37,23 +37,31 @@ - - + {% if share.recycle %} + + {% else %} + + {% endif %} + {% if share.btrfs %} + + {% else %} + + {% endif %}
Name PfadPapierkorbBtrFS OptimierungenPapierkorbBtrFS Modul Aktionen
{{ share.name }} {{ share.path }}{{ share.recycle }}{{ share.btrfs }} Aktiviert Deaktiviert Aktiviert Deaktiviert{% if share.enabled %} -
- Exkludieren +
+
{% else %} -
- Inkludieren +
+
{% endif %} -
+
diff --git a/dirkules/samba/templates/samba/remove.html b/dirkules/samba/templates/samba/remove.html index 170fd99..e580609 100644 --- a/dirkules/samba/templates/samba/remove.html +++ b/dirkules/samba/templates/samba/remove.html @@ -1,20 +1,5 @@ {% extends "base.html" %} {% block title %}Freigabe entfernen{% endblock %} -{% block head %} - {{ super() }} - {% if show_modal %} - - {% endif %} -{% endblock %} {% block body %}
@@ -35,23 +20,20 @@ {% endif %} {{ form.hidden_tag() }} {{ render_field(form.remove_data) }} - diff --git a/dirkules/samba/validation.py b/dirkules/samba/validation.py index b892c9a..6b5424f 100644 --- a/dirkules/samba/validation.py +++ b/dirkules/samba/validation.py @@ -7,12 +7,12 @@ from dirkules.wtforms_extension import ToggleBooleanField, RequiredIf class SambaConfigForm(FlaskForm): workgroup = StringField("workgroup", [validators.required(message="Bitte Feld ausfüllen!"), validators.Regexp('^[a-z]+$', message="Bitte nur Kleinbuchstaben eingeben."), - validators.Length(max=255, message="Eingabe zu lang")], + validators.Length(max=11, message="Maximal 11 Zeichen.")], render_kw={"placeholder": "Nichts..."}) server_string = StringField("server string", [validators.required(message="Bitte Feld ausfüllen!"), validators.Regexp('^[a-z]+$', message="Bitte nur Kleinbuchstaben eingeben."), - validators.Length(max=255, message="Eingabe zu lang")], + validators.Length(max=15, message="Maximal 15 Zeichen.")], render_kw={"placeholder": "Nichts..."}) submit = SubmitField("Speichern") @@ -41,4 +41,4 @@ class SambaRemovalForm(FlaskForm): remove_data = BooleanField("Alle Daten entfernen") okay = ToggleBooleanField("Sicherheitsprotokolle überbrücken.", validators=[RequiredIf("remove_data",)]) - submit = SubmitField("Pool erstellen") + submit = SubmitField("Freigabe löschen") diff --git a/dirkules/samba/views.py b/dirkules/samba/views.py index 197f8cf..e51a838 100644 --- a/dirkules/samba/views.py +++ b/dirkules/samba/views.py @@ -4,7 +4,7 @@ from dirkules.config import staticDir from flask import render_template, url_for, request, redirect, flash from dirkules.samba import bp_samba from dirkules.samba import manager as smb_man -from dirkules.samba.models import SambaShare +from dirkules.samba.models import SambaShare, SambaGlobal from dirkules.samba.validation import SambaConfigForm, SambaAddForm, SambaRemovalForm @@ -17,31 +17,34 @@ def index(): if e_share is not None: try: e_share = int(e_share) - share = smb_man.get_share_by_id(e_share) + share = SambaShare.query.get_or_404(e_share) smb_man.enable_share(share) except ValueError: - flash("ValueError: enable") + flash("ValueError: enable", category="error") except LookupError: - flash("LookupError: id not valid") + flash("LookupError: id not valid", category="error") return redirect(url_for('.index')) elif d_share is not None: try: d_share = int(d_share) - share = smb_man.get_share_by_id(d_share) + share = SambaShare.query.get_or_404(d_share) smb_man.disable_share(share) except ValueError: - flash("ValueError: disable") + flash("ValueError: disable", category="error") except LookupError: - flash("LookupError: id not valid") + flash("LookupError: id not valid", category="error") return redirect(url_for('.index')) else: - flash("Value Error: enable and disable set") + flash("Value Error: enable and disable set", category="error") return render_template('samba/index.html', shares=shares) @bp_samba.route('/config', methods=['GET', 'POST']) def config(): form = SambaConfigForm(request.form) + if SambaGlobal.query.first() is not None: + form.workgroup.data = SambaGlobal.query.get(1) + form.server_string.data = SambaGlobal.query.get(2) if request.method == 'POST' and form.validate(): smb_man.set_samba_global(form.workgroup.data, form.server_string.data) return redirect(url_for('.index')) @@ -70,6 +73,9 @@ def add(): @bp_samba.route('/generate') def generate(): smb_man.generate_smb() + if SambaGlobal.query.first() is None: + flash("Samba wurde nicht konfiguriert. Es wird der default fallback verwendet", category="warn") + flash("Konfiguration erfolgreich generiert", category="positive") return redirect(url_for('.index')) @@ -78,23 +84,20 @@ def remove(): share_id = request.args.get('share') show_modal = False if share_id is None: - flash("Can't remove drive without id.") + flash("Keine id angegeben. Möglicherweise ist der Verweis veraltet", category="error") return redirect(url_for('.index')) else: try: form = SambaRemovalForm(request.form) share_id = int(share_id) - share = smb_man.get_share_by_id(share_id) + share = SambaShare.query.get_or_404(share_id) if request.method == 'POST': if form.validate(): - print("Alles Easy") + smb_man.remove_share(share, remove_data=bool(form.remove_data.data)) return redirect(url_for('.index')) else: show_modal = True - print(form.okay.data) return render_template('samba/remove.html', name=share.name, form=form, show_modal=show_modal) except ValueError: - flash("ValueError: id is not an int") - except LookupError: - flash("LookupError: id not valid") + flash("ValueError: id is not an int", category="error") return redirect(url_for('.index')) diff --git a/dirkules/templates/_formhelpers.html b/dirkules/templates/_formhelpers.html index c0feaf0..f3f9027 100644 --- a/dirkules/templates/_formhelpers.html +++ b/dirkules/templates/_formhelpers.html @@ -46,13 +46,7 @@ {% else %}
{% endif %} - {% if field.errors %} - {% for error in field.errors %} -
- {{ error }} -
- {% endfor %} - {% endif %} + {% if field.type == 'BooleanField' %}
{% else %} @@ -110,6 +104,6 @@
{% else %} - ERROR + ERROR LOL {% endif %} {% endmacro %} \ No newline at end of file diff --git a/dirkules/templates/base.html b/dirkules/templates/base.html index 38567b1..1a170fa 100644 --- a/dirkules/templates/base.html +++ b/dirkules/templates/base.html @@ -28,8 +28,9 @@ über Dirkules
-{% with messages = get_flashed_messages() %} - {% if messages %} + +{% with errors = get_flashed_messages(category_filter=["error"]) %} + {% if errors %}
@@ -37,16 +38,72 @@ Ein oder Mehrere Fehler sind aufgetreten
    - {% for message in messages %} -
  • {{ message }}
  • - {% endfor %} + {% for msg in errors %} +
  • {{ msg }}
  • + {% endfor -%}
{% endif %} {% endwith %} + +{% with warns = get_flashed_messages(category_filter=["warn"]) %} + {% if warns %} +
+
+ +
+ Ein oder Mehrere Warnungen sind aufgetreten +
+
    + {% for msg in warns %} +
  • {{ msg }}
  • + {% endfor -%} +
+
+
+ {% endif %} +{% endwith %} + +{% with info = get_flashed_messages(category_filter=["info"]) %} + {% if info %} +
+
+ +
+ Folgende Informationen sind vorhanden +
+
    + {% for msg in info %} +
  • {{ msg }}
  • + {% endfor -%} +
+
+
+ {% endif %} +{% endwith %} + +{% with pos = get_flashed_messages(category_filter=["positive"]) %} + {% if pos %} +
+
+ +
+ Folgendes wurde erfolgreich abgeschlossen +
+
    + {% for msg in pos %} +
  • {{ msg }}
  • + {% endfor -%} +
+
+
+ {% endif %} +{% endwith %} + {% block body %} {% endblock %} + - - From d7c9931665185b83b35089aa197f5540ebe22bec Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Nov 2019 18:26:33 +0100 Subject: [PATCH 12/15] updated samba global --- dirkules/static/conf/samba_global.conf | 1 - 1 file changed, 1 deletion(-) diff --git a/dirkules/static/conf/samba_global.conf b/dirkules/static/conf/samba_global.conf index c515550..8906966 100644 --- a/dirkules/static/conf/samba_global.conf +++ b/dirkules/static/conf/samba_global.conf @@ -1,5 +1,4 @@ socket options = TCP_NODELAY -security = user encrypt passwords = yes invalid users = root usershare allow guests = no From 2751095810850cc05e7940cbfaafca2c1a924d3c Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 18 Nov 2019 19:46:57 +0100 Subject: [PATCH 13/15] small fix in samba global --- dirkules/samba/manager.py | 6 +++--- dirkules/samba/views.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dirkules/samba/manager.py b/dirkules/samba/manager.py index 6125510..01e2f0f 100644 --- a/dirkules/samba/manager.py +++ b/dirkules/samba/manager.py @@ -68,7 +68,7 @@ def set_samba_global(workgroup, name): """ SambaGlobal.query.delete() workgroup = SambaGlobal("workgroup", workgroup) - name = SambaGlobal("server string", "%h {}".format(name)) + name = SambaGlobal("server string", "{}".format(name)) db.session.add(workgroup) db.session.add(name) db.session.commit() @@ -85,8 +85,8 @@ def generate_smb(): workgroup = 'WORKGROUP' server_string = '%h dirkules' else: - workgroup = SambaGlobal.query.get(1) - server_string = SambaGlobal.query.get(2) + workgroup = SambaGlobal.query.get(1).value + server_string = "%h " + str(SambaGlobal.query.get(2).value) f = open("smb.conf.txt", "w") samba_global = open(staticDir + "/conf/samba_global.conf", "r") samba_share = open(staticDir + "/conf/samba_share.conf", "r") diff --git a/dirkules/samba/views.py b/dirkules/samba/views.py index e51a838..08181ab 100644 --- a/dirkules/samba/views.py +++ b/dirkules/samba/views.py @@ -43,8 +43,8 @@ def index(): def config(): form = SambaConfigForm(request.form) if SambaGlobal.query.first() is not None: - form.workgroup.data = SambaGlobal.query.get(1) - form.server_string.data = SambaGlobal.query.get(2) + form.workgroup.data = SambaGlobal.query.get(1).value + form.server_string.data = SambaGlobal.query.get(2).value if request.method == 'POST' and form.validate(): smb_man.set_samba_global(form.workgroup.data, form.server_string.data) return redirect(url_for('.index')) From ccb51aab8ee294ec87679fbe2fad34c9f2ad3d73 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 23 Nov 2019 18:31:38 +0100 Subject: [PATCH 14/15] fix path thing (path is now path of pool, not of share) --- dirkules/manager/driveManager.py | 2 +- dirkules/samba/manager.py | 3 ++- dirkules/templates/_formhelpers.html | 18 +++++------------- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/dirkules/manager/driveManager.py b/dirkules/manager/driveManager.py index 5adb96b..633e6bd 100644 --- a/dirkules/manager/driveManager.py +++ b/dirkules/manager/driveManager.py @@ -95,7 +95,7 @@ def pool_gen(): raid = "unbekannt" drives = "" for part in value: - drives = drives + str(Drive.query.get(part.drive_id)) + "," + drives = drives + str(Drive.query.get(part.drive_id).name) + "," drives = drives[:-1] value = value[0] missing = absent_drive(drives) diff --git a/dirkules/samba/manager.py b/dirkules/samba/manager.py index 01e2f0f..c956ed1 100644 --- a/dirkules/samba/manager.py +++ b/dirkules/samba/manager.py @@ -13,7 +13,7 @@ def get_pools(): :return: List of tuples (id, label) of pools """ pools = Pool.query.all() - choices = [(str(pool.id), pool.label) for pool in pools] + choices = [(str(pool.id), pool.label + ": " + pool.drives + " mounted on " + pool.mountpoint) for pool in pools] return choices @@ -39,6 +39,7 @@ def create_share(name, path, user, dir_mask, create_mask, writeable, btrfs, recy :return: Nothing :rtype: None """ + path = Pool.query.get(int(path)) share = SambaShare(name, path, btrfs=btrfs, recycle=recycling) user = SambaOption("valid users", user) if dir_mask is None: diff --git a/dirkules/templates/_formhelpers.html b/dirkules/templates/_formhelpers.html index f3f9027..2fe88f0 100644 --- a/dirkules/templates/_formhelpers.html +++ b/dirkules/templates/_formhelpers.html @@ -15,7 +15,7 @@ {% endif %} {{ field(**kwargs)|safe }}
- {% elif field.type == 'SelectField' or field.type =='QuerySelectField' %} + {% elif field.type == 'SelectField' %}
{% if field.errors %}
@@ -104,6 +96,6 @@
{% else %} - ERROR LOL + This field is not defined in formhelpers. {% endif %} {% endmacro %} \ No newline at end of file From 42da594162ea8c94d1810e62fe920e88d8accbf4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 27 Nov 2019 16:03:30 +0100 Subject: [PATCH 15/15] the full path is now saved in samba shares --- dirkules/samba/manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dirkules/samba/manager.py b/dirkules/samba/manager.py index c956ed1..832e8c1 100644 --- a/dirkules/samba/manager.py +++ b/dirkules/samba/manager.py @@ -39,7 +39,7 @@ def create_share(name, path, user, dir_mask, create_mask, writeable, btrfs, recy :return: Nothing :rtype: None """ - path = Pool.query.get(int(path)) + path = Pool.query.get(int(path)).mountpoint + "/{}".format(name) share = SambaShare(name, path, btrfs=btrfs, recycle=recycling) user = SambaOption("valid users", user) if dir_mask is None: