ciclos

ref: responsive

src/blueprints/groups.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from flask import flash, render_template, Blueprint, request, url_for, jsonify, 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 jsonify([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'))