92 lines
3.4 KiB
Python
92 lines
3.4 KiB
Python
|
from datetime import datetime
|
||
|
|
||
|
from flask import render_template, url_for, flash, redirect, Blueprint
|
||
|
from flask_wtf import FlaskForm
|
||
|
from sqlalchemy import exc
|
||
|
from wtforms import SelectField, StringField, SubmitField
|
||
|
from wtforms.validators import DataRequired
|
||
|
|
||
|
from app import db
|
||
|
from app.models.base import MirrorList
|
||
|
from app.portal.util import response_404, view_lifecycle
|
||
|
|
||
|
bp = Blueprint("list", __name__)
|
||
|
|
||
|
|
||
|
@bp.route('/list')
|
||
|
def list_list():
|
||
|
lists = MirrorList.query.filter(MirrorList.destroyed == None).all()
|
||
|
return render_template("list.html.j2",
|
||
|
section="list",
|
||
|
title="Mirror Lists",
|
||
|
item="mirror list",
|
||
|
new_link=url_for("portal.list.list_new"),
|
||
|
items=lists)
|
||
|
|
||
|
|
||
|
@bp.route("/destroy/<list_id>", methods=['GET', 'POST'])
|
||
|
def list_destroy(list_id: int):
|
||
|
list_ = MirrorList.query.filter(MirrorList.id == list_id, MirrorList.destroyed == None).first()
|
||
|
if list_ is None:
|
||
|
return response_404("The requested bridge configuration could not be found.")
|
||
|
return view_lifecycle(
|
||
|
header=f"Destroy mirror list?",
|
||
|
message=list_.description,
|
||
|
success_view="portal.list.list_list",
|
||
|
success_message="This list will no longer be updated and may be deleted depending on the provider.",
|
||
|
section="list",
|
||
|
resource=list_,
|
||
|
action="destroy"
|
||
|
)
|
||
|
|
||
|
|
||
|
@bp.route("/new", methods=['GET', 'POST'])
|
||
|
@bp.route("/new/<group_id>", methods=['GET', 'POST'])
|
||
|
def list_new(group_id=None):
|
||
|
form = NewMirrorListForm()
|
||
|
form.provider.choices = [
|
||
|
("github", "GitHub"),
|
||
|
("gitlab", "GitLab"),
|
||
|
("s3", "AWS S3"),
|
||
|
]
|
||
|
form.format.choices = [
|
||
|
("bc2", "Bypass Censorship v2"),
|
||
|
("bc3", "Bypass Censorship v3"),
|
||
|
("bca", "Bypass Censorship Analytics"),
|
||
|
("bridgelines", "Tor Bridge Lines")
|
||
|
]
|
||
|
form.container.description = "GitHub Project, GitLab Project or AWS S3 bucket name."
|
||
|
form.branch.description = "Ignored for AWS S3."
|
||
|
if form.validate_on_submit():
|
||
|
list_ = MirrorList()
|
||
|
list_.provider = form.provider.data
|
||
|
list_.format = form.format.data
|
||
|
list_.description = form.description.data
|
||
|
list_.container = form.container.data
|
||
|
list_.branch = form.branch.data
|
||
|
list_.filename = form.filename.data
|
||
|
list_.created = datetime.utcnow()
|
||
|
list_.updated = datetime.utcnow()
|
||
|
try:
|
||
|
db.session.add(list_)
|
||
|
db.session.commit()
|
||
|
flash(f"Created new mirror list.", "success")
|
||
|
return redirect(url_for("portal.list.list_list"))
|
||
|
except exc.SQLAlchemyError as e:
|
||
|
print(e)
|
||
|
flash("Failed to create new mirror list.", "danger")
|
||
|
return redirect(url_for("portal.list.list_list"))
|
||
|
if group_id:
|
||
|
form.group.data = group_id
|
||
|
return render_template("new.html.j2", section="list", form=form)
|
||
|
|
||
|
|
||
|
class NewMirrorListForm(FlaskForm):
|
||
|
provider = SelectField('Provider', validators=[DataRequired()])
|
||
|
format = SelectField('Distribution Method', validators=[DataRequired()])
|
||
|
description = StringField('Description', validators=[DataRequired()])
|
||
|
container = StringField('Container', validators=[DataRequired()])
|
||
|
branch = StringField('Branch')
|
||
|
filename = StringField('Filename', validators=[DataRequired()])
|
||
|
submit = SubmitField('Save Changes')
|