From 511bdebea5b9ee025bdd0d4c9582b5e49e8b15cd Mon Sep 17 00:00:00 2001 From: Feliciaan De Palmenaer Date: Sat, 28 Mar 2015 19:25:38 +0100 Subject: [PATCH] Change food => product, orders => items, closes #6 --- app/admin.py | 4 ++-- app/create_database.py | 4 ++-- app/forms.py | 4 ++-- app/models.py | 25 ++++++++++++------------- app/templates/order.html | 4 ++-- app/views/order.py | 10 +++++----- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/app/admin.py b/app/admin.py index b6dceff..b2f8123 100644 --- a/app/admin.py +++ b/app/admin.py @@ -4,7 +4,7 @@ from flask.ext import login from app import app, db -from models import User, Location, Food, Order, OrderItem +from models import User, Location, Product, Order, OrderItem 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(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(OrderItem, db.session)) diff --git a/app/create_database.py b/app/create_database.py index 4be42ca..9b8a6dd 100644 --- a/app/create_database.py +++ b/app/create_database.py @@ -20,11 +20,11 @@ blauw_kotje = Location() blauw_kotje.configure("'t Blauw Kotje", "Top-5-straat Keknet-city", "frietfest.com") db.session.add(blauw_kotje) -chili_con_carne = Food() +chili_con_carne = Product() chili_con_carne.configure(burrito, "Chili Con Carne", 550) db.session.add(chili_con_carne) -medium_pak_frieten = Food() +medium_pak_frieten = Product() medium_pak_frieten.configure(blauw_kotje, "Medium Pak Frieten", 220) db.session.add(medium_pak_frieten) # To future developers, add yourself here diff --git a/app/forms.py b/app/forms.py index 873c9cb..8411c44 100644 --- a/app/forms.py +++ b/app/forms.py @@ -28,8 +28,8 @@ class OrderForm(Form): class OrderItemForm(Form): - food_id = SelectField('Item', coerce=int) + product_id = SelectField('Item', coerce=int) submit_button = SubmitField('Submit') def populate(self, location): - self.food_id.choices = [(i.id, (i.name + ": " + euro(i.price))) for i in location.food] \ No newline at end of file + self.product_id.choices = [(i.id, (i.name + ": " + euro(i.price))) for i in location.products] \ No newline at end of file diff --git a/app/models.py b/app/models.py index f338157..cedbce9 100644 --- a/app/models.py +++ b/app/models.py @@ -11,7 +11,7 @@ class User(db.Model): admin = db.Column(db.Boolean) bias = db.Column(db.Integer) 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): self.username = username @@ -45,10 +45,9 @@ class Location(db.Model): name = db.Column(db.String(120)) address = db.Column(db.String(254)) 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') - def configure(self, name, address, website): self.name = name self.address = address @@ -58,12 +57,12 @@ class Location(db.Model): return '%s: %s' % (self.name, self.address) -class Food(db.Model): +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)) 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): @@ -81,7 +80,7 @@ class Order(db.Model): location_id = db.Column(db.Integer, db.ForeignKey('location.id')) starttime = 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): self.courrier = courrier @@ -94,26 +93,26 @@ class Order(db.Model): def group_by_user(self): group = defaultdict(list) - for item in self.orders: - group[item.user_id] += [item.food] + for item in self.items: + group[item.user_id] += [item.product] return group def group_by_user_pay(self): group = defaultdict(int) - for item in self.orders: - group[item.user] += item.food.price + for item in self.items: + group[item.user] += item.product.price return group 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')) - 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.order = order - self.food = food + self.product = product def __repr__(self): return 'OrderItem' diff --git a/app/templates/order.html b/app/templates/order.html index 8c8b3fa..5aba8f0 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -19,8 +19,8 @@ Stoptime: {{ order.stoptime }}
Total price: {{ total_price|euro }}

Orders

- {% for item in order.orders %} - {{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }} + {% for item in order.items %} + {{ item.user.username }} - {{ item.product.name }} - {{ item.product.price|euro }} {% if item.can_delete(order.id, current_user.id) -%}{%- endif %}
{% endfor %}

Debts

diff --git a/app/views/order.py b/app/views/order.py index 15bdd51..8e7d0e9 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -41,7 +41,7 @@ def order(id): if order is not None: form = OrderItemForm() 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() return render_template('order.html', order=order, form=form, total_price=total_price, total_payments=total_payments) return abort(404) @@ -98,12 +98,12 @@ def close_order(id): order = Order.query.filter(Order.id == id).first() if order is not None: 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() - print(order.courrier_id) if order.courrier_id == 0 or order.courrier_id is None: - order.courrier_id = select_user(order.orders).id - print(order.courrier_id) + courrier = select_user(order.items) + if courrier is not None: + order.courrier_id = courrier.id db.session.commit() return redirect(url_for('.order', id=id)) abort(401)