mirror of
https://github.com/Doctorado-ML/beflask.git
synced 2025-08-16 07:35:51 +00:00
Benchmarks crud
This commit is contained in:
@@ -7,7 +7,7 @@ from flask import (
|
|||||||
current_app,
|
current_app,
|
||||||
)
|
)
|
||||||
from flask_login import current_user, login_required
|
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
|
from ..models import User, Benchmark, db
|
||||||
|
|
||||||
admin = Blueprint("admin", __name__, template_folder="templates")
|
admin = Blueprint("admin", __name__, template_folder="templates")
|
||||||
@@ -55,17 +55,12 @@ def user_edit(user_id):
|
|||||||
def user_delete(user_id):
|
def user_delete(user_id):
|
||||||
if user_id != current_user.id and not current_user.admin:
|
if user_id != current_user.id and not current_user.admin:
|
||||||
flash("You are not an admin.", "danger")
|
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()
|
user = User.query.filter_by(id=user_id).first()
|
||||||
form = UserForm(obj=user)
|
form = UserForm(obj=user)
|
||||||
del form.password
|
del form.password
|
||||||
del form.password2
|
del form.password2
|
||||||
for field in form:
|
disable_fields(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}
|
|
||||||
|
|
||||||
form.benchmark_id.choices = [
|
form.benchmark_id.choices = [
|
||||||
(b.id, b.name) for b in Benchmark.query.order_by("name")
|
(b.id, b.name) for b in Benchmark.query.order_by("name")
|
||||||
@@ -90,7 +85,7 @@ def user_delete(user_id):
|
|||||||
def user_new():
|
def user_new():
|
||||||
if not current_user.admin:
|
if not current_user.admin:
|
||||||
flash("You are not an admin.", "danger")
|
flash("You are not an admin.", "danger")
|
||||||
return redirect(url_for(INDEX))
|
return redirect(url_for(current_app.config["INDEX"]))
|
||||||
form = UserForm()
|
form = UserForm()
|
||||||
user = User()
|
user = User()
|
||||||
form.user_id = None
|
form.user_id = None
|
||||||
@@ -137,3 +132,88 @@ def password(user_id, back):
|
|||||||
return render_template(
|
return render_template(
|
||||||
"password.html", form=form, back=destination, user_name=user.username
|
"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"
|
||||||
|
)
|
||||||
|
32
app/admin/templates/_table_benchmarks.html
Normal file
32
app/admin/templates/_table_benchmarks.html
Normal 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>
|
12
app/admin/templates/benchmark.html
Normal file
12
app/admin/templates/benchmark.html
Normal 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 %}
|
20
app/admin/templates/benchmarks.html
Normal file
20
app/admin/templates/benchmarks.html
Normal 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 %}
|
@@ -40,3 +40,4 @@ class Benchmark(db.Model):
|
|||||||
name = db.Column(db.String(64), index=True, unique=True)
|
name = db.Column(db.String(64), index=True, unique=True)
|
||||||
description = db.Column(db.String(120), index=False, unique=False)
|
description = db.Column(db.String(120), index=False, unique=False)
|
||||||
folder = db.Column(db.String(128), index=False, unique=True)
|
folder = db.Column(db.String(128), index=False, unique=True)
|
||||||
|
date_created = db.Column(db.DateTime, default=db.func.now())
|
||||||
|
@@ -37,7 +37,10 @@
|
|||||||
</a>
|
</a>
|
||||||
<ul class="dropdown-menu dropdown-menu-dark"
|
<ul class="dropdown-menu dropdown-menu-dark"
|
||||||
aria-labelledby="navbarDarkDropdownMenuLink">
|
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"
|
<a class="nav-item nav-link"
|
||||||
href="{{ url_for("admin.password", user_id=current_user.id) }}">Password</a>
|
href="{{ url_for("admin.password", user_id=current_user.id) }}">Password</a>
|
||||||
{{ render_nav_item('main.logout', 'Logout') |safe }}
|
{{ render_nav_item('main.logout', 'Logout') |safe }}
|
||||||
|
Reference in New Issue
Block a user