2024-12-06 18:02:59 +00:00
|
|
|
from flask import Response, flash, redirect, render_template, url_for
|
2022-05-16 11:44:03 +01:00
|
|
|
from flask.typing import ResponseReturnValue
|
2022-05-11 15:47:39 +01:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import SubmitField
|
2022-05-04 13:46:52 +01:00
|
|
|
|
2022-05-17 08:28:37 +01:00
|
|
|
from app.extensions import db
|
2022-05-04 13:46:52 +01:00
|
|
|
from app.models import AbstractResource
|
2022-05-14 10:18:00 +01:00
|
|
|
from app.models.activity import Activity
|
2022-05-04 13:46:52 +01:00
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
def response_404(message: str) -> ResponseReturnValue:
|
2022-05-04 13:46:52 +01:00
|
|
|
return Response(render_template("error.html.j2",
|
|
|
|
header="404 Not Found",
|
|
|
|
message=message))
|
|
|
|
|
|
|
|
|
|
|
|
def view_lifecycle(*,
|
|
|
|
header: str,
|
|
|
|
message: str,
|
|
|
|
success_message: str,
|
|
|
|
success_view: str,
|
|
|
|
section: str,
|
|
|
|
resource: AbstractResource,
|
2022-05-16 11:44:03 +01:00
|
|
|
action: str) -> ResponseReturnValue:
|
2022-05-04 13:46:52 +01:00
|
|
|
form = LifecycleForm()
|
2022-05-08 17:20:04 +01:00
|
|
|
if action == "destroy":
|
|
|
|
form.submit.render_kw = {"class": "btn btn-danger"}
|
|
|
|
elif action == "deprecate":
|
|
|
|
form.submit.render_kw = {"class": "btn btn-warning"}
|
|
|
|
elif action == "kick":
|
|
|
|
form.submit.render_kw = {"class": "btn btn-success"}
|
2022-05-04 13:46:52 +01:00
|
|
|
if form.validate_on_submit():
|
|
|
|
if action == "destroy":
|
|
|
|
resource.destroy()
|
|
|
|
elif action == "deprecate":
|
|
|
|
resource.deprecate(reason="manual")
|
2022-05-08 17:20:04 +01:00
|
|
|
elif action == "kick":
|
|
|
|
resource.kick()
|
2022-05-04 13:46:52 +01:00
|
|
|
else:
|
|
|
|
flash("Unknown action")
|
|
|
|
return redirect(url_for("portal.portal_home"))
|
2022-05-14 10:18:00 +01:00
|
|
|
activity = Activity(
|
|
|
|
activity_type="lifecycle",
|
|
|
|
text=f"Portal action: {message}. {success_message}"
|
|
|
|
)
|
|
|
|
db.session.add(activity)
|
2022-05-04 13:46:52 +01:00
|
|
|
db.session.commit()
|
2022-05-14 10:18:00 +01:00
|
|
|
activity.notify()
|
2022-05-04 13:46:52 +01:00
|
|
|
flash(success_message, "success")
|
|
|
|
return redirect(url_for(success_view))
|
|
|
|
return render_template("lifecycle.html.j2",
|
|
|
|
header=header,
|
|
|
|
message=message,
|
|
|
|
section=section,
|
2022-05-04 14:03:04 +01:00
|
|
|
form=form)
|
2022-05-11 15:47:39 +01:00
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class LifecycleForm(FlaskForm): # type: ignore
|
2022-05-11 15:47:39 +01:00
|
|
|
submit = SubmitField('Confirm')
|