This commit is contained in:
Feliciaan De Palmenaer 2015-06-24 22:10:26 +02:00
parent a689dcfa1a
commit 4de65752ec
5 changed files with 57 additions and 4 deletions

View file

@ -30,6 +30,7 @@ class OrderForm(Form):
class OrderItemForm(Form):
product_id = SelectField('Item', coerce=int)
extra = StringField('Extra')
submit_button = SubmitField('Submit')
def populate(self, location):

View file

@ -0,0 +1,26 @@
"""Add extra message
Revision ID: 4e94c0b08ed
Revises: 42709384216
Create Date: 2015-06-24 21:42:41.466973
"""
# revision identifiers, used by Alembic.
revision = '4e94c0b08ed'
down_revision = '42709384216'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('order_item', sa.Column('extra', sa.String(length=254), nullable=True))
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_column('order_item', 'extra')
### end Alembic commands ###

View file

@ -102,12 +102,18 @@ class Order(db.Model):
user["paid"] = user.get("paid", True) and item.paid
user["products"] = user.get("products", []) + [item.product]
group[item.get_name()] = user
return group
def group_by_product(self):
group = defaultdict(int)
group = dict()
for item in self.items:
group[item.product.name] += 1
product = group.get(item.product.name, dict())
product['count'] = product.get("count", 0) + 1
if item.extra:
product["extras"] = product.get("extras", []) + [item.extra]
group[item.product.name] = product
return group
def can_close(self, user_id):
@ -128,6 +134,7 @@ class OrderItem(db.Model):
order_id = db.Column(db.Integer, db.ForeignKey('order.id'), nullable=False)
product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
paid = db.Column(db.Boolean, default=False)
extra = db.Column(db.String(254), nullable=True)
name = db.Column(db.String(120))
def configure(self, user, order, product):

View file

@ -18,3 +18,7 @@ body {
.full-width {
width: 100%;
}
.product .extras {
padding-left: 20px;
}

View file

@ -38,6 +38,11 @@
{{ form.product_id(class='form-control select') }}
{{ util.render_form_field_errors(form.product_id) }}
</div>
<div class="form-group {{ 'has-errors' if form.product_id.errors }}">
{{ form.extra.label(class='control-label') }}<br>
{{ form.extra(class='form-control', placeholder='Fill in extras, when applicable') }}
{{ util.render_form_field_errors(form.extra) }}
</div>
{% if current_user.is_anonymous() %}
<div class="form-group{{ ' has-error' if form.name.errors }}{{ ' required' if form.name.flags.required }}">
{{ form.name.label(class='control-label') }}
@ -57,7 +62,7 @@
<h3>Items</h3>
<table class="table table-hover table-condensed">
<thead>
<tr><th>Name</th><th>Item</th><th>Price</th>{% if courier_or_admin %}<th>Paid?</th>{% endif %}<th>Delete</th></tr>
<tr><th>Name</th><th>Item</th><th>Price</th><th>Extra</th>{% if courier_or_admin %}<th>Paid?</th>{% endif %}<th>Delete</th></tr>
</thead>
<tbody>
{% for item in order.items -%}
@ -65,6 +70,7 @@
<td>{{ item.get_name() }}</td>
<td>{{ item.product.name }}</td>
<td>{{ item.product.price|euro }}</td>
<td>{{ item.extra if item.extra }}</td>
{% if courier_or_admin %}<td>{% if not item.paid %} <a class="btn btn-xs btn-primary" href="{{ url_for('.item_paid', order_id=order.id, item_id=item.id) }}">Pay</a> {% else %} <span class="glyphicon glyphicon-chevron-down"></span> {% endif %}</td>{% endif %}
<td>{% if item.can_delete(order.id, current_user.id, session.get('anon_name', '')) -%}<a href="{{ url_for('.delete_item', order_id=order.id, item_id=item.id) }}"><span class="glyphicon glyphicon-remove"></span></a>{%- endif %}<br/></td>
</tr>
@ -75,7 +81,16 @@
<div class="col-md-push-2 col-md-4 darker" id="items-ordered">
<h3>Ordered products:</h3>
{% for key, value in order.group_by_product().items() -%}
{{ key }} - {{ value }}<br/>
<div class="product">
{{ key }}: {{ value["count"] }}
{% if value["extras"] -%}
<div class="extras">
{% for extra in value["extras"] -%}
<div>{{ extra }}</div>
{% endfor %}
</div>
{%- endif %}
</div>
{%- endfor %}
</div>
</div>