Merge pull request #60 from ZeusWPI/add-extras

Extras
This commit is contained in:
Wout Schellaert 2015-08-04 17:21:36 +01:00
commit f2e2bd2727
5 changed files with 56 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') }}
@ -63,7 +68,7 @@
{% for item in order.items -%}
<tr>
<td>{{ item.get_name() }}</td>
<td>{{ item.product.name }}</td>
<td><span title="{{ item.extra if item.extra }}">{{ item.product.name }}{{ "*" if item.extra }}</span></td>
<td>{{ item.product.price|euro }}</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>
@ -75,7 +80,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>