Removed some bugs in closing an order

This commit is contained in:
Feliciaan De Palmenaer 2015-03-28 21:12:02 +01:00
parent bf1b6b92ab
commit 08c0922591
4 changed files with 23 additions and 4 deletions

View file

@ -35,7 +35,6 @@ class OrderItemForm(Form):
def populate(self, location): def populate(self, location):
self.product_id.choices = [(i.id, (i.name + ": " + euro(i.price))) for i in location.products] self.product_id.choices = [(i.id, (i.name + ": " + euro(i.price))) for i in location.products]
class AnonOrderItemForm(OrderItemForm): class AnonOrderItemForm(OrderItemForm):
name = StringField('Name', validators=[validators.required()]) name = StringField('Name', validators=[validators.required()])

View file

@ -104,6 +104,17 @@ class Order(db.Model):
group[item.get_name()] += item.product.price group[item.get_name()] += item.product.price
return group 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
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'))

View file

@ -6,7 +6,7 @@
<div class="row"> <div class="row">
<div class="col-md-7"><!-- Shitty html--> <div class="col-md-7"><!-- Shitty html-->
<h3>Order {{ order.id }} <h3>Order {{ order.id }}
{% if (current_user.id == order.courrier_id and (not order.stoptime)) or current_user.is_admin() -%} {% if order.can_close(current_user.id) -%}
<a class="btn btn-danger" href="{{ url_for('.close_order', id=order.id) }}">Close</a><br/> <a class="btn btn-danger" href="{{ url_for('.close_order', id=order.id) }}">Close</a><br/>
{%- endif %}</h3> {%- endif %}</h3>
Courrier: {{ order.courrier.username }} Courrier: {{ order.courrier.username }}
@ -28,9 +28,11 @@
{{ key }} - {{ value|euro }}<br/> {{ key }} - {{ value|euro }}<br/>
{% endfor %} {% endfor %}
</div> </div>
{% if form -%}
<div class="col-md-push-1 col-md-4"> <div class="col-md-push-1 col-md-4">
<h4>Order:</h4> <h4>Order:</h4>
{{ wtf.quick_form(form, action=url_for('.order_item_create', id=order.id), button_map={'submit_button': 'primary'}, form_type='horizontal') }} {{ wtf.quick_form(form, action=url_for('.order_item_create', id=order.id), button_map={'submit_button': 'primary'}, form_type='horizontal') }}
</div> </div>
{%- endif %}
</div> </div>
{% endblock %} {% endblock %}

View file

@ -45,6 +45,8 @@ def order(id):
else: else:
form = AnonOrderItemForm() form = AnonOrderItemForm()
form.populate(order.location) form.populate(order.location)
if order.stoptime and order.stoptime < datetime.now():
form = None
total_price = sum([o.product.price for o in order.items]) 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)
@ -55,6 +57,8 @@ def order_item_create(id):
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
if order is None: if order is None:
abort(404) abort(404)
if order.stoptime and order.stoptime < datetime.now():
abort(404)
form = None form = None
if not current_user.is_anonymous(): if not current_user.is_anonymous():
form = OrderItemForm() form = OrderItemForm()
@ -107,12 +111,13 @@ def volunteer(id):
def close_order(id): def close_order(id):
order = Order.query.filter(Order.id == id).first() order = Order.query.filter(Order.id == id).first()
if order is None: if order is None:
abort(401) abort(404)
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 or (order.stoptime > datetime.now()): and order.stoptime is None or (order.stoptime > datetime.now()):
order.stoptime = datetime.now() order.stoptime = datetime.now()
if order.courrier_id == 0 or order.courrier_id is None: if order.courrier_id == 0 or order.courrier_id is None:
courrier = select_user(order.items) courrier = select_user(order.items)
print(courrier)
if courrier is not None: if courrier is not None:
order.courrier_id = courrier.id order.courrier_id = courrier.id
db.session.commit() db.session.commit()
@ -123,7 +128,9 @@ app.register_blueprint(order_bp, url_prefix='/order')
def select_user(items): def select_user(items):
user = None user = None
items = list(items) # remove non users
items = [i for i in items if i.user_id]
if len(items) <= 0: if len(items) <= 0:
return None return None