2019-09-10 01:06:11 +02:00
|
|
|
"Script for everything User related in the database"
|
2022-05-25 10:07:30 +02:00
|
|
|
from typing import List, Optional
|
2022-05-20 19:04:32 +02:00
|
|
|
|
2019-08-28 03:46:04 +02:00
|
|
|
from models import db
|
|
|
|
|
|
|
|
|
|
|
|
class User(db.Model):
|
2019-09-10 01:06:11 +02:00
|
|
|
"Class used for configuring the User model in the database"
|
2019-08-28 03:46:04 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
|
|
|
admin = db.Column(db.Boolean)
|
|
|
|
bias = db.Column(db.Integer)
|
2022-05-20 19:04:32 +02:00
|
|
|
# Assocation logic
|
2022-05-30 18:50:46 +02:00
|
|
|
associations = db.Column(db.String(255), nullable=False, server_default="")
|
2022-05-20 19:04:32 +02:00
|
|
|
|
|
|
|
# Relations
|
2019-09-05 03:33:29 +02:00
|
|
|
runs = db.relation(
|
|
|
|
"Order",
|
2020-01-26 02:28:20 +01:00
|
|
|
backref="courier",
|
|
|
|
primaryjoin="Order.courier_id==User.id",
|
|
|
|
foreign_keys="Order.courier_id",
|
2019-09-05 03:33:29 +02:00
|
|
|
)
|
|
|
|
orderItems = db.relationship("OrderItem", backref="user", lazy="dynamic")
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2022-05-20 19:04:32 +02:00
|
|
|
def association_list(self) -> List[str]:
|
|
|
|
return self.associations.split(",")
|
|
|
|
|
2022-05-25 10:07:30 +02:00
|
|
|
def configure(self, username: str, admin: bool, bias: int, associations: Optional[List[str]] = None) -> None:
|
2022-05-20 19:04:32 +02:00
|
|
|
"""Configure the User"""
|
|
|
|
if associations is None:
|
|
|
|
associations = []
|
2019-08-28 03:46:04 +02:00
|
|
|
self.username = username
|
|
|
|
self.admin = admin
|
|
|
|
self.bias = bias
|
2022-05-20 19:04:32 +02:00
|
|
|
self.associations = ",".join(associations)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-10 01:06:11 +02:00
|
|
|
# pylint: disable=C0111, R0201
|
2019-09-08 00:41:50 +02:00
|
|
|
def is_authenticated(self) -> bool:
|
2019-08-28 03:46:04 +02:00
|
|
|
return True
|
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def is_active(self) -> bool:
|
2019-08-28 03:46:04 +02:00
|
|
|
return True
|
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def is_admin(self) -> bool:
|
2019-08-28 03:46:04 +02:00
|
|
|
return self.admin
|
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def is_anonymous(self) -> bool:
|
2019-08-28 03:46:04 +02:00
|
|
|
return False
|
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def get_id(self) -> str:
|
2019-08-31 00:50:01 +02:00
|
|
|
return str(self.id)
|
2019-08-28 03:46:04 +02:00
|
|
|
|
2019-09-08 00:41:50 +02:00
|
|
|
def __repr__(self) -> str:
|
2022-04-19 22:03:00 +02:00
|
|
|
return f"{self.username}"
|