79 lines
3 KiB
Python
79 lines
3 KiB
Python
|
from datetime import datetime
|
||
|
|
||
|
from flask import render_template, url_for, flash, redirect, Response, Blueprint
|
||
|
from flask.typing import ResponseReturnValue
|
||
|
from flask_wtf import FlaskForm
|
||
|
import sqlalchemy
|
||
|
from wtforms import StringField, SubmitField
|
||
|
from wtforms.validators import DataRequired
|
||
|
|
||
|
from app.extensions import db
|
||
|
from app.models.base import Pool
|
||
|
|
||
|
bp = Blueprint("pool", __name__)
|
||
|
|
||
|
|
||
|
class NewPoolForm(FlaskForm): # type: ignore
|
||
|
group_name = StringField("Short Name", validators=[DataRequired()])
|
||
|
description = StringField("Description", validators=[DataRequired()])
|
||
|
submit = SubmitField('Save Changes', render_kw={"class": "btn btn-success"})
|
||
|
|
||
|
|
||
|
class EditPoolForm(FlaskForm): # type: ignore
|
||
|
description = StringField("Description", validators=[DataRequired()])
|
||
|
submit = SubmitField('Save Changes', render_kw={"class": "btn btn-success"})
|
||
|
|
||
|
|
||
|
@bp.route("/list")
|
||
|
def pool_list() -> ResponseReturnValue:
|
||
|
pools = Pool.query.order_by(Pool.pool_name).all()
|
||
|
return render_template("list.html.j2",
|
||
|
section="pool",
|
||
|
title="Resource Pools",
|
||
|
item="pool",
|
||
|
items=pools,
|
||
|
new_link=url_for("portal.pool.pool_new"))
|
||
|
|
||
|
|
||
|
@bp.route("/new", methods=['GET', 'POST'])
|
||
|
def pool_new() -> ResponseReturnValue:
|
||
|
form = NewPoolForm()
|
||
|
if form.validate_on_submit():
|
||
|
pool = Pool()
|
||
|
pool.pool_name = form.group_name.data
|
||
|
pool.description = form.description.data
|
||
|
pool.created = datetime.utcnow()
|
||
|
pool.updated = datetime.utcnow()
|
||
|
try:
|
||
|
db.session.add(pool)
|
||
|
db.session.commit()
|
||
|
flash(f"Created new pool {pool.pool_name}.", "success")
|
||
|
return redirect(url_for("portal.pool.pool_edit", pool_id=pool.id))
|
||
|
except sqlalchemy.exc.SQLAlchemyError:
|
||
|
flash("Failed to create new pool.", "danger")
|
||
|
return redirect(url_for("portal.pool.pool_list"))
|
||
|
return render_template("new.html.j2", section="pool", form=form)
|
||
|
|
||
|
|
||
|
@bp.route('/edit/<pool_id>', methods=['GET', 'POST'])
|
||
|
def pool_edit(pool_id: int) -> ResponseReturnValue:
|
||
|
pool = Pool.query.filter(Pool.id == pool_id).first()
|
||
|
if pool is None:
|
||
|
return Response(render_template("error.html.j2",
|
||
|
section="pool",
|
||
|
header="404 Pool Not Found",
|
||
|
message="The requested pool could not be found."),
|
||
|
status=404)
|
||
|
form = EditPoolForm(description=pool.description)
|
||
|
if form.validate_on_submit():
|
||
|
pool.description = form.description.data
|
||
|
pool.updated = datetime.utcnow()
|
||
|
try:
|
||
|
db.session.commit()
|
||
|
flash("Saved changes to pool.", "success")
|
||
|
except sqlalchemy.exc.SQLAlchemyError:
|
||
|
flash("An error occurred saving the changes to the pool.", "danger")
|
||
|
return render_template("pool.html.j2",
|
||
|
section="pool",
|
||
|
pool=pool, form=form)
|