Fix pylint for models files

This commit is contained in:
Jan-Pieter Baert 2019-09-10 01:06:11 +02:00
parent d21f2d48f5
commit ec82639c5c
No known key found for this signature in database
GPG key ID: B19186932178234A
8 changed files with 43 additions and 17 deletions

View file

@ -1,3 +1,5 @@
"The base file for everything related to the models"
# pylint: disable=C0301
# This file will expose what we want from the models module
# This will probably be everything. But putting the imports here makes it possible to import all models in one line like this:
#

View file

@ -1,3 +1,7 @@
"AnonymouseUser for people who are not logged in the normal way"
# pylint: disable=R0201,C0111
class AnonymouseUser:
id = None

View file

@ -1,3 +1,4 @@
# pylint: disable=C0111
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

View file

@ -1,9 +1,11 @@
"Script for everything Location related in the database"
import typing
from models import db
class Location(db.Model):
"Class used for configuring the Location model in the database"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), nullable=False)
address = db.Column(db.String(254))
@ -12,9 +14,9 @@ class Location(db.Model):
products = db.relationship("Product", backref="location", lazy="dynamic")
orders = db.relationship("Order", backref="location", lazy="dynamic")
def configure(
self, name: str, address: str, telephone: typing.Optional[str], website: str
) -> None:
def configure(self, name: str, address: str,
telephone: typing.Optional[str], website: str) -> None:
"Configure the Location"
self.name = name
self.address = address
self.website = website

View file

@ -1,3 +1,4 @@
"Script for everything Order related in the database"
import typing
from datetime import datetime
@ -7,6 +8,7 @@ from .user import User
class Order(db.Model):
"Class used for configuring the Order model in the database"
id = db.Column(db.Integer, primary_key=True)
courrier_id = db.Column(db.Integer, nullable=True)
location_id = db.Column(db.Integer, db.ForeignKey("location.id"))
@ -15,31 +17,31 @@ class Order(db.Model):
public = db.Column(db.Boolean, default=True)
items = db.relationship("OrderItem", backref="order", lazy="dynamic")
def configure(
self,
courrier: User,
location: Location,
starttime: db.DateTime,
stoptime: db.DateTime,
) -> None:
def configure(self, courrier: User, location: Location,
starttime: db.DateTime, stoptime: db.DateTime,) -> None:
"Configure the Order"
# pylint: disable=W0201
self.courrier = courrier
self.location = location
self.starttime = starttime
self.stoptime = stoptime
def __repr__(self) -> str:
# pylint: disable=R1705
if self.location:
return "Order %d @ %s" % (self.id, self.location.name or "None")
else:
return "Order %d" % (self.id)
def group_by_user(self) -> typing.Dict[str, typing.Any]:
"Group items of an Order by user"
group: typing.Dict[str, typing.Any] = dict()
for item in self.items:
user = group.get(item.get_name(), dict())
user["total"] = user.get("total", 0) + item.product.price
user["to_pay"] = (
user.get("to_pay", 0) + item.product.price if not item.paid else 0
user.get("to_pay", 0) +
item.product.price if not item.paid else 0
)
user["paid"] = user.get("paid", True) and item.paid
user["products"] = user.get("products", []) + [item.product]
@ -48,6 +50,7 @@ class Order(db.Model):
return group
def group_by_product(self) -> typing.Dict[str, typing.Any]:
"Group items of an Order by product"
group: typing.Dict[str, typing.Any] = dict()
for item in self.items:
product = group.get(item.product.name, dict())
@ -59,6 +62,7 @@ class Order(db.Model):
return group
def can_close(self, user_id: int) -> bool:
"Check if a user can close the Order"
if self.stoptime and self.stoptime < datetime.now():
return False
user = None

View file

@ -1,3 +1,4 @@
"Script for everything OrderItem related in the database"
from datetime import datetime
from .database import db
@ -7,6 +8,7 @@ from .user import User
class OrderItem(db.Model):
"Class used for configuring the OrderItem model in the database"
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
order_id = db.Column(db.Integer, db.ForeignKey("order.id"), nullable=False)
@ -20,11 +22,14 @@ class OrderItem(db.Model):
name = db.Column(db.String(120))
def configure(self, user: User, order: Order, product: Product) -> None:
"Configure the OrderItem"
# pylint: disable=W0201
self.user = user
self.order = order
self.product = product
def get_name(self) -> str:
"Get the name of the user which 'owns' the item"
if self.user_id is not None and self.user_id > 0:
return self.user.username
return self.name
@ -39,7 +44,9 @@ class OrderItem(db.Model):
product_name or "None",
)
# pylint: disable=W0613
def can_delete(self, order_id: int, user_id: int, name: str) -> bool:
"Check if a user can delete an item"
if int(self.order_id) != int(order_id):
return False
if self.order.stoptime and self.order.stoptime < datetime.now():

View file

@ -1,23 +1,25 @@
"Script for everything Product related in the database"
from models import db
from .location import Location
class Product(db.Model):
"Class used for configuring the Product model in the database"
id = db.Column(db.Integer, primary_key=True)
location_id = db.Column(db.Integer, db.ForeignKey("location.id"))
name = db.Column(db.String(120), nullable=False)
price = db.Column(db.Integer, nullable=False)
orderItems = db.relationship("OrderItem", backref="product", lazy="dynamic")
orderItems = db.relationship("OrderItem",
backref="product", lazy="dynamic")
def configure(self, location: Location, name: str, price: int) -> None:
"Configure the Product"
# pylint: disable=W0201
self.location = location
self.name = name
self.price = price
def __repr__(self) -> str:
return "%s (€%d)from %s" % (
self.name,
self.price / 100,
self.location or "None",
)
return "%s (€%d)from %s" % (self.name, self.price / 100,
self.location or "None",)

View file

@ -1,7 +1,9 @@
"Script for everything User related in the database"
from models import db
class User(db.Model):
"Class used for configuring the User model in the database"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
admin = db.Column(db.Boolean)
@ -15,10 +17,12 @@ class User(db.Model):
orderItems = db.relationship("OrderItem", backref="user", lazy="dynamic")
def configure(self, username: str, admin: bool, bias: int) -> None:
"Configure the User"
self.username = username
self.admin = admin
self.bias = bias
# pylint: disable=C0111, R0201
def is_authenticated(self) -> bool:
return True