ciclos

commit e49f37863aa88589e3b6b7190074840eaa276e18

Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>

Rig up all the core modules from the project

 core/blueprints/groups.py | 41 +++++++++++++++
 core/blueprints/product_categories.py | 39 ++++++++++++++
 core/blueprints/products.py | 76 +++++++++++++++++++++++++++++
 core/types/__init__.py | 4 +
 core/types/balance.py | 33 ++++++++++++
 core/types/group.py | 32 ++++++++++++
 core/types/product.py | 44 ++++++++++++++++
 core/types/product_category.py | 26 +++++++++
 core/types/user.py | 2 


diff --git a/core/blueprints/groups.py b/core/blueprints/groups.py
new file mode 100644
index 0000000000000000000000000000000000000000..d4484fcac7c3b73810ccb3a9c9b6cbeec73ac002
--- /dev/null
+++ b/core/blueprints/groups.py
@@ -0,0 +1,41 @@
+from flask import flash, render_template, Blueprint, request, url_for, jsonify, redirect
+from core.types.group import Group
+from core.database import db
+import json
+
+groups = Blueprint('groups', __name__, url_prefix = '/groups')
+
+@groups.route("/", methods = ['GET', 'POST'])
+def index():
+    if request.method == 'GET':
+        groups = Group.query.all()
+
+        return render_template("groups.html", groups = groups, title = "Fornecedores - Feira Virtual")
+    else:
+        return create(request.form)
+
+@groups.route('/new', methods = ['GET'])
+def new():
+    return render_template('new_group.html', title = 'Criar novo Fornecedor - Feira Virtual Bem da Terra')
+
+def create(params):
+    name = params.get('name')
+
+    group = Group.query.filter_by(name = name).first()
+
+    if group:
+        flash('This group has been created already')
+        return redirect(url_for('groups.new'))
+
+    group = Group(
+        name = name,
+        location = params.get('location'),
+        lat = params.get('lat', None),
+        lng = params.get('lng', None)
+    )
+
+    db.add(group)
+    db.commit()
+
+    return redirect(url_for('groups.index'))
+




diff --git a/core/blueprints/product_categories.py b/core/blueprints/product_categories.py
new file mode 100644
index 0000000000000000000000000000000000000000..91997a51fab77d4b4c8770d693f3621bdc926b47
--- /dev/null
+++ b/core/blueprints/product_categories.py
@@ -0,0 +1,39 @@
+from flask import flash, Blueprint, jsonify, make_response, render_template, redirect, url_for, request
+from core.types.product_category import ProductCategory
+from core.database import db
+import json
+
+product_categories = Blueprint('product_categories', __name__, url_prefix = '/product_categories')
+
+@product_categories.route('/', methods = ['GET', 'POST'])
+def index():
+    if request.method == 'GET':
+        categories = ProductCategory.query.all()
+
+        return render_template('product_categories.html', categories = categories, title = "Categorias de Produtos - ")
+    else:
+        return create(request.form)
+
+@product_categories.route('/new', methods = ['GET'])
+def new():
+    return render_template('new_product_category.html', title = "Nova Categoria de Produto - Feira Virtual Bem da Terra")
+
+def create(params):
+    name = params.get('name')
+
+    category = ProductCategory.query.filter_by(name = name).first()
+
+    if category:
+        flash('This category is created already')
+        return redirect(url_for('product_categories.new'))
+
+    category = ProductCategory(
+        name = name,
+        description = params.get('description')
+    )
+
+    db.add(category)
+    db.commit()
+
+    return redirect(url_for('product_categories.index'))
+




