Delete items, with fancy icon

This commit is contained in:
Feliciaan De Palmenaer 2015-03-28 00:44:58 +01:00
parent b34dc7fccc
commit c160002a94
6 changed files with 65 additions and 33 deletions

View file

@ -1,3 +1,4 @@
from datetime import datetime
from app import db from app import db
@ -103,3 +104,12 @@ class OrderItem(db.Model):
def __repr__(self): def __repr__(self):
return 'OrderItem' return 'OrderItem'
def can_delete(self, order_id, user_id):
if self.user_id != user_id:
return False
if int(self.order_id) != int(order_id):
return False
if self.order.stoptime and self.order.stoptime < datetime.now():
return False
return True

View file

@ -6,7 +6,7 @@
<h3>Open orders:</h3> <h3>Open orders:</h3>
<ul> <ul>
{% for order in orders %} {% for order in orders %}
<li><a href="{{ url_for('order', id=order.id) }}">{{ order.location.name }}-{{ order.stoptime }}</a></li> <li><a href="{{ url_for('order_bp.order', id=order.id) }}">{{ order.location.name }}-{{ order.stoptime }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

View file

@ -2,7 +2,7 @@
{% set navbar = [ {% set navbar = [
('home', 'Home'), ('home', 'Home'),
('orders', 'Orders'), ('order_bp.orders', 'Orders'),
('about', 'About'), ('about', 'About'),
('stats', 'Stats'), ('stats', 'Stats'),
] -%} ] -%}

View file

@ -1,18 +1,25 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% set active_page = "orders" -%} {% set active_page = "orders" -%}
{% import "bootstrap/wtf.html" as wtf %}
{% block container %} {% block container %}
<div class="row"> <div class="row">
<div class="col-md-12"><!-- Shitty html--> <div class="col-md-7"><!-- Shitty html-->
<h3>Order {{ order.id }}</h3> <h3>Order {{ order.id }}</h3>
Courrier: {{ order.courrier.username }}<br/> Courrier: {{ order.courrier.username }}<br/>
Location: <a href="{{ order.location.website }}">{{ order.location.name }}</a><br/> Location: <a href="{{ order.location.website }}">{{ order.location.name }}</a><br/>
Starttime: {{ order.starttime }}<br/> Starttime: {{ order.starttime }}<br/>
Stoptime: {{ order.stoptime }}<br/> Stoptime: {{ order.stoptime }}<br/>
Total price: {{ total_price|euro }}
<h3>Orders</h3> <h3>Orders</h3>
{% for item in order.orders %} {% for item in order.orders %}
{{ item.user.username }} - {{ item.food.name }} - {{ item.food.price|euro }}<br/> {{ item.user.username }} - {{ item.food.name }} - {{ item.food.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/>
{% endfor %} {% endfor %}
</div> </div>
<div class="col-md-push-1 col-md-4">
<h4>Order:</h4>
{{ wtf.quick_form(form, action=url_for('.order_create'), button_map={'submit_button': 'primary'}, form_type='horizontal') }}
</div>
</div> </div>
{% endblock %} {% endblock %}

View file

@ -8,12 +8,12 @@
<h3>Open orders:</h3> <h3>Open orders:</h3>
<ul> <ul>
{% for order in orders %} {% for order in orders %}
<li><a href="{{ url_for('order', id=order.id) }}">{{ order.location.name }}-{{ order.stoptime }}</a></li> <li><a href="{{ url_for('.order', id=order.id) }}">{{ order.location.name }}-{{ order.stoptime }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>
<div class="col-md-push-1 col-md-6"> <div class="col-md-push-1 col-md-6">
{{ wtf.quick_form(form, action=url_for('order_create'), button_map={'submit_button': 'primary'}, form_type='horizontal') }} {{ wtf.quick_form(form, action=url_for('.order_create'), button_map={'submit_button': 'primary'}, form_type='horizontal') }}
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,5 +1,5 @@
__author__ = 'feliciaan' __author__ = 'feliciaan'
from flask import url_for, render_template, abort, redirect from flask import url_for, render_template, abort, redirect, Blueprint
from flask.ext.login import current_user, login_required from flask.ext.login import current_user, login_required
from datetime import datetime from datetime import datetime
@ -7,17 +7,45 @@ from app import app, db
from models import Order, OrderItem from models import Order, OrderItem
from forms import OrderItemForm, OrderForm from forms import OrderItemForm, OrderForm
order_bp = Blueprint('order_bp', 'order')
@app.route('/order/<id>') @order_bp.route('/')
@login_required
def orders():
orders = Order.query.filter((Order.stoptime > datetime.now()) | (Order.stoptime == None)).all()
orderForm = OrderForm()
orderForm.populate()
return render_template('orders.html', orders=orders, form=orderForm)
@order_bp.route('/create', methods=['GET', 'POST'])
@login_required
def order_create():
orderForm = OrderForm()
orderForm.populate()
if orderForm.validate_on_submit():
order = Order()
orderForm.populate_obj(order)
db.session.add(order)
db.session.commit()
return redirect(url_for('.order', id=order.id))
return render_template('order_form.html', form=orderForm, url=url_for(".order_create"))
@order_bp.route('/<id>')
@login_required @login_required
def order(id): def 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:
return render_template('order.html', order=order) form = OrderItemForm()
form.populate(order.location)
total_price = sum([o.food.price for o in order.orders])
return render_template('order.html', order=order, form=form, total_price=total_price)
return abort(404) return abort(404)
@app.route('/order/<id>/create', methods=['GET', 'POST']) @order_bp.route('/<id>/create', methods=['GET', 'POST'])
@login_required @login_required
def order_item_create(id): def order_item_create(id):
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
@ -31,31 +59,18 @@ def order_item_create(id):
item.user_id = current_user.id item.user_id = current_user.id
db.session.add(item) db.session.add(item)
db.session.commit() db.session.commit()
return redirect(url_for('order', id=id)) return redirect(url_for('.order', id=id))
return render_template('order_form.html', form=form, url=url_for("order_item_create", id=id)) return render_template('order_form.html', form=form, url=url_for(".order_item_create", id=id))
return abort(404) return abort(404)
@order_bp.route('/<order_id>/<item_id>/delete')
@app.route('/order/create', methods=['GET', 'POST'])
@login_required @login_required
def order_create(): def delete_item(order_id, item_id):
orderForm = OrderForm() item = OrderItem.query.filter(OrderItem.id == item_id).first()
orderForm.populate() if item.can_delete(order_id, current_user.id):
if orderForm.validate_on_submit(): db.session.delete(item)
order = Order()
orderForm.populate_obj(order)
db.session.add(order)
db.session.commit() db.session.commit()
return redirect(url_for('order', id=order.id)) return redirect(url_for('.order', id=order_id))
abort(404)
return render_template('order_form.html', form=orderForm, url=url_for("order_create"))
@app.route('/order')
@login_required
def orders():
orders = Order.query.filter((Order.stoptime > datetime.now()) | (Order.stoptime == None)).all()
orderForm = OrderForm()
orderForm.populate()
return render_template('orders.html', orders=orders, form=orderForm)
app.register_blueprint(order_bp, url_prefix='/order')