diff --git a/app/admin/main_admin.py b/app/admin/main_admin.py index 83c53fe..8a63fed 100644 --- a/app/admin/main_admin.py +++ b/app/admin/main_admin.py @@ -7,7 +7,7 @@ from flask import ( current_app, ) from flask_login import current_user, login_required -from .forms import UserForm, UpdatePasswordForm +from .forms import UserForm, UpdatePasswordForm, BenchmarkForm from ..models import User, Benchmark, db admin = Blueprint("admin", __name__, template_folder="templates") @@ -55,17 +55,12 @@ def user_edit(user_id): def user_delete(user_id): if user_id != current_user.id and not current_user.admin: flash("You are not an admin.", "danger") - return redirect(url_for(INDEX)) + return redirect(url_for(current_app.config["INDEX"])) user = User.query.filter_by(id=user_id).first() form = UserForm(obj=user) del form.password del form.password2 - for field in form: - if field.type != "SubmitField" and field.type != "CSRFTokenField": - if field.type == "SelectField" or field.type == "BooleanField": - field.render_kw = {"disabled": True} - else: - field.render_kw = {"readonly": True} + disable_fields(form) form.benchmark_id.choices = [ (b.id, b.name) for b in Benchmark.query.order_by("name") @@ -90,7 +85,7 @@ def user_delete(user_id): def user_new(): if not current_user.admin: flash("You are not an admin.", "danger") - return redirect(url_for(INDEX)) + return redirect(url_for(current_app.config["INDEX"])) form = UserForm() user = User() form.user_id = None @@ -137,3 +132,88 @@ def password(user_id, back): return render_template( "password.html", form=form, back=destination, user_name=user.username ) + + +@admin.route("/benchmarks") +@login_required +def benchmarks(): + if not current_user.admin: + flash("You are not an admin.", "danger") + return redirect(url_for(current_app.config["INDEX"])) + benchmarks = Benchmark.query.all() + return render_template("benchmarks.html", benchmarks=benchmarks) + + +@admin.route("/benchmark_edit/", methods=["GET", "POST"]) +@login_required +def benchmark_edit(benchmark_id): + if not current_user.admin: + flash("You are not an admin.", "danger") + return redirect(url_for(current_app.config["INDEX"])) + form = BenchmarkForm( + obj=Benchmark.query.filter_by(id=benchmark_id).first() + ) + form.submit.label.text = "Edit Benchmark" + if form.validate_on_submit(): + form.populate_obj(Benchmark.query.filter_by(id=benchmark_id).first()) + db.session.commit() + flash("Benchmark edited successfully.") + return redirect(url_for("admin.benchmarks")) + return render_template( + "benchmark.html", + form=form, + alert_type="primary", + title="Edit Benchmark", + ) + + +def disable_fields(form): + for field in form: + if field.type != "SubmitField" and field.type != "CSRFTokenField": + if field.type == "SelectField" or field.type == "BooleanField": + field.render_kw = {"disabled": True} + else: + field.render_kw = {"readonly": True} + + +@admin.route("/benchmark_delete/", methods=["GET", "POST"]) +@login_required +def benchmark_delete(benchmark_id): + if not current_user.admin: + flash("You are not an admin.", "danger") + return redirect(url_for(current_app.config["INDEX"])) + benchmark = Benchmark.query.filter_by(id=benchmark_id).first() + form = BenchmarkForm(obj=benchmark) + disable_fields(form) + form.submit.label.text = "Delete Benchmark" + if form.validate_on_submit(): + flash("Benchmark deleted successfully.") + db.session.delete(benchmark) + db.session.commit() + return redirect(url_for("admin.benchmarks")) + return render_template( + "benchmark.html", + form=form, + alert_type="danger", + title="Delete Benchmark", + ) + + +@admin.route("/benchmark_new", methods=["GET", "POST"]) +@login_required +def benchmark_new(): + if not current_user.admin: + flash("You are not an admin.", "danger") + return redirect(url_for(current_app.config["INDEX"])) + form = BenchmarkForm() + benchmark = Benchmark() + form.submit.label.text = "New Benchmark" + if form.validate_on_submit(): + form.populate_obj(benchmark) + db.session.add(benchmark) + db.session.commit() + flash("Benchmark created successfully.") + return redirect(url_for("admin.benchmarks")) + return render_template( + "benchmark.html", form=form, alert_type="info", title="New Benchmark" + ) diff --git a/app/admin/templates/_table_benchmarks.html b/app/admin/templates/_table_benchmarks.html new file mode 100644 index 0000000..665e170 --- /dev/null +++ b/app/admin/templates/_table_benchmarks.html @@ -0,0 +1,32 @@ + + + + + + + + + + + + {% for benchmark in benchmarks %} + + + + + + + + {% endfor %} + +
BenchmarkDescriptionFolderDate CreatedActions
{{ benchmark.name }}{{ benchmark.description }}{{ benchmark.folder }}{{ benchmark.date_created.strftime("%d-%m-%Y, %T") }} + + +
diff --git a/app/admin/templates/benchmark.html b/app/admin/templates/benchmark.html new file mode 100644 index 0000000..ad6e7b5 --- /dev/null +++ b/app/admin/templates/benchmark.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} +{% from 'bootstrap5/form.html' import render_form %} +{% block content %} + +{% endblock %} diff --git a/app/admin/templates/benchmarks.html b/app/admin/templates/benchmarks.html new file mode 100644 index 0000000..3ea6881 --- /dev/null +++ b/app/admin/templates/benchmarks.html @@ -0,0 +1,20 @@ +{% extends "report_tables.html" %} +{% block content %} +
+ +
{% include "_table_benchmarks.html" %}
+
+ +{% endblock content %} diff --git a/app/models.py b/app/models.py index 0cde669..963d6dc 100644 --- a/app/models.py +++ b/app/models.py @@ -40,3 +40,4 @@ class Benchmark(db.Model): name = db.Column(db.String(64), index=True, unique=True) description = db.Column(db.String(120), index=False, unique=False) folder = db.Column(db.String(128), index=False, unique=True) + date_created = db.Column(db.DateTime, default=db.func.now()) diff --git a/app/templates/_nav.html b/app/templates/_nav.html index c4b13df..e42d105 100644 --- a/app/templates/_nav.html +++ b/app/templates/_nav.html @@ -37,7 +37,10 @@