Add typing to model files

This commit is contained in:
Jan-Pieter Baert 2019-09-08 00:41:50 +02:00
parent 51edd1bdc1
commit 27cb10f745
No known key found for this signature in database
GPG key ID: B19186932178234A
6 changed files with 41 additions and 31 deletions

View file

@ -1,17 +1,17 @@
class AnonymouseUser:
id = None
def is_active(self):
def is_active(self) -> bool:
return False
def is_authenticated(self):
def is_authenticated(self) -> bool:
return False
def is_anonymous(self):
def is_anonymous(self) -> bool:
return True
def is_admin(self):
def is_admin(self) -> bool:
return False
def get_id(self):
def get_id(self) -> None:
return None

View file

@ -1,3 +1,5 @@
import typing
from models import db
@ -10,11 +12,12 @@ class Location(db.Model):
products = db.relationship("Product", backref="location", lazy="dynamic")
orders = db.relationship("Order", backref="location", lazy="dynamic")
def configure(self, name, address, telephone, website):
def configure(self, name: str, address: str,
telephone: typing.Optional[str], website: str) -> None:
self.name = name
self.address = address
self.website = website
self.telephone = telephone
def __repr__(self):
def __repr__(self) -> str:
return "%s" % (self.name)

View file

@ -1,6 +1,8 @@
import typing
from datetime import datetime
from .database import db
from .location import Location
from .user import User
@ -13,34 +15,34 @@ class Order(db.Model):
public = db.Column(db.Boolean, default=True)
items = db.relationship("OrderItem", backref="order", lazy="dynamic")
def configure(self, courrier, location, starttime, stoptime):
def configure(self, courrier: User, location: Location,
starttime: db.DateTime, stoptime: db.DateTime) -> None:
self.courrier = courrier
self.location = location
self.starttime = starttime
self.stoptime = stoptime
def __repr__(self):
def __repr__(self) -> str:
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):
group = dict()
def group_by_user(self) -> typing.Dict[str, typing.Any]:
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["to_pay"] = (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]
group[item.get_name()] = user
return group
def group_by_product(self):
group = dict()
def group_by_product(self) -> typing.Dict[str, typing.Any]:
group: typing.Dict[str, typing.Any] = dict()
for item in self.items:
product = group.get(item.product.name, dict())
product["count"] = product.get("count", 0) + 1
@ -50,7 +52,7 @@ class Order(db.Model):
return group
def can_close(self, user_id):
def can_close(self, user_id: int) -> bool:
if self.stoptime and self.stoptime < datetime.now():
return False
user = None

View file

@ -1,6 +1,8 @@
from datetime import datetime
from .database import db
from .order import Order
from .product import Product
from .user import User
@ -17,17 +19,17 @@ class OrderItem(db.Model):
extra = db.Column(db.String(254), nullable=True)
name = db.Column(db.String(120))
def configure(self, user, order, product):
def configure(self, user: User, order: Order, product: Product) -> None:
self.user = user
self.order = order
self.product = product
def get_name(self):
def get_name(self) -> str:
if self.user_id is not None and self.user_id > 0:
return self.user.username
return self.name
def __repr__(self):
def __repr__(self) -> str:
product_name = None
if self.product:
product_name = self.product.name
@ -37,7 +39,7 @@ class OrderItem(db.Model):
product_name or "None",
)
def can_delete(self, order_id, user_id, name):
def can_delete(self, order_id: int, user_id: int, name: str) -> bool:
if int(self.order_id) != int(order_id):
return False
if self.order.stoptime and self.order.stoptime < datetime.now():

View file

@ -1,19 +1,22 @@
from models import db
from .location import Location
class Product(db.Model):
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, name, price):
def configure(self, location: Location, name: str, price: int) -> None:
self.location = location
self.name = name
self.price = price
def __repr__(self):
def __repr__(self) -> str:
return "%s (€%d)from %s" % (
self.name,
self.price / 100,

View file

@ -14,25 +14,25 @@ class User(db.Model):
)
orderItems = db.relationship("OrderItem", backref="user", lazy="dynamic")
def configure(self, username, admin, bias):
def configure(self, username: str, admin: bool, bias: int) -> None:
self.username = username
self.admin = admin
self.bias = bias
def is_authenticated(self):
def is_authenticated(self) -> bool:
return True
def is_active(self):
def is_active(self) -> bool:
return True
def is_admin(self):
def is_admin(self) -> bool:
return self.admin
def is_anonymous(self):
def is_anonymous(self) -> bool:
return False
def get_id(self):
def get_id(self) -> str:
return str(self.id)
def __repr__(self):
def __repr__(self) -> str:
return "%s" % self.username