haldis/app/models.py

135 lines
4 KiB
Python
Raw Normal View History

2015-03-28 00:44:58 +01:00
from datetime import datetime
2015-03-28 01:31:04 +01:00
from collections import defaultdict
2015-03-26 21:49:14 +01:00
from app import db
2015-03-26 22:45:41 +01:00
2015-03-26 21:49:14 +01:00
# Create database models
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
admin = db.Column(db.Boolean)
bias = db.Column(db.Integer)
2015-03-26 23:17:51 +01:00
runs = db.relationship('Order', backref='courrier', lazy='dynamic')
orderItems = db.relationship('OrderItem', backref='user', lazy='dynamic')
2015-03-26 21:49:14 +01:00
2015-03-26 22:23:11 +01:00
def configure(self, username, admin, bias):
2015-03-26 21:49:14 +01:00
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)
name = db.Column(db.String(120))
address = db.Column(db.String(254))
website = db.Column(db.String(120))
products = db.relationship('Product', backref='location', lazy='dynamic')
orders = db.relationship('Order', backref='location', lazy='dynamic')
2015-03-26 21:49:14 +01:00
def configure(self, name, address, website):
self.name = name
self.address = address
self.website = website
def __repr__(self):
return '%s: %s' % (self.name, self.address)
2015-03-26 22:32:34 +01:00
class Product(db.Model):
2015-03-26 21:49:14 +01:00
id = db.Column(db.Integer, primary_key=True)
location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
name = db.Column(db.String(120))
price = db.Column(db.Integer)
orderItems = db.relationship('OrderItem', backref='product', lazy='dynamic')
2015-03-26 23:17:51 +01:00
2015-03-26 21:49:14 +01:00
def configure(self, location, name, price):
self.location = location
self.name = name
self.price = price
def __repr__(self):
return '%s' % self.name
2015-03-26 22:32:34 +01:00
2015-03-26 21:49:14 +01:00
class Order(db.Model):
id = db.Column(db.Integer, primary_key=True)
courrier_id = db.Column(db.Integer, db.ForeignKey('user.id'))
location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
starttime = db.Column(db.DateTime)
stoptime = db.Column(db.DateTime)
2015-03-28 20:38:15 +01:00
public = db.Column(db.Boolean, default=True)
items = db.relationship('OrderItem', backref='order', lazy='dynamic')
2015-03-26 21:49:14 +01:00
def configure(self, courrier, location, starttime, stoptime):
self.courrier = courrier
self.location = location
self.starttime = starttime
self.stoptime = stoptime
def __repr__(self):
return 'Order %s' % (self.location.name)
2015-03-26 21:49:14 +01:00
2015-03-28 01:31:04 +01:00
def group_by_user(self):
group = defaultdict(list)
for item in self.items:
2015-03-28 20:38:15 +01:00
group[item.get_name()] += [item.product]
2015-03-28 01:31:04 +01:00
return group
def group_by_user_pay(self):
group = defaultdict(int)
for item in self.items:
2015-03-28 20:38:15 +01:00
group[item.get_name()] += item.product.price
2015-03-28 01:31:04 +01:00
return group
2015-03-26 23:17:51 +01:00
2015-03-26 21:49:14 +01:00
class OrderItem(db.Model):
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'))
product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
2015-03-28 20:38:15 +01:00
name = db.Column(db.String(120))
2015-03-26 21:49:14 +01:00
def configure(self, user, order, product):
2015-03-26 21:49:14 +01:00
self.user = user
self.order = order
self.product = product
2015-03-26 21:49:14 +01:00
2015-03-28 20:38:15 +01:00
def get_name(self):
if self.user_id is not None and self.user_id > 0:
return self.user.username
return self.name
2015-03-26 21:49:14 +01:00
def __repr__(self):
2015-03-28 20:38:15 +01:00
return 'Order %d: %s wants %s' % (self.order_id, self.get_name(), self.product.name)
2015-03-28 00:44:58 +01:00
2015-03-28 20:38:15 +01:00
def can_delete(self, order_id, user_id, name):
2015-03-28 00:44:58 +01:00
if int(self.order_id) != int(order_id):
return False
if self.order.stoptime and self.order.stoptime < datetime.now():
return False
2015-03-28 20:38:15 +01:00
if self.user_id == user_id or self.name == name:
return True
return False