Benchmarks crud

This commit is contained in:
2023-06-08 21:54:08 +02:00
parent bc57f6f747
commit a3d182d54e
6 changed files with 158 additions and 10 deletions

View File

@@ -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/<int:benchmark_id>", 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/<int:benchmark_id>", 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"
)

View File

@@ -0,0 +1,32 @@
<table id="report-table"
class="table table-striped table-hover table-bordered bg-light"
data-toggle="table"
data-sticky-header="true"
data-sticky-header-offset-y="65"
data-sortable="true">
<thead>
<tr class="bg-primary text-white">
<th class="text-center">Benchmark</th>
<th class="text-center">Description</th>
<th class="text-center">Folder</th>
<th class="text-center">Date Created</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
{% for benchmark in benchmarks %}
<tr>
<td class="text-left">{{ benchmark.name }}</td>
<td class="text-left">{{ benchmark.description }}</td>
<td>{{ benchmark.folder }}</td>
<td class="text-center">{{ benchmark.date_created.strftime("%d-%m-%Y, %T") }}</td>
<td>
<a href="{{ url_for("admin.benchmark_edit", benchmark_id=benchmark.id) }}"
class="btn btn-primary"><span><i class="mdi mdi-test-tube"></i></span></a>
<a href="{{ url_for("admin.benchmark_delete", benchmark_id=benchmark.id) }}"
class="btn btn-danger"><i class="mdi mdi-test-tube-off"></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>

View File

@@ -0,0 +1,12 @@
{% extends "base.html" %}
{% from 'bootstrap5/form.html' import render_form %}
{% block content %}
<div class="alert alert-{{ alert_type }} col-md-4" role="alert">
<h4 class="alert-heading">{{ title }}</h4>
<button class="btn btn-primary"
onclick="window.location.href='{{ url_for("admin.benchmarks") }}'">Back</button>
<div class="row">
<div>{{ render_form(form) }}</div>
</div>
</div>
{% endblock %}

View File

@@ -0,0 +1,20 @@
{% extends "report_tables.html" %}
{% block content %}
<div class="container-fluid">
<div class="alert alert-primary" role="alert">
<div class="navbar">
<div class="float-left">
<h2>
Benchmarks <b>Management</b>
</h2>
</div>
<div class="float-end">
<a href="{{ url_for("admin.benchmark_new") }}" class="btn btn-primary"><span><i class="mdi mdi-plus-circle"></i> Add New Benchmark</span></a>
</div>
</div>
<hr>
</div>
<div class="float-left">{% include "_table_benchmarks.html" %}</div>
</div>
</div>
{% endblock content %}

View File

@@ -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())

View File

@@ -37,7 +37,10 @@
</a>
<ul class="dropdown-menu dropdown-menu-dark"
aria-labelledby="navbarDarkDropdownMenuLink">
{% if current_user.admin %}{{ render_nav_item('admin.users', 'Users') |safe }}{% endif %}
{% if current_user.admin %}
{{ render_nav_item('admin.users', 'Users') |safe }}
{{ render_nav_item('admin.benchmarks', 'Benchmarks') |safe }}
{% endif %}
<a class="nav-item nav-link"
href="{{ url_for("admin.password", user_id=current_user.id) }}">Password</a>
{{ render_nav_item('main.logout', 'Logout') |safe }}