Change food => product, orders => items, closes #6

This commit is contained in:
Feliciaan De Palmenaer 2015-03-28 19:25:38 +01:00
parent 95519f813f
commit 511bdebea5
6 changed files with 25 additions and 26 deletions

View file

@ -4,7 +4,7 @@ from flask.ext import login
from app import app, db from app import app, db
from models import User, Location, Food, Order, OrderItem from models import User, Location, Product, Order, OrderItem
class ModelBaseView(ModelView): class ModelBaseView(ModelView):
@ -32,6 +32,6 @@ admin = Admin(app, name='FoodBot', url='/admin', template_mode='bootstrap3')
admin.add_view(UserAdminModel(User, db.session)) admin.add_view(UserAdminModel(User, db.session))
admin.add_view(LocationAdminModel(Location, db.session)) admin.add_view(LocationAdminModel(Location, db.session))
admin.add_view(ModelBaseView(Food, db.session)) admin.add_view(ModelBaseView(Product, db.session))
admin.add_view(ModelBaseView(Order, db.session)) admin.add_view(ModelBaseView(Order, db.session))
admin.add_view(ModelBaseView(OrderItem, db.session)) admin.add_view(ModelBaseView(OrderItem, db.session))

View file

@ -20,11 +20,11 @@ blauw_kotje = Location()
blauw_kotje.configure("'t Blauw Kotje", "Top-5-straat Keknet-city", "frietfest.com") blauw_kotje.configure("'t Blauw Kotje", "Top-5-straat Keknet-city", "frietfest.com")
db.session.add(blauw_kotje) db.session.add(blauw_kotje)
chili_con_carne = Food() chili_con_carne = Product()
chili_con_carne.configure(burrito, "Chili Con Carne", 550) chili_con_carne.configure(burrito, "Chili Con Carne", 550)
db.session.add(chili_con_carne) db.session.add(chili_con_carne)
medium_pak_frieten = Food() medium_pak_frieten = Product()
medium_pak_frieten.configure(blauw_kotje, "Medium Pak Frieten", 220) medium_pak_frieten.configure(blauw_kotje, "Medium Pak Frieten", 220)
db.session.add(medium_pak_frieten) db.session.add(medium_pak_frieten)
# To future developers, add yourself here # To future developers, add yourself here

View file

@ -28,8 +28,8 @@ class OrderForm(Form):
class OrderItemForm(Form): class OrderItemForm(Form):
food_id = SelectField('Item', coerce=int) product_id = SelectField('Item', coerce=int)
submit_button = SubmitField('Submit') submit_button = SubmitField('Submit')
def populate(self, location): def populate(self, location):
self.food_id.choices = [(i.id, (i.name + ": " + euro(i.price))) for i in location.food] self.product_id.choices = [(i.id, (i.name + ": " + euro(i.price))) for i in location.products]

View file

