Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Create and display products
src/blueprints/groups.py | 4 +- src/blueprints/products.py | 42 ++++++++++++++++++++++++++++-- src/templates/new_product.html | 50 ++++++++++++++++++++++++++++++++++++ src/templates/products.html | 23 ++++++++++++++++ src/types/group.py | 2 src/types/product.py | 20 ++++++++++++++ src/types/product_category.py | 2
diff --git a/src/blueprints/groups.py b/src/blueprints/groups.py index 87032cf983a25ec9b22928fbf1fbb400dfaf257e..27c04a905a411ed3ed9ba9501a079aa04b25c71c 100644 --- a/src/blueprints/groups.py +++ b/src/blueprints/groups.py @@ -1,4 +1,4 @@ -from flask import flash, render_template, Blueprint, request, url_for, redirect +from flask import flash, render_template, Blueprint, request, url_for, jsonify, redirect from ..types.group import Group import json @@ -7,7 +7,7 @@ @groups.route("/", methods = ['GET', 'POST']) def index(): if request.method == 'GET': - return json.dumps([dict(group.to_json()) for group in Group.query.all()]) + return jsonify([dict(group.to_json()) for group in Group.query.all()]) else: return create(request.form) diff --git a/src/blueprints/products.py b/src/blueprints/products.py index 1695d5e14b43e9dd3db769af93ff2e4fa96efaa7..4ed5e4086e1a9a884f5b021285d28f2f050a0ce5 100644 --- a/src/blueprints/products.py +++ b/src/blueprints/products.py @@ -1,10 +1,46 @@ -from flask import Blueprint, request, render_template, redirect, url_for +from flask import Blueprint, flash, request, render_template, redirect, url_for +from ..types.product import Product +from ..types.group import Group +from ..types.product_category import ProductCategory + +import json pro = Blueprint('products', __name__, url_prefix = '/products') @pro.route('/', methods = ['GET', 'POST']) def index(): if request.method == 'GET': - return 'List of prod' + products = Product.query.all() + + return render_template('products.html', products = products, title = "Produtos - Feira Virtual Bem da Terra") else: - return 'Create new prod' + 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 + ) + + product.create() + + return redirect(url_for('products.index')) diff --git a/src/templates/new_product.html b/src/templates/new_product.html new file mode 100644 index 0000000000000000000000000000000000000000..cf1b8450ea0b818dee23e98e1fc11c0223dda3ec --- /dev/null +++ b/src/templates/new_product.html @@ -0,0 +1,50 @@ +{% extends 'layout.html' %} + +{% block body %} + <section class="messages"> + {% with messages = get_flashed_messages() %} + {% if messages %} + <section> + {{ messages[0] }} + </section> + {% endif %} + {% endwith %} + </section> + + <form action='{{ url_for('products.index') }}' method="POST"> + + <div class="form-section"> + <label for="title">Title</label> + <input type="text" placeholder="Title" name="title"> + </div> + + <div class="form-section"> + <label for="description">Description</label> + <textarea rows="10" columns="50" placeholder="Description" name="description"></textarea> + </div> + + <div class="form-section"> + <label for="group_id">Group</label> + <select id="" name="group_id"> + {% for group in groups %} + <option value="{{group.id}}">{{group.name}}</option> + {% endfor %} + </select> + </div> + + <div class="form-section"> + <label for="category_id">Category</label> + <select id="" name="category_id"> + {% for category in categories %} + <option value="{{ category.id }}">{{ category.name }}</option> + {% endfor %} + </select> + </div> + + <div class="form-action"> + <button type="submit" rel="button">Criar Produto</button> + </div> + + </form> +{% endblock %} + diff --git a/src/templates/products.html b/src/templates/products.html new file mode 100644 index 0000000000000000000000000000000000000000..2cd5c78b755c5863d4e084ac1515095590bf6238 --- /dev/null +++ b/src/templates/products.html @@ -0,0 +1,23 @@ +{% extends 'layout.html' %} + +{% block body %} + <section> + <a href="{{ url_for('products.new') }}">Novo Produto</a> + </section> + + <table> + <thead> + <tr> + <th>TÃtulo</th> + </tr> + </thead> + <tbody> + {% for product in products %} + <tr> + <td>{{ product.title }}</td> + </tr> + {% endfor %} + </tbody> + </table> + +{% endblock %} diff --git a/src/types/group.py b/src/types/group.py index 7eef507fb01de5b19cda179372b3ecef170e96a9..9f6176c478ae4b47a0c4eeaa0f7341f6c6b5703d 100644 --- a/src/types/group.py +++ b/src/types/group.py @@ -31,5 +31,5 @@ return { "id": self.id, "name": self.name, "location": self.location, - "products": self.products + "products": list(map(lambda p: p.to_json(),self.products)) } diff --git a/src/types/product.py b/src/types/product.py index b85663b1481cba4f4135c9067f075cec97e712d2..35f8241cdcba0fdf3e30bbdc98a17661309573bc 100644 --- a/src/types/product.py +++ b/src/types/product.py @@ -11,3 +11,23 @@ category_id = db.Column(db.Integer, db.ForeignKey('product_categories.id'), nullable = False) created_at = db.Column(db.DateTime, default = dt.utcnow) updated_at = db.Column(db.DateTime, default = dt.utcnow) + + def __init__(self, title, description, group_id, category_id): + self.title = title + self.description = description + self.group_id = group_id + self.category_id = category_id + + def create(self): + db.session.add(self) + db.session.commit() + + def to_json(self): + return { + "title": self.title, + "description": self.description, + "group_id": self.group_id, + "category_id": self.category_id, + "created_at": self.created_at.__str__(), + "updated_at": self.updated_at.__str__() + } diff --git a/src/types/product_category.py b/src/types/product_category.py index 4d3cfb64482186e2dcdd911a12cf21f97b36c240..dde57331cdb9c64c618f3e8c08b33015db5b1f3c 100644 --- a/src/types/product_category.py +++ b/src/types/product_category.py @@ -25,5 +25,5 @@ def to_json(self): return { "name": self.name, "description": self.description, - "products": self.products + "products": list(map(lambda p: p.to_json(), self.products)) }