2015-03-31 20:15:22 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
from app import db
|
|
|
|
|
|
|
|
|
|
|
|
# Create database models
|
|
|
|
class User(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2015-04-03 18:11:39 +02:00
|
|
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
2015-03-31 20:15:22 +02:00
|
|
|
admin = db.Column(db.Boolean)
|
|
|
|
bias = db.Column(db.Integer)
|
2015-06-04 22:37:00 +02:00
|
|
|
runs = db.relation('Order', backref='courrier', primaryjoin='Order.courrier_id==User.id',
|
|
|
|
foreign_keys='Order.courrier_id')
|
2015-03-31 20:15:22 +02:00
|
|
|
orderItems = db.relationship('OrderItem', backref='user', lazy='dynamic')
|
|
|
|
|
|
|
|
def configure(self, username, admin, bias):
|
|
|
|
self.username = username
|
|
|
|
self.admin = admin
|
|
|
|
self.bias = bias
|
|
|
|
|
|
|
|
def is_authenticated(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_active(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_admin(self):
|
|
|
|
return self.admin
|
|
|
|
|
|
|
|
def is_anonymous(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
try:
|
|
|
|
return unicode(self.id) # python 2
|
|
|
|
except NameError:
|
|
|
|
return str(self.id) # python 3
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '%s' % self.username
|
|
|
|
|
|
|
|
|
|
|
|
class Location(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2015-04-03 14:28:28 +02:00
|
|
|
name = db.Column(db.String(120), nullable=False)
|
2015-03-31 20:15:22 +02:00
|
|
|
address = db.Column(db.String(254))
|
|
|
|
website = db.Column(db.String(120))
|
2015-06-04 18:45:59 +02:00
|
|
|
telephone = db.Column(db.String(20), nullable=True)
|
2015-03-31 20:15:22 +02:00
|
|
|
products = db.relationship('Product', backref='location', lazy='dynamic')
|
|
|
|
orders = db.relationship('Order', backref='location', lazy='dynamic')
|
|
|
|
|
|
|
|
def configure(self, name, address, website):
|
|
|
|
self.name = name
|
|
|
|
self.address = address
|
|
|
|
self.website = website
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '%s' % (self.name)
|
|
|
|
|
|
|
|
|
|
|
|
class Product(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
|
2015-04-03 14:28:28 +02:00
|
|
|
name = db.Column(db.String(120), nullable=False)
|
|
|
|
price = db.Column(db.Integer, nullable=False)
|
2015-03-31 20:15:22 +02:00
|
|
|
orderItems = db.relationship('OrderItem', backref='product', lazy='dynamic')
|
|
|
|
|
|
|
|
def configure(self, location, name, price):
|
|
|
|
self.location = location
|
|
|
|
self.name = name
|
|
|
|
self.price = price
|
|
|
|
|
|
|
|
def __repr__(self):
|
2015-04-03 14:28:28 +02:00
|
|
|
return '%s (€%d)from %s' % (self.name, self.price/100, self.location or 'None')
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Order(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2015-06-04 22:37:00 +02:00
|
|
|
courrier_id = db.Column(db.Integer, nullable=True)
|
2015-03-31 20:15:22 +02:00
|
|
|
location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
|
|
|
|
starttime = db.Column(db.DateTime)
|
|
|
|
stoptime = db.Column(db.DateTime)
|
|
|
|
public = db.Column(db.Boolean, default=True)
|
|
|
|
items = db.relationship('OrderItem', backref='order', lazy='dynamic')
|
|
|
|
|
|
|
|
def configure(self, courrier, location, starttime, stoptime):
|
|
|
|
self.courrier = courrier
|
|
|
|
self.location = location
|
|
|
|
self.starttime = starttime
|
|
|
|
self.stoptime = stoptime
|
|
|
|
|
|
|
|
def __repr__(self):
|
2015-04-03 14:28:28 +02:00
|
|
|
return 'Order %d @ %s' % (self.id, self.location.name or 'None')
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
def group_by_user(self):
|
2015-06-04 21:20:38 +02:00
|
|
|
group = dict()
|
2015-03-31 20:15:22 +02:00
|
|
|
for item in self.items:
|
2015-06-04 21:20:38 +02:00
|
|
|
user = group.get(item.get_name(), dict())
|
2015-06-04 22:08:04 +02:00
|
|
|
user["total"] = user.get("total", 0) + item.product.price
|
2015-06-04 21:20:38 +02:00
|
|
|
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
|
2015-06-24 22:10:26 +02:00
|
|
|
|
2015-03-31 20:15:22 +02:00
|
|
|
return group
|
|
|
|
|
|
|
|
def group_by_product(self):
|
2015-06-24 22:10:26 +02:00
|
|
|
group = dict()
|
2015-03-31 20:15:22 +02:00
|
|
|
for item in self.items:
|
2015-06-24 22:10:26 +02:00
|
|
|
product = group.get(item.product.name, dict())
|
|
|
|
product['count'] = product.get("count", 0) + 1
|
|
|
|
if item.extra:
|
|
|
|
product["extras"] = product.get("extras", []) + [item.extra]
|
|
|
|
group[item.product.name] = product
|
|
|
|
|
2015-03-31 20:15:22 +02:00
|
|
|
return group
|
|
|
|
|
|
|
|
def can_close(self, user_id):
|
|
|
|
if self.stoptime and self.stoptime < datetime.now():
|
|
|
|
return False
|
|
|
|
user = None
|
|
|
|
if user_id:
|
|
|
|
user = User.query.filter_by(id=user_id).first()
|
|
|
|
print(user)
|
|
|
|
if self.courrier_id == user_id or (user and user.is_admin()):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2015-06-04 21:20:38 +02:00
|
|
|
|
2015-03-31 20:15:22 +02:00
|
|
|
class OrderItem(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
2015-04-03 18:11:39 +02:00
|
|
|
order_id = db.Column(db.Integer, db.ForeignKey('order.id'), nullable=False)
|
2015-08-18 20:47:49 +02:00
|
|
|
product_id = db.Column(db.Integer, db.ForeignKey('product.id'),
|
|
|
|
nullable=False)
|
|
|
|
paid = db.Column(db.Boolean, default=False, nullable=False)
|
2015-06-24 22:10:26 +02:00
|
|
|
extra = db.Column(db.String(254), nullable=True)
|
2015-03-31 20:15:22 +02:00
|
|
|
name = db.Column(db.String(120))
|
|
|
|
|
|
|
|
def configure(self, user, order, product):
|
|
|
|
self.user = user
|
|
|
|
self.order = order
|
|
|
|
self.product = product
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
if self.user_id is not None and self.user_id > 0:
|
|
|
|
return self.user.username
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def __repr__(self):
|
2016-01-04 19:38:10 +01:00
|
|
|
product_name = None
|
2016-01-04 19:14:08 +01:00
|
|
|
if self.product:
|
2016-01-04 19:38:10 +01:00
|
|
|
product_name = self.product.name
|
|
|
|
return 'Order %d: %s wants %s' % (self.order_id or 0, self.get_name(), product_name or 'None')
|
2015-03-31 20:15:22 +02:00
|
|
|
|
|
|
|
def can_delete(self, order_id, user_id, name):
|
|
|
|
if int(self.order_id) != int(order_id):
|
|
|
|
return False
|
|
|
|
if self.order.stoptime and self.order.stoptime < datetime.now():
|
|
|
|
return False
|
2015-10-30 20:30:18 +01:00
|
|
|
if self.user is not None and self.user_id == user_id:
|
|
|
|
return True
|
2015-11-05 17:15:03 +01:00
|
|
|
if user_id is None:
|
|
|
|
return False
|
2015-10-30 20:30:18 +01:00
|
|
|
user = User.query.filter(User.id == user_id).first()
|
|
|
|
if user and user.is_admin():
|
2015-03-31 20:15:22 +02:00
|
|
|
return True
|
|
|
|
return False
|