@ -11,7 +11,7 @@ class User(db.Model):
admin = db.Column(db.Boolean) admin = db.Column(db.Boolean)
bias = db.Column(db.Integer) bias = db.Column(db.Integer)
runs = db.relationship('Order', backref='courrier', lazy='dynamic') runs = db.relationship('Order', backref='courrier', lazy='dynamic')
orders = db.relationship('OrderItem', backref='user', lazy='dynamic') orderItems = db.relationship('OrderItem', backref='user', lazy='dynamic')
def configure(self, username, admin, bias): def configure(self, username, admin, bias):
self.username = username self.username = username
@ -45,10 +45,9 @@ class Location(db.Model):
name = db.Column(db.String(120)) name = db.Column(db.String(120))
address = db.Column(db.String(254)) address = db.Column(db.String(254))
website = db.Column(db.String(120)) website = db.Column(db.String(120))
food = db.relationship('Food', backref='location', lazy='dynamic') products = db.relationship('Product', backref='location', lazy='dynamic')
orders = db.relationship('Order', backref='location', lazy='dynamic') orders = db.relationship('Order', backref='location', lazy='dynamic')
def configure(self, name, address, website): def configure(self, name, address, website):
self.name = name self.name = name
self.address = address self.address = address
@ -58,12 +57,12 @@ class Location(db.Model):
return '%s: %s' % (self.name, self.address) return '%s: %s' % (self.name, self.address)
class Food(db.Model): class Product(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
location_id = db.Column(db.Integer, db.ForeignKey('location.id')) location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
name = db.Column(db.String(120)) name = db.Column(db.String(120))
price = db.Column(db.Integer) price = db.Column(db.Integer)
orders = db.relationship('OrderItem', backref='food', lazy='dynamic') orderItems = db.relationship('OrderItem', backref='product', lazy='dynamic')
def configure(self, location, name, price): def configure(self, location, name, price):
@ -81,7 +80,7 @@ class Order(db.Model):
location_id = db.Column(db.Integer, db.ForeignKey('location.id')) location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
starttime = db.Column(db.DateTime) starttime = db.Column(db.DateTime)
stoptime = db.Column(db.DateTime) stoptime = db.Column(db.DateTime)
orders = db.relationship('OrderItem', backref='order', lazy='dynamic') items = db.relationship('OrderItem', backref='order', lazy='dynamic')
def configure(self, courrier, location, starttime, stoptime): def configure(self, courrier, location, starttime, stoptime):
self.courrier = courrier self.courrier = courrier
@ -94,26 +93,26 @@ class Order(db.Model):
def group_by_user(self): def group_by_user(self):
group = defaultdict(list) group = defaultdict(list)
for item in self.orders: for item in self.items:
group[item.user_id] += [item.food] group[item.user_id] += [item.product]
return group return group
def group_by_user_pay(self): def group_by_user_pay(self):
group = defaultdict(int) group = defaultdict(int)
for item in self.orders: for item in self.items:
group[item.user] += item.food.price group[item.user] += item.product.price
return group return group
class OrderItem(db.Model): class OrderItem(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
order_id = db.Column(db.Integer, db.ForeignKey('order.id')) order_id = db.Column(db.Integer, db.ForeignKey('order.id'))
food_id = db.Column(db.Integer, db.ForeignKey('food.id')) product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
def configure(self, user, order, food): def configure(self, user, order, product):
self.user = user self.user = user
self.order = order self.order = order
self.food = food self.product = product
def __repr__(self): def __repr__(self):
return 'OrderItem' return 'OrderItem'

View file

@ -19,8 +19,8 @@
Stoptime: {{ order.stoptime }}<br/> Stoptime: {{ order.stoptime }}<br/>
Total price: {{ total_price|euro }} Total price: {{ total_price|euro }}
<h3>Orders</h3> <h3>Orders</h3>
{% for item in order.orders %} {% for item in order.items %}
{{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }} {{ item.user.username }} - {{ item.product.name }} - {{ item.product.price|euro }}
{% if item.can_delete(order.id, current_user.id) -%}<a href="{{ url_for('.delete_item', order_id=order.id, item_id=item.id) }}"><span class="glyphicon glyphicon-remove"></span></a>{%- endif %}<br/> {% if item.can_delete(order.id, current_user.id) -%}<a href="{{ url_for('.delete_item', order_id=order.id, item_id=item.id) }}"><span class="glyphicon glyphicon-remove"></span></a>{%- endif %}<br/>
{% endfor %} {% endfor %}
<h3>Debts</h3> <h3>Debts</h3>

View file

@ -41,7 +41,7 @@ def order(id):
if order is not None: if order is not None:
form = OrderItemForm() form = OrderItemForm()
form.populate(order.location) form.populate(order.location)
total_price = sum([o.food.price for o in order.orders]) total_price = sum([o.product.price for o in order.items])
total_payments = order.group_by_user_pay() total_payments = order.group_by_user_pay()
return render_template('order.html', order=order, form=form, total_price=total_price, total_payments=total_payments) return render_template('order.html', order=order, form=form, total_price=total_price, total_payments=total_payments)
return abort(404) return abort(404)
@ -98,12 +98,12 @@ def close_order(id):
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
if order is not None: if order is not None:
if (current_user.id == order.courrier_id or current_user.is_admin()) \ if (current_user.id == order.courrier_id or current_user.is_admin()) \
and order.stoptime is None: and order.stoptime is None or (order.stoptime > datetime.now()):
order.stoptime = datetime.now() order.stoptime = datetime.now()
print(order.courrier_id)
if order.courrier_id == 0 or order.courrier_id is None: if order.courrier_id == 0 or order.courrier_id is None:
order.courrier_id = select_user(order.orders).id courrier = select_user(order.items)
print(order.courrier_id) if courrier is not None:
order.courrier_id = courrier.id
db.session.commit() db.session.commit()
return redirect(url_for('.order', id=id)) return redirect(url_for('.order', id=id))
abort(401) abort(401)