diff --git a/core/blueprints/products.py b/core/blueprints/products.py
new file mode 100644
index 0000000000000000000000000000000000000000..9445eb9d2db6c0ccd848fd253161c5b05ba471f4
--- /dev/null
+++ b/core/blueprints/products.py
@@ -0,0 +1,76 @@
+from flask import Blueprint, jsonify, flash, request, render_template, redirect, url_for
+from core.types.product import Product
+from core.types.group import Group
+from core.types.product_category import ProductCategory
+from core.types.balance import Balance
+
+from core.database import db
+
+import pdb
+import json
+
+pro = Blueprint('products', __name__, url_prefix = '/products')
+
+@pro.route('/', methods = ['GET', 'POST'])
+def index():
+    if request.method == 'GET':
+        products = Product.query.all()
+
+        return render_template('products.html', products = products, title = "Produtos - Feira Virtual Bem da Terra")
+    else:
+        return create(request.form)
+
+@pro.route('/new', methods = ['GET'])
+def new():
+    categories = ProductCategory.query.all()
+    groups = Group.query.all()
+
+    return render_template('new_product.html', groups = groups, categories = categories, title = "Novo Produto - Feira Virtual Bem da Terra")
+
+def create(params):
+    title = params.get('title')
+    group_id = params.get('group_id')
+    category_id = params.get('category_id')
+
+    product = Product.query.filter_by(title = title, category_id = category_id, group_id = group_id).first()
+
+    if product:
+        flash('A product with this name for this group and category already exists')
+        return redirect(url_for('products.new'))
+
+    product = Product(
+        title = title,
+        description = params.get('description'),
+        group_id = group_id,
+        category_id = category_id,
+        price = params.get('price')
+    )
+
+
+    db.add(product)
+    db.commit()
+
+    balance = Balance(0.0, 0.0, product.id)
+
+    db.add(balance)
+    db.commit()
+
+    return redirect(url_for('products.index'))
+
+@pro.route("/<id>", methods = ['GET'])
+def show(id):
+    product = Product.query.filter_by(id = id).first()
+
+    return jsonify(product.to_json())
+
+@pro.route("/<id>/balance", methods = ['GET', 'POST'])
+def balance(id):
+    product = Product.query.get(id)
+
+    if request.method == "GET":
+        return render_template('edit_product_balance.html', product = product, title = "Editar Balanco - Feira Virtual Bem da Terra")
+    else:
+        product.balance.value = float(request.form.get('value'))
+        db.commit()
+
+        return redirect(url_for('products.index'))




diff --git a/core/types/__init__.py b/core/types/__init__.py
index f9b61db23e9eda545c21c78b8b746d90f1863990..266672d1c6034802b9b596219502e244913b5c12 100644
--- a/core/types/__init__.py
+++ b/core/types/__init__.py
@@ -1 +1,5 @@
 from . import user
+from . import product
+from . import product_category
+from . import group
+from . import balance




diff --git a/core/types/balance.py b/core/types/balance.py
new file mode 100644
index 0000000000000000000000000000000000000000..3a1c590a76111a56fc94b0c726781786c8dc0534
--- /dev/null
+++ b/core/types/balance.py
@@ -0,0 +1,33 @@
+from sqlalchemy import Column, Integer, DateTime, Numeric, ForeignKey
+from core.database import Base
+from datetime import datetime as dt
+
+class Balance(Base):
+    __tablename__ = "balances"
+
+    id = Column(Integer, primary_key=True)
+    value = Column(Numeric(10, 2), default=0.0, nullable=False)
+    reserved = Column(Numeric(10, 2), default=0.0, nullable=False)
+    product_id = Column(Integer, ForeignKey('products.id'), nullable=False)
+
+    created_at = Column(DateTime, nullable=False)
+    updated_at = Column(DateTime, nullable=False)
+
+    def __init__(self, value, reserved, product_id):
+        self.value = value
+        self.reserved = reserved
+        self.product_id = product_id
+
+    def rollback_reserved(self):
+        self.value += self.reserved
+        self.reserved = 0.0
+
+        session.commit()
+
+    def to_json(self):
+        return {
+            "reserved": float(self.reserved),
+            "value": float(self.value),
+            "updated_at": self.updated_at.__str__()
+        }
+




diff --git a/core/types/group.py b/core/types/group.py
new file mode 100644
index 0000000000000000000000000000000000000000..4f2001956a8d653c6af4ad1a85c941365f85f4b7
--- /dev/null
+++ b/core/types/group.py
@@ -0,0 +1,32 @@
+from sqlalchemy import Integer, Column, DateTime, String
+from sqlalchemy.orm import relationship
+from core.database import Base
+from datetime import datetime as dt
+from core.types.product import Product
+
+class Group(Base):
+    __tablename__ = 'groups'
+
+    id = Column(Integer, primary_key=True)
+    name = Column(String(255), nullable=False)
+    location = Column(String(255), nullable=False)
+    lat = Column(String(255))
+    lng = Column(String(255))
+    created_at = Column(DateTime, nullable=False)
+    updated_at = Column(DateTime, nullable=False)
+
+    products = relationship("Product", backref="group", lazy=True)
+
+    def __init__(self, name, location, lat, lng):
+        self.name = name
+        self.location = location
+        self.lat = lat
+        self.lng = lng
+
+    def to_json(self):
+        return {
+            "id": self.id,
+            "name": self.name,
+            "location": self.location,
+        }
+




diff --git a/core/types/product.py b/core/types/product.py
new file mode 100644
index 0000000000000000000000000000000000000000..d2fe4943c0f1da85bcd4b7a208702a4269b2ecd5
--- /dev/null
+++ b/core/types/product.py
@@ -0,0 +1,44 @@
+from core.database import Base
+from core.types.balance import Balance
+from datetime import datetime as dt
+from sqlalchemy import Integer, Column, DateTime, Numeric, String, ForeignKey, Text
+from sqlalchemy.orm import relationship
+
+class Product(Base):
+    __tablename__ = 'products'
+
+    id = Column(Integer, primary_key=True)
+    title = Column(String(255), nullable=False)
+    description = Column(Text)
+    group_id = Column(Integer, ForeignKey('groups.id'), nullable=False)
+    category_id = Column(Integer, ForeignKey('product_categories.id'), nullable=False)
+    price = Column(Numeric(10, 2), nullable=False, default=0.0)
+
+    created_at = Column(DateTime, nullable=False)
+    updated_at = Column(DateTime, nullable=False)
+
+    balance = relationship("Balance", backref="product", lazy=True, uselist=False)
+
+    def __init__(self, title, description, group_id, category_id, price):
+        self.title = title
+        self.description = description
+        self.group_id = group_id
+        self.category_id = category_id
+        self.price = price
+
+    def price_on(self, cycle):
+        margin = (cycle.price_margin * self.price) / 100
+        return self.price + margin
+
+    def to_json(self):
+        return {
+            "title": self.title,
+            "description": self.description,
+            "group_id": self.group_id,
+            "category_id": self.category_id,
+            "price": self.price,
+            "created_at": self.created_at.__str__(),
+            "updated_at": self.updated_at.__str__(),
+            "balance": (self.balance.to_json() if self.balance else None)
+        }
+




diff --git a/core/types/product_category.py b/core/types/product_category.py
new file mode 100644
index 0000000000000000000000000000000000000000..31f3c6d164a976f4146f8ca6ef906b812e94d9a5
--- /dev/null
+++ b/core/types/product_category.py
@@ -0,0 +1,26 @@
+from sqlalchemy import Column, Integer, String, DateTime, Text
+from sqlalchemy.orm import relationship
+from core.database import Base
+from datetime import datetime as dt
+from core.types.product import Product
+
+class ProductCategory(Base):
+    __tablename__ = "product_categories"
+    id = Column(Integer, primary_key=True)
+    name = Column(String(255), nullable=False)
+    description = Column(Text)
+    created_at = Column(DateTime, nullable=False)
+    updated_at = Column(DateTime, nullable=False)
+
+    products = relationship("Product", backref="category", lazy=True)
+
+    def __init__(self, name, description):
+        self.name = name
+        self.description = description
+
+    def to_json(self):
+        return {
+            "name": self.name,
+            "description": self.description,
+        }
+




diff --git a/core/types/user.py b/core/types/user.py
index 2636b6228dfccf9f9e4e9d262f2b63fdbad237da..045f142f612173e8f098ef42fe341147d05cfeff 100644
--- a/core/types/user.py
+++ b/core/types/user.py
@@ -1,5 +1,5 @@
 from sqlalchemy import Column, Integer, String, Unicode, Boolean, DateTime
-from sqlalchemy import ForeignKey, Table, UnicodeText, Text, text
+from sqlalchemy import ForeignKey, Table, Text, text
 from sqlalchemy.orm import relationship, backref
 from core.database import Base