Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Allow to create Product Categories
src/__init__.py | 2 + src/blueprints/product_categories.py | 36 +++++++++++++++++++++++++++ src/templates/new_product_category.html | 32 ++++++++++++++++++++++++ src/types/product_category.py | 24 ++++++++++++++++++
diff --git a/src/__init__.py b/src/__init__.py index e6d53084f1b8dc32bfdcec37d0da31fa54c35c50..8fe250475168f5faaf90236806100b6d88bb5dd9 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -26,8 +26,10 @@ from .blueprints.basic import basic from .blueprints.auth import auth from .blueprints.groups import groups from .blueprints.products import pro as products + from .blueprints.product_categories import product_categories app.register_blueprint(basic) app.register_blueprint(auth) + app.register_blueprint(product_categories) app.register_blueprint(products) app.register_blueprint(groups) diff --git a/src/blueprints/product_categories.py b/src/blueprints/product_categories.py new file mode 100644 index 0000000000000000000000000000000000000000..a7662e1c2074a1d9e9c5809267612555abd2c28f --- /dev/null +++ b/src/blueprints/product_categories.py @@ -0,0 +1,36 @@ +from flask import flash, Blueprint, jsonify, make_response, render_template, redirect, url_for, request +from ..types.product_category import ProductCategory +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 jsonify(list(map(lambda cat: cat.to_json(), categories))) + 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') + ) + + category.create() + + return redirect(url_for('product_categories.index')) diff --git a/src/templates/new_product_category.html b/src/templates/new_product_category.html new file mode 100644 index 0000000000000000000000000000000000000000..effedf35cafa079124f64db0465383dfe5154d9a --- /dev/null +++ b/src/templates/new_product_category.html @@ -0,0 +1,32 @@ +{% 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('product_categories.index') }}' method="POST"> + + <div class="form-section"> + <label for="name">Name</label> + <input type="text" placeholder="Name" name="name"> + </div> + + <div class="form-section"> + <label for="description">Description</label> + <textarea rows="10" columns="50" placeholder="Description" name="description"></textarea> + </div> + + <div class="form-action"> + <button type="submit" rel="button">Criar Categoria</button> + </div> + + </form> +{% endblock %} + diff --git a/src/types/product_category.py b/src/types/product_category.py new file mode 100644 index 0000000000000000000000000000000000000000..3c506e5dd482edef5a525a6ba28da87c150c1bfb --- /dev/null +++ b/src/types/product_category.py @@ -0,0 +1,24 @@ +from . import db +from datetime import datetime as dt + +class ProductCategory(db.Model): + __tablename__ = "product_categories" + id = db.Column(db.Integer, primary_key = True) + name = db.Column(db.String(255), nullable = False) + description = db.Column(db.Text) + created_at = db.Column(db.DateTime, default = dt.utcnow) + updated_at = db.Column(db.DateTime, default = dt.utcnow) + + def __init__(self, name, description): + self.name = name + self.description = description + + def create(self): + db.session.add(self) + db.session.commit() + + def to_json(self): + return { + "name": self.name, + "description": self.description + }