diff --git a/app/forms.py b/app/forms.py index 61ab915..4eae97d 100644 --- a/app/forms.py +++ b/app/forms.py @@ -35,7 +35,6 @@ class OrderItemForm(Form): def populate(self, location): self.product_id.choices = [(i.id, (i.name + ": " + euro(i.price))) for i in location.products] - class AnonOrderItemForm(OrderItemForm): name = StringField('Name', validators=[validators.required()]) diff --git a/app/models.py b/app/models.py index 14ccd8c..625ed40 100644 --- a/app/models.py +++ b/app/models.py @@ -104,6 +104,17 @@ class Order(db.Model): group[item.get_name()] += item.product.price 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): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) diff --git a/app/templates/order.html b/app/templates/order.html index 136a12f..694652f 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -6,7 +6,7 @@

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) -%} Close
{%- endif %}

Courrier: {{ order.courrier.username }} @@ -28,9 +28,11 @@ {{ key }} - {{ value|euro }}
{% endfor %}
+ {% if form -%}

Order:

{{ wtf.quick_form(form, action=url_for('.order_item_create', id=order.id), button_map={'submit_button': 'primary'}, form_type='horizontal') }}
+ {%- endif %}
{% endblock %} \ No newline at end of file diff --git a/app/views/order.py b/app/views/order.py index a172edb..267a921 100644 --- a/app/views/order.py +++ b/app/views/order.py @@ -45,6 +45,8 @@ def order(id): else: form = AnonOrderItemForm() 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_payments = order.group_by_user_pay() 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() if order is None: abort(404) + if order.stoptime and order.stoptime < datetime.now(): + abort(404) form = None if not current_user.is_anonymous(): form = OrderItemForm() @@ -107,12 +111,13 @@ def volunteer(id): def close_order(id): order = Order.query.filter(Order.id == id).first() if order is None: - abort(401) + abort(404) if (current_user.id == order.courrier_id or current_user.is_admin()) \ and order.stoptime is None or (order.stoptime > datetime.now()): order.stoptime = datetime.now() if order.courrier_id == 0 or order.courrier_id is None: courrier = select_user(order.items) + print(courrier) if courrier is not None: order.courrier_id = courrier.id db.session.commit() @@ -123,7 +128,9 @@ app.register_blueprint(order_bp, url_prefix='/order') def select_user(items): user = None - items = list(items) + # remove non users + items = [i for i in items if i.user_id] + if len(items) <= 0: return None