lint: reformat python code with black

This commit is contained in:
Iain Learmonth 2024-12-06 18:15:47 +00:00
parent 331beb01b4
commit a406a7974b
88 changed files with 2579 additions and 1608 deletions

View file

@ -2,8 +2,7 @@ import json
from datetime import datetime, timezone
from typing import Any, Optional
from flask import (Blueprint, Response, flash, redirect, render_template,
url_for)
from flask import Blueprint, Response, flash, redirect, render_template, url_for
from flask.typing import ResponseReturnValue
from flask_wtf import FlaskForm
from sqlalchemy import exc
@ -23,7 +22,7 @@ bp = Blueprint("list", __name__)
_SECTION_TEMPLATE_VARS = {
"section": "list",
"help_url": "https://bypass.censorship.guide/user/lists.html"
"help_url": "https://bypass.censorship.guide/user/lists.html",
}
@ -42,37 +41,44 @@ def list_encoding_name(key: str) -> str:
return MirrorList.encodings_supported.get(key, "Unknown")
@bp.route('/list')
@bp.route("/list")
def list_list() -> ResponseReturnValue:
lists = MirrorList.query.filter(MirrorList.destroyed.is_(None)).all()
return render_template("list.html.j2",
title="Distribution Lists",
item="distribution list",
new_link=url_for("portal.list.list_new"),
items=lists,
**_SECTION_TEMPLATE_VARS
)
return render_template(
"list.html.j2",
title="Distribution Lists",
item="distribution list",
new_link=url_for("portal.list.list_new"),
items=lists,
**_SECTION_TEMPLATE_VARS
)
@bp.route('/preview/<format_>/<pool_id>')
@bp.route("/preview/<format_>/<pool_id>")
def list_preview(format_: str, pool_id: int) -> ResponseReturnValue:
pool = Pool.query.filter(Pool.id == pool_id).first()
if not pool:
return response_404(message="Pool not found")
if format_ == "bca":
return Response(json.dumps(mirror_mapping(pool)), content_type="application/json")
return Response(
json.dumps(mirror_mapping(pool)), content_type="application/json"
)
if format_ == "bc2":
return Response(json.dumps(mirror_sites(pool)), content_type="application/json")
if format_ == "bridgelines":
return Response(json.dumps(bridgelines(pool)), content_type="application/json")
if format_ == "rdr":
return Response(json.dumps(redirector_data(pool)), content_type="application/json")
return Response(
json.dumps(redirector_data(pool)), content_type="application/json"
)
return response_404(message="Format not found")
@bp.route("/destroy/<list_id>", methods=['GET', 'POST'])
@bp.route("/destroy/<list_id>", methods=["GET", "POST"])
def list_destroy(list_id: int) -> ResponseReturnValue:
list_ = MirrorList.query.filter(MirrorList.id == list_id, MirrorList.destroyed.is_(None)).first()
list_ = MirrorList.query.filter(
MirrorList.id == list_id, MirrorList.destroyed.is_(None)
).first()
if list_ is None:
return response_404("The requested bridge configuration could not be found.")
return view_lifecycle(
@ -82,12 +88,12 @@ def list_destroy(list_id: int) -> ResponseReturnValue:
success_message="This list will no longer be updated and may be deleted depending on the provider.",
section="list",
resource=list_,
action="destroy"
action="destroy",
)
@bp.route("/new", methods=['GET', 'POST'])
@bp.route("/new/<group_id>", methods=['GET', 'POST'])
@bp.route("/new", methods=["GET", "POST"])
@bp.route("/new/<group_id>", methods=["GET", "POST"])
def list_new(group_id: Optional[int] = None) -> ResponseReturnValue:
form = NewMirrorListForm()
form.provider.choices = list(MirrorList.providers_supported.items())
@ -116,43 +122,53 @@ def list_new(group_id: Optional[int] = None) -> ResponseReturnValue:
return redirect(url_for("portal.list.list_list"))
if group_id:
form.group.data = group_id
return render_template("new.html.j2",
form=form,
**_SECTION_TEMPLATE_VARS)
return render_template("new.html.j2", form=form, **_SECTION_TEMPLATE_VARS)
class NewMirrorListForm(FlaskForm): # type: ignore
pool = SelectField('Resource Pool', validators=[DataRequired()])
provider = SelectField('Provider', validators=[DataRequired()])
format = SelectField('Distribution Method', validators=[DataRequired()])
encoding = SelectField('Encoding', validators=[DataRequired()])
description = StringField('Description', validators=[DataRequired()])
container = StringField('Container', validators=[DataRequired()],
description="GitHub Project, GitLab Project or AWS S3 bucket name.")
branch = StringField('Git Branch/AWS Region', validators=[DataRequired()],
description="For GitHub/GitLab, set this to the desired branch name, e.g. main. For AWS S3, "
"set this field to the desired region, e.g. us-east-1.")
role = StringField('Role ARN',
description="(Optional) ARN for IAM role to assume for interaction with the S3 bucket.")
filename = StringField('Filename', validators=[DataRequired()])
submit = SubmitField('Save Changes')
pool = SelectField("Resource Pool", validators=[DataRequired()])
provider = SelectField("Provider", validators=[DataRequired()])
format = SelectField("Distribution Method", validators=[DataRequired()])
encoding = SelectField("Encoding", validators=[DataRequired()])
description = StringField("Description", validators=[DataRequired()])
container = StringField(
"Container",
validators=[DataRequired()],
description="GitHub Project, GitLab Project or AWS S3 bucket name.",
)
branch = StringField(
"Git Branch/AWS Region",
validators=[DataRequired()],
description="For GitHub/GitLab, set this to the desired branch name, e.g. main. For AWS S3, "
"set this field to the desired region, e.g. us-east-1.",
)
role = StringField(
"Role ARN",
description="(Optional) ARN for IAM role to assume for interaction with the S3 bucket.",
)
filename = StringField("Filename", validators=[DataRequired()])
submit = SubmitField("Save Changes")
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.pool.choices = [
(pool.id, pool.pool_name) for pool in Pool.query.all()
]
self.pool.choices = [(pool.id, pool.pool_name) for pool in Pool.query.all()]
@bp.route('/edit/<list_id>', methods=['GET', 'POST'])
@bp.route("/edit/<list_id>", methods=["GET", "POST"])
def list_edit(list_id: int) -> ResponseReturnValue:
list_: Optional[MirrorList] = MirrorList.query.filter(MirrorList.id == list_id).first()
list_: Optional[MirrorList] = MirrorList.query.filter(
MirrorList.id == list_id
).first()
if list_ is None:
return Response(render_template("error.html.j2",
header="404 Distribution List Not Found",
message="The requested distribution list could not be found.",
**_SECTION_TEMPLATE_VARS),
status=404)
return Response(
render_template(
"error.html.j2",
header="404 Distribution List Not Found",
message="The requested distribution list could not be found.",
**_SECTION_TEMPLATE_VARS
),
status=404,
)
form = NewMirrorListForm(
pool=list_.pool_id,
provider=list_.provider,
@ -162,7 +178,7 @@ def list_edit(list_id: int) -> ResponseReturnValue:
container=list_.container,
branch=list_.branch,
role=list_.role,
filename=list_.filename
filename=list_.filename,
)
form.provider.choices = list(MirrorList.providers_supported.items())
form.format.choices = list(MirrorList.formats_supported.items())
@ -182,7 +198,10 @@ def list_edit(list_id: int) -> ResponseReturnValue:
db.session.commit()
flash("Saved changes to group.", "success")
except exc.SQLAlchemyError:
flash("An error occurred saving the changes to the distribution list.", "danger")
return render_template("distlist.html.j2",
list=list_, form=form,
**_SECTION_TEMPLATE_VARS)
flash(
"An error occurred saving the changes to the distribution list.",
"danger",
)
return render_template(
"distlist.html.j2", list=list_, form=form, **_SECTION_TEMPLATE_VARS
)