Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>
Create new group and list them
src/__init__.py | 2 + src/blueprints/groups.py | 36 +++++++++++++++++++++++++++++++++ src/blueprints/products.py | 2 src/templates/new_group.html | 41 ++++++++++++++++++++++++++++++++++++++ src/types/group.py | 9 +++++++ src/types/product.py | 1
diff --git a/src/__init__.py b/src/__init__.py index 4e4472f3cefbb45a12531771fddfb20dfc7622f9..e6d53084f1b8dc32bfdcec37d0da31fa54c35c50 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -24,10 +24,12 @@ # Blueprints from .blueprints.basic import basic from .blueprints.auth import auth + from .blueprints.groups import groups from .blueprints.products import pro as products app.register_blueprint(basic) app.register_blueprint(auth) app.register_blueprint(products) + app.register_blueprint(groups) with app.app_context(): db.create_all() diff --git a/src/blueprints/groups.py b/src/blueprints/groups.py new file mode 100644 index 0000000000000000000000000000000000000000..87032cf983a25ec9b22928fbf1fbb400dfaf257e --- /dev/null +++ b/src/blueprints/groups.py @@ -0,0 +1,36 @@ +from flask import flash, render_template, Blueprint, request, url_for, redirect +from ..types.group import Group +import json + +groups = Blueprint('groups', __name__, url_prefix = '/groups') + +@groups.route("/", methods = ['GET', 'POST']) +def index(): + if request.method == 'GET': + return json.dumps([dict(group.to_json()) for group in Group.query.all()]) + else: + return create(request.form) + +@groups.route('/new', methods = ['GET']) +def new(): + return render_template('new_group.html', title = 'Criar novo Grupo - 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) + ) + + group.create() + + return redirect(url_for('groups.index')) diff --git a/src/blueprints/products.py b/src/blueprints/products.py index c66415ccb0f8a28974181021fe5b68fcabcfcdb6..1695d5e14b43e9dd3db769af93ff2e4fa96efaa7 100644 --- a/src/blueprints/products.py +++ b/src/blueprints/products.py @@ -1,6 +1,6 @@ from flask import Blueprint, request, render_template, redirect, url_for -pro = Blueprint('products', __name__, url_prefix = 'products') +pro = Blueprint('products', __name__, url_prefix = '/products') @pro.route('/', methods = ['GET', 'POST']) def index(): diff --git a/src/templates/new_group.html b/src/templates/new_group.html new file mode 100644 index 0000000000000000000000000000000000000000..4b7f5a5d8c2e0aaf65d440d4c354f0300ad16085 --- /dev/null +++ b/src/templates/new_group.html @@ -0,0 +1,41 @@ +{% 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('groups.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="location">Location</label> + <input type="text" placeholder="Location" name="location"> + </div> + + <div class="form-section"> + <label for="lat">Lat</label> + <input type="text" placeholder="Lat" name="lat"> + </div> + + <div class="form-section"> + <label for="lng">Lng</label> + <input type="text" placeholder="Lng" name="lng"> + </div> + + <div class="form-action"> + <button type="submit" rel="button">Criar Grupo </button> + </div> + + </form> +{% endblock %} diff --git a/src/types/group.py b/src/types/group.py index d7c21de8b875a604ce84e23a617f6d9cc958c21c..484c726b772b4de54313296934b2b7f73286d199 100644 --- a/src/types/group.py +++ b/src/types/group.py @@ -1,5 +1,6 @@ from . import db from datetime import datetime as dt +from .product import Product class Group(db.Model): __tablename__ = 'groups' @@ -24,4 +25,10 @@ def create(self): db.session.add(self) db.session.commit() - + + def to_json(self): + return { + "id": self.id, + "name": self.name, + "location": self.location + } diff --git a/src/types/product.py b/src/types/product.py index c47301333c2d3756ee1bb29a2cc256b78dd38bf8..432c490de183c5ee394112caf5e824dee59635dc 100644 --- a/src/types/product.py +++ b/src/types/product.py @@ -7,7 +7,6 @@ id = db.Column(db.Integer, primary_key = True) 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) - group = db.relationship("Group", back_populates = "products") created_at = db.Column(db.DateTime, default = dt.utcnow) updated_at = db.Column(db.DateTime, default = dt.utcnow)