2022-05-08 17:20:04 +01:00
|
|
|
from datetime import datetime
|
2022-05-16 11:44:03 +01:00
|
|
|
from typing import Optional
|
2022-05-08 17:20:04 +01:00
|
|
|
|
2022-06-22 17:13:58 +01:00
|
|
|
from flask import render_template, flash, Response, Blueprint, current_app
|
2022-05-16 11:44:03 +01:00
|
|
|
from flask.typing import ResponseReturnValue
|
2022-05-08 17:20:04 +01:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from sqlalchemy import exc
|
|
|
|
from wtforms import SubmitField, BooleanField
|
|
|
|
|
|
|
|
from app.extensions import db
|
|
|
|
from app.models.automation import Automation
|
|
|
|
from app.portal.util import view_lifecycle, response_404
|
|
|
|
|
|
|
|
bp = Blueprint("automation", __name__)
|
|
|
|
|
|
|
|
|
2022-08-25 20:49:20 +01:00
|
|
|
_SECTION_TEMPLATE_VARS = {
|
|
|
|
"section": "automation",
|
|
|
|
"help_url": "https://bypass.censorship.guide/user/automation.html"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
class EditAutomationForm(FlaskForm): # type: ignore
|
2022-05-08 17:20:04 +01:00
|
|
|
enabled = BooleanField('Enabled')
|
|
|
|
submit = SubmitField('Save Changes')
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/list")
|
2022-05-16 11:44:03 +01:00
|
|
|
def automation_list() -> ResponseReturnValue:
|
2022-05-08 17:20:04 +01:00
|
|
|
automations = Automation.query.filter(
|
2022-05-16 13:29:48 +01:00
|
|
|
Automation.destroyed.is_(None)).order_by(Automation.description).all()
|
2022-06-22 17:13:58 +01:00
|
|
|
automations = list(filter(
|
|
|
|
lambda a: a.short_name not in current_app.config.get('HIDDEN_AUTOMATIONS', []),
|
|
|
|
automations
|
|
|
|
))
|
2022-05-08 17:20:04 +01:00
|
|
|
return render_template("list.html.j2",
|
|
|
|
title="Automation Jobs",
|
|
|
|
item="automation",
|
2022-08-25 20:49:20 +01:00
|
|
|
items=automations,
|
|
|
|
**_SECTION_TEMPLATE_VARS)
|
2022-05-08 17:20:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route('/edit/<automation_id>', methods=['GET', 'POST'])
|
2022-05-16 11:44:03 +01:00
|
|
|
def automation_edit(automation_id: int) -> ResponseReturnValue:
|
|
|
|
automation: Optional[Automation] = Automation.query.filter(Automation.id == automation_id).first()
|
2022-05-08 17:20:04 +01:00
|
|
|
if automation is None:
|
|
|
|
return Response(render_template("error.html.j2",
|
|
|
|
header="404 Automation Job Not Found",
|
2022-08-25 20:49:20 +01:00
|
|
|
message="The requested automation job could not be found.",
|
|
|
|
**_SECTION_TEMPLATE_VARS),
|
2022-05-08 17:20:04 +01:00
|
|
|
status=404)
|
|
|
|
form = EditAutomationForm(enabled=automation.enabled)
|
|
|
|
if form.validate_on_submit():
|
|
|
|
automation.enabled = form.enabled.data
|
|
|
|
automation.updated = datetime.utcnow()
|
|
|
|
try:
|
|
|
|
db.session.commit()
|
|
|
|
flash("Saved changes to bridge configuration.", "success")
|
|
|
|
except exc.SQLAlchemyError:
|
|
|
|
flash("An error occurred saving the changes to the bridge configuration.", "danger")
|
|
|
|
return render_template("automation.html.j2",
|
2022-08-25 20:49:20 +01:00
|
|
|
automation=automation,
|
|
|
|
form=form,
|
|
|
|
**_SECTION_TEMPLATE_VARS)
|
2022-05-08 17:20:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/kick/<automation_id>", methods=['GET', 'POST'])
|
2022-05-16 11:44:03 +01:00
|
|
|
def automation_kick(automation_id: int) -> ResponseReturnValue:
|
2022-05-08 17:20:04 +01:00
|
|
|
automation = Automation.query.filter(
|
|
|
|
Automation.id == automation_id,
|
2022-05-16 13:29:48 +01:00
|
|
|
Automation.destroyed.is_(None)).first()
|
2022-05-08 17:20:04 +01:00
|
|
|
if automation is None:
|
|
|
|
return response_404("The requested bridge configuration could not be found.")
|
|
|
|
return view_lifecycle(
|
2022-05-16 13:29:48 +01:00
|
|
|
header="Kick automation timer?",
|
2022-05-08 17:20:04 +01:00
|
|
|
message=automation.description,
|
2022-09-26 13:40:59 +01:00
|
|
|
section="automation",
|
2022-05-08 17:20:04 +01:00
|
|
|
success_view="portal.automation.automation_list",
|
|
|
|
success_message="This automation job will next run within 1 minute.",
|
|
|
|
resource=automation,
|
2022-08-30 14:55:19 +01:00
|
|
|
action="kick"
|
2022-05-08 17:20:04 +01:00
|
|
|
)
|