2024-12-06 16:08:48 +00:00
|
|
|
from datetime import datetime, timezone
|
2022-05-04 13:17:01 +01:00
|
|
|
|
2024-12-06 16:08:48 +00:00
|
|
|
import sqlalchemy
|
2024-12-06 18:15:47 +00:00
|
|
|
from flask import Blueprint, Response, flash, redirect, render_template, url_for
|
2022-05-16 11:44:03 +01:00
|
|
|
from flask.typing import ResponseReturnValue
|
2022-05-04 13:17:01 +01:00
|
|
|
from flask_wtf import FlaskForm
|
2024-12-06 16:08:48 +00:00
|
|
|
from wtforms import BooleanField, StringField, SubmitField
|
2022-05-04 13:17:01 +01:00
|
|
|
from wtforms.validators import DataRequired
|
|
|
|
|
|
|
|
from app.extensions import db
|
|
|
|
from app.models.base import Group
|
|
|
|
|
2022-05-04 13:31:14 +01:00
|
|
|
bp = Blueprint("group", __name__)
|
2022-05-04 13:17:01 +01:00
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class NewGroupForm(FlaskForm): # type: ignore
|
2022-05-04 13:17:01 +01:00
|
|
|
group_name = StringField("Short Name", validators=[DataRequired()])
|
|
|
|
description = StringField("Description", validators=[DataRequired()])
|
|
|
|
eotk = BooleanField("Deploy EOTK instances?")
|
2024-12-06 18:15:47 +00:00
|
|
|
submit = SubmitField("Save Changes", render_kw={"class": "btn btn-success"})
|
2022-05-04 13:17:01 +01:00
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class EditGroupForm(FlaskForm): # type: ignore
|
2024-12-06 18:15:47 +00:00
|
|
|
description = StringField("Description", validators=[DataRequired()])
|
2022-05-04 13:17:01 +01:00
|
|
|
eotk = BooleanField("Deploy EOTK instances?")
|
2024-12-06 18:15:47 +00:00
|
|
|
submit = SubmitField("Save Changes", render_kw={"class": "btn btn-success"})
|
2022-05-04 13:17:01 +01:00
|
|
|
|
|
|
|
|
2022-05-04 13:31:14 +01:00
|
|
|
@bp.route("/list")
|
2022-05-16 11:44:03 +01:00
|
|
|
def group_list() -> ResponseReturnValue:
|
2022-05-04 13:17:01 +01:00
|
|
|
groups = Group.query.order_by(Group.group_name).all()
|
2024-12-06 18:15:47 +00:00
|
|
|
return render_template(
|
|
|
|
"list.html.j2",
|
|
|
|
section="group",
|
|
|
|
title="Groups",
|
|
|
|
item="group",
|
|
|
|
items=groups,
|
|
|
|
new_link=url_for("portal.group.group_new"),
|
|
|
|
)
|
2022-05-04 13:17:01 +01:00
|
|
|
|
|
|
|
|
2024-12-06 18:15:47 +00:00
|
|
|
@bp.route("/new", methods=["GET", "POST"])
|
2022-05-16 11:44:03 +01:00
|
|
|
def group_new() -> ResponseReturnValue:
|
2022-05-04 13:17:01 +01:00
|
|
|
form = NewGroupForm()
|
|
|
|
if form.validate_on_submit():
|
|
|
|
group = Group()
|
|
|
|
group.group_name = form.group_name.data
|
|
|
|
group.description = form.description.data
|
|
|
|
group.eotk = form.eotk.data
|
2024-12-06 14:48:07 +00:00
|
|
|
group.added = datetime.now(tz=timezone.utc)
|
|
|
|
group.updated = datetime.now(tz=timezone.utc)
|
2022-05-04 13:17:01 +01:00
|
|
|
try:
|
|
|
|
db.session.add(group)
|
|
|
|
db.session.commit()
|
|
|
|
flash(f"Created new group {group.group_name}.", "success")
|
|
|
|
return redirect(url_for("portal.group.group_edit", group_id=group.id))
|
2022-06-23 11:38:27 +01:00
|
|
|
except sqlalchemy.exc.SQLAlchemyError:
|
2022-05-04 13:17:01 +01:00
|
|
|
flash("Failed to create new group.", "danger")
|
|
|
|
return redirect(url_for("portal.group.group_list"))
|
|
|
|
return render_template("new.html.j2", section="group", form=form)
|
|
|
|
|
|
|
|
|
2024-12-06 18:15:47 +00:00
|
|
|
@bp.route("/edit/<group_id>", methods=["GET", "POST"])
|
2022-05-16 11:44:03 +01:00
|
|
|
def group_edit(group_id: int) -> ResponseReturnValue:
|
2022-05-04 13:17:01 +01:00
|
|
|
group = Group.query.filter(Group.id == group_id).first()
|
|
|
|
if group is None:
|
2024-12-06 18:15:47 +00:00
|
|
|
return Response(
|
|
|
|
render_template(
|
|
|
|
"error.html.j2",
|
|
|
|
section="group",
|
|
|
|
header="404 Group Not Found",
|
|
|
|
message="The requested group could not be found.",
|
|
|
|
),
|
|
|
|
status=404,
|
|
|
|
)
|
|
|
|
form = EditGroupForm(description=group.description, eotk=group.eotk)
|
2022-05-04 13:17:01 +01:00
|
|
|
if form.validate_on_submit():
|
|
|
|
group.description = form.description.data
|
|
|
|
group.eotk = form.eotk.data
|
2024-12-06 16:08:48 +00:00
|
|
|
group.updated = datetime.now(tz=timezone.utc)
|
2022-05-04 13:17:01 +01:00
|
|
|
try:
|
|
|
|
db.session.commit()
|
|
|
|
flash("Saved changes to group.", "success")
|
2022-06-23 11:38:27 +01:00
|
|
|
except sqlalchemy.exc.SQLAlchemyError:
|
2022-05-04 13:17:01 +01:00
|
|
|
flash("An error occurred saving the changes to the group.", "danger")
|
2024-12-06 18:15:47 +00:00
|
|
|
return render_template("group.html.j2", section="group", group=group, form=form)
|