Fix pylint on everything

Except hlds related files because that's a mess
This commit is contained in:
Jan-Pieter Baert 2022-04-19 23:20:03 +02:00
parent 461664f629
commit e93460743a
No known key found for this signature in database
GPG key ID: C61C42632C5952F3
8 changed files with 60 additions and 41 deletions

View file

@ -5,7 +5,7 @@
# run arbitrary code.
extension-pkg-whitelist=
fail-under=9
fail-under=9.58
# Add files or directories to the blacklist. They should be base names, not
# paths.

View file

@ -1,3 +1,4 @@
"Module for everything related to Admin users"
import flask_login as login
from flask import Flask
from flask_admin import Admin
@ -7,12 +8,15 @@ from models import Order, OrderItem, OrderItemChoice, User
class ModelBaseView(ModelView):
"Class for the base view of the model"
# pylint: disable=too-few-public-methods, no-self-use
def is_accessible(self) -> bool:
"Function to check if the logged in user is an admin"
return login.current_user.is_admin()
class UserAdminModel(ModelBaseView):
"Class for the model of a UserAdmin"
# pylint: disable=too-few-public-methods
column_searchable_list = ("username",)
column_editable_list = ("username",)
@ -21,6 +25,7 @@ class UserAdminModel(ModelBaseView):
class OrderAdminModel(ModelBaseView):
"Class for the model of a OrderAdmin"
# pylint: disable=too-few-public-methods
column_default_sort = ("starttime", True)
column_list = ["starttime", "stoptime", "location_name", "location_id", "courier"]
@ -34,6 +39,7 @@ class OrderAdminModel(ModelBaseView):
class OrderItemAdminModel(ModelBaseView):
"Class for the model of a OrderItemAdmin"
# pylint: disable=too-few-public-methods
column_default_sort = ("order_id", True)
column_list = [

View file

@ -23,69 +23,70 @@ from utils import euro_string, price_range_string
from zeus import init_oauth
def register_plugins(app: Flask) -> Manager:
def register_plugins(_app: Flask) -> Manager:
"Register the plugins to the app"
# pylint: disable=W0612
if not app.debug:
if not _app.debug:
timedFileHandler = TimedRotatingFileHandler(
app.config["LOGFILE"], when="midnight", backupCount=100
_app.config["LOGFILE"], when="midnight", backupCount=100
)
timedFileHandler.setLevel(logging.DEBUG)
loglogger = logging.getLogger("werkzeug")
loglogger.setLevel(logging.DEBUG)
loglogger.addHandler(timedFileHandler)
app.logger.addHandler(timedFileHandler)
_app.logger.addHandler(timedFileHandler)
# Initialize SQLAlchemy
db.init_app(app)
db.init_app(_app)
# Initialize Flask-Migrate
migrate = Migrate(app, db)
app_manager = Manager(app)
app_manager.add_command("db", MigrateCommand)
app_manager.add_command("runserver", Server(port=8000))
init_admin(app, db)
migrate = Migrate(_app, db)
_app_manager = Manager(_app)
_app_manager.add_command("db", MigrateCommand)
_app_manager.add_command("runserver", Server(port=8000))
init_admin(_app, db)
# Init login manager
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.init_app(_app)
login_manager.anonymous_user = AnonymouseUser
init_login(app)
init_login(_app)
# Add oauth
zeus = init_oauth(app)
app.zeus = zeus
zeus = init_oauth(_app)
_app.zeus = zeus
# Load the bootstrap local cdn
Bootstrap(app)
app.config["BOOTSTRAP_SERVE_LOCAL"] = True
Bootstrap(_app)
_app.config["BOOTSTRAP_SERVE_LOCAL"] = True
# use our own bootstrap theme
app.extensions["bootstrap"]["cdns"]["bootstrap"] = StaticCDN()
_app.extensions["bootstrap"]["cdns"]["bootstrap"] = StaticCDN()
# Load the flask debug toolbar
toolbar = DebugToolbarExtension(app)
toolbar = DebugToolbarExtension(_app)
# Make cookies more secure
app.config.update(
_app.config.update(
SESSION_COOKIE_HTTPONLY=True,
SESSION_COOKIE_SAMESITE="Lax",
)
if not app.debug:
app.config.update(SESSION_COOKIE_SECURE=True)
if not _app.debug:
_app.config.update(SESSION_COOKIE_SECURE=True)
return app_manager
return _app_manager
def add_handlers(app: Flask) -> None:
def add_handlers(_app: Flask) -> None:
"Add handlers for 4xx error codes"
# pylint: disable=W0612,W0613
@app.errorhandler(404)
@_app.errorhandler(404)
def handle404(e) -> typing.Tuple[str, int]:
return render_template("errors/404.html"), 404
@app.errorhandler(401)
@_app.errorhandler(401)
def handle401(e) -> typing.Tuple[str, int]:
return render_template("errors/401.html"), 401
@ -112,7 +113,7 @@ def add_routes(application: Flask) -> None:
application.register_blueprint(debug_bp, url_prefix="/debug")
def add_template_filters(app: Flask) -> None:
def add_template_filters(_app: Flask) -> None:
"Add functions which can be used in the templates"
# pylint: disable=W0612
@app.template_filter("countdown")
@ -144,10 +145,10 @@ def add_template_filters(app: Flask) -> None:
def current_year(_value: typing.Any) -> str:
return str(datetime.now().year)
app.template_filter("euro")(euro_string)
app.template_filter("price_range")(price_range_string)
app.template_filter("any")(any)
app.template_filter("all")(all)
_app.template_filter("euro")(euro_string)
_app.template_filter("price_range")(price_range_string)
_app.template_filter("any")(any)
_app.template_filter("all")(all)
app = Flask(__name__)

View file

@ -41,7 +41,7 @@ def recreate_from_scratch() -> None:
def add_to_current() -> None:
"""Add things to the current database"""
available = [entry_set for entry_set in entry_sets]
available = list(entry_sets)
def add_numbers() -> str:
return " ".join(

View file

@ -1,3 +1,4 @@
"Module used for everything related to the fat versions of models"
import typing
from hlds.definitions import location_definitions
@ -7,16 +8,24 @@ from sqlalchemy.sql import desc, func
class FatModel:
"General class for the fat version of models"
@classmethod
def all(cls):
"Function to query all"
# pylint: disable=E1101
return cls.query.all()
@classmethod
def amount(cls):
"Function to query the amount"
# pylint: disable=E1101
return cls.query.count()
class FatLocation(Location, FatModel):
"Fat version of the Location model"
@classmethod
def all(cls):
return location_definitions
@ -27,6 +36,7 @@ class FatLocation(Location, FatModel):
class FatOrder(Order, FatModel):
"Fat version of the Order model"
# It's hard to add the unique user constraint,
# as DISTINCT seems to apply after a GROUP BY and aggregate
@ -34,16 +44,15 @@ class FatOrder(Order, FatModel):
# even if they get reduced by the disctinct afterwards.
@classmethod
def items_per_order(cls):
return (
Order.query.join(OrderItem)
.group_by(Order.id)
.with_entities(Order.id, func.count(OrderItem.user_id).label("total"))
)
"Function to get the total of all items per order"
return (Order.query.join(OrderItem).group_by(Order.id).with_entities(
Order.id,
func.count(OrderItem.user_id).label("total")))
class FatUser(User, FatModel):
pass
"Fat version of the User model"
class FatOrderItem(OrderItem, FatModel):
pass
"Fat version of the OrderItem model"

View file

@ -50,6 +50,7 @@ class OrderItemForm(Form):
submit_button = SubmitField("Submit")
def populate(self, location: Location) -> None:
"Populate the order item form"
self.dish_id.choices = [(dish.id, dish.name) for dish in location.dishes]
if not self.is_submitted() and self.comment.data is None:
self.comment.data = request.args.get("comment")

View file

@ -1,4 +1,5 @@
#!/usr/bin/env python3
"Module used for parsing the HLDS files"
from hlds.parser import parse_files

View file

@ -10,11 +10,11 @@ def euro_string(value: int) -> str:
euro, cents = divmod(value, 100)
if cents:
return f"{euro}.{cents:02}"
else:
return f"{euro}"
return f"{euro}"
def price_range_string(price_range, include_upper=False):
"Convert a price range to a string formatted euro"
if price_range[0] == price_range[1]:
return euro_string(price_range[0])
return ("{}{}" if include_upper else "from {}").format(
@ -33,4 +33,5 @@ def first(iterable: Iterable, default=None):
def ignore_none(iterable: Iterable):
"Filter to ignore None objects"
return filter(lambda x: x is not None, iterable)