portal: refactor proxy views into module

This commit is contained in:
Iain Learmonth 2022-05-04 13:31:14 +01:00
parent ef1f3208fc
commit 33e6d58455
5 changed files with 48 additions and 36 deletions

View file

@ -11,11 +11,13 @@ from app import Origin, Proxy
from app.models.base import Group, MirrorList from app.models.base import Group, MirrorList
from app.portal.forms import NewOriginForm, EditOriginForm, LifecycleForm, \ from app.portal.forms import NewOriginForm, EditOriginForm, LifecycleForm, \
NewBridgeConfForm, EditBridgeConfForm, NewMirrorListForm NewBridgeConfForm, EditBridgeConfForm, NewMirrorListForm
from app.portal.group import group, NewGroupForm, EditGroupForm from app.portal.group import bp as group
from app.portal.proxy import bp as proxy
portal = Blueprint("portal", __name__, template_folder="templates", static_folder="static") portal = Blueprint("portal", __name__, template_folder="templates", static_folder="static")
portal.register_blueprint(group, url_prefix="/group") portal.register_blueprint(group, url_prefix="/group")
portal.register_blueprint(proxy, url_prefix="/proxy")
@portal.app_template_filter("mirror_expiry") @portal.app_template_filter("mirror_expiry")
@ -111,35 +113,6 @@ def view_origins():
new_link=url_for("portal.new_origin"), new_link=url_for("portal.new_origin"),
items=origins) items=origins)
@portal.route("/proxies")
def view_proxies():
proxies = Proxy.query.filter(Proxy.destroyed == None).order_by(desc(Proxy.updated)).all()
return render_template("list.html.j2",
section="proxy",
title="Proxies",
item="proxy",
items=proxies)
@portal.route("/proxy/block/<proxy_id>", methods=['GET', 'POST'])
def blocked_proxy(proxy_id):
proxy = Proxy.query.filter(Proxy.id == proxy_id, Proxy.destroyed == None).first()
if proxy is None:
return Response(render_template("error.html.j2",
header="404 Proxy Not Found",
message="The requested proxy could not be found."))
form = LifecycleForm()
if form.validate_on_submit():
proxy.deprecate(reason="manual")
db.session.commit()
flash("Proxy will be shortly replaced.", "success")
return redirect(url_for("portal.edit_origin", origin_id=proxy.origin.id))
return render_template("lifecycle.html.j2",
header=f"Mark proxy for {proxy.origin.domain_name} as blocked?",
message=proxy.url,
section="proxy",
form=form)
@portal.route("/search") @portal.route("/search")
def search(): def search():

View file

@ -9,7 +9,7 @@ from wtforms.validators import DataRequired
from app.extensions import db from app.extensions import db
from app.models.base import Group from app.models.base import Group
group = Blueprint("group", __name__) bp = Blueprint("group", __name__)
class NewGroupForm(FlaskForm): class NewGroupForm(FlaskForm):
@ -25,7 +25,7 @@ class EditGroupForm(FlaskForm):
submit = SubmitField('Save Changes', render_kw={"class": "btn btn-success"}) submit = SubmitField('Save Changes', render_kw={"class": "btn btn-success"})
@group.route("/list") @bp.route("/list")
def group_list(): def group_list():
groups = Group.query.order_by(Group.group_name).all() groups = Group.query.order_by(Group.group_name).all()
return render_template("list.html.j2", return render_template("list.html.j2",
@ -36,7 +36,7 @@ def group_list():
new_link=url_for("portal.group.group_new")) new_link=url_for("portal.group.group_new"))
@group.route("/new", methods=['GET', 'POST']) @bp.route("/new", methods=['GET', 'POST'])
def group_new(): def group_new():
form = NewGroupForm() form = NewGroupForm()
if form.validate_on_submit(): if form.validate_on_submit():
@ -58,7 +58,7 @@ def group_new():
return render_template("new.html.j2", section="group", form=form) return render_template("new.html.j2", section="group", form=form)
@group.route('/edit/<group_id>', methods=['GET', 'POST']) @bp.route('/edit/<group_id>', methods=['GET', 'POST'])
def group_edit(group_id): def group_edit(group_id):
group = Group.query.filter(Group.id == group_id).first() group = Group.query.filter(Group.id == group_id).first()
if group is None: if group is None:

39
app/portal/proxy.py Normal file
View file

@ -0,0 +1,39 @@
from flask import render_template, Response, flash, redirect, url_for, Blueprint
from sqlalchemy import desc
from app.extensions import db
from app.models.mirrors import Proxy
from app.portal.forms import LifecycleForm
bp = Blueprint("proxy", __name__)
@bp.route("/list")
def proxy_list():
proxies = Proxy.query.filter(Proxy.destroyed == None).order_by(desc(Proxy.added)).all()
return render_template("list.html.j2",
section="proxy",
title="Proxies",
item="proxy",
items=proxies)
@bp.route("/block/<proxy_id>", methods=['GET', 'POST'])
def proxy_block(proxy_id):
proxy = Proxy.query.filter(Proxy.id == proxy_id, Proxy.destroyed == None).first()
if proxy is None:
return Response(render_template("error.html.j2",
header="404 Proxy Not Found",
message="The requested proxy could not be found. It may have already been "
"destroyed."))
form = LifecycleForm()
if form.validate_on_submit():
proxy.deprecate(reason="manual")
db.session.commit()
flash("Proxy will be shortly replaced.", "success")
return redirect(url_for("portal.edit_origin", origin_id=proxy.origin.id))
return render_template("lifecycle.html.j2",
header=f"Mark proxy for {proxy.origin.domain_name} as blocked?",
message=proxy.url,
section="proxy",
form=form)

View file

@ -107,7 +107,7 @@
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link{% if section == "proxy" %} active{% endif %}" <a class="nav-link{% if section == "proxy" %} active{% endif %}"
href="{{ url_for("portal.view_proxies") }}"> href="{{ url_for("portal.proxy.proxy_list") }}">
Proxies Proxies
</a> </a>
</li> </li>

View file

@ -146,7 +146,7 @@
<a href="#" class="disabled btn btn-sm btn-outline-dark">Expiring <a href="#" class="disabled btn btn-sm btn-outline-dark">Expiring
in {{ proxy.deprecated | mirror_expiry }}</a> in {{ proxy.deprecated | mirror_expiry }}</a>
{% else %} {% else %}
<a href="{{ url_for("portal.blocked_proxy", proxy_id=proxy.id) }}" <a href="{{ url_for("portal.proxy.proxy_block", proxy_id=proxy.id) }}"
class="btn btn-warning btn-sm">Mark blocked</a> class="btn btn-warning btn-sm">Mark blocked</a>
{% endif %} {% endif %}
</td> </td>