Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Display order total sum
src/__init__.py | 7 ++ src/alembic/versions/6454dfce4c2d_add_price_to_product.py | 26 +++++++++ src/templates/cycle.html | 2 src/types/order.py | 4 + src/types/product.py | 1
diff --git a/src/__init__.py b/src/__init__.py index b327e6235689ecdff274dfabd38cef0eb0273525..01a0131bd0835cbd92780bd4edc4e7e831641b56 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,13 +1,20 @@ from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy from flask_login import LoginManager +import locale + +def as_currency(value): + return locale.currency(value) def create_app(): app = Flask(__name__) + locale.setlocale(locale.LC_ALL, 'pt_BR') + # Database app.config['SECRET_KEY'] = '123456asckjnsac' app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/ciclos_dev' + app.jinja_env.filters['as_currency'] = as_currency login_manager = LoginManager() login_manager.login_view = 'auth.login' diff --git a/src/alembic/versions/6454dfce4c2d_add_price_to_product.py b/src/alembic/versions/6454dfce4c2d_add_price_to_product.py new file mode 100644 index 0000000000000000000000000000000000000000..3823c70313f7625ff3856ce58c004f829e28fa37 --- /dev/null +++ b/src/alembic/versions/6454dfce4c2d_add_price_to_product.py @@ -0,0 +1,26 @@ +"""add price to product + +Revision ID: 6454dfce4c2d +Revises: 6e334556e8f8 +Create Date: 2019-12-17 00:18:19.738236 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy import Column +from sqlalchemy.types import DECIMAL + +# revision identifiers, used by Alembic. +revision = '6454dfce4c2d' +down_revision = '6e334556e8f8' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('products', + Column('price', DECIMAL(10, 2), nullable = False, default = 0) + ) + +def downgrade(): + op.drop_column('products', 'price') diff --git a/src/templates/cycle.html b/src/templates/cycle.html index 4594afc511688f97183bad971e586b73e46f3355..bc9a853bec2b8d21d4245fb79a557217001bc150 100644 --- a/src/templates/cycle.html +++ b/src/templates/cycle.html @@ -47,7 +47,7 @@ {% endfor %} </ul> <div style="text-align: left;"> - Total: <em>R$ 19,17</em> + Total: <em>{{ current_order.total() | as_currency }}</em> </div> </section> diff --git a/src/types/order.py b/src/types/order.py index e8ac1d2f31d79c7559fe1979797d07223755271b..6470f0a2e78cde41a74b3c79a9e337c0849fabd0 100644 --- a/src/types/order.py +++ b/src/types/order.py @@ -2,6 +2,7 @@ from . import db from datetime import datetime as dt from .order_product import OrderProduct from .product import Product +from sqlalchemy.sql import func class Order(db.Model): __tablename__ = 'orders' @@ -41,6 +42,9 @@ return list(map(lambda entry: entry.product_id, self.order_products)) def products(self): return list(map(lambda entry_id: Product.query.get(entry_id), self.selected_product_ids())) + + def total(self): + return Product.query.filter(Product.id.in_(self.selected_product_ids())).with_entities(func.sum(Product.price)).scalar() def to_json(self): return { diff --git a/src/types/product.py b/src/types/product.py index 75dab0884b580a4938997fe841628e9ead848f16..4f8ca499c9214bd6072f7c9ae783e79938f2a667 100644 --- a/src/types/product.py +++ b/src/types/product.py @@ -9,6 +9,7 @@ title = db.Column(db.String(256), nullable = False) description = db.Column(db.Text) group_id = db.Column(db.Integer, db.ForeignKey('groups.id'), nullable = False) category_id = db.Column(db.Integer, db.ForeignKey('product_categories.id'), nullable = False) + price = db.Column(db.Numeric(10, 2), nullable = False, default = 0.0) created_at = db.Column(db.DateTime, default = dt.utcnow) updated_at = db.Column(db.DateTime, default = dt.utcnow)