def create(oid=None): db = get_db() query = db.execute('SELECT id, name FROM products').fetchall() choices = [(str(q['id']), q['name']) for q in query] message = 'Amount must be between 1 and 100.' validators = [NumberRange(min=1, max=100, message=message)] class OrderForm(FlaskForm): item = SelectField('Item', choices=choices) amount = IntegerField('Amount', validators=validators) submit = SubmitField('\u2611') form = OrderForm(data={'amount': 1}) if form.validate_on_submit(): if oid: for _ in range(form.amount.data): db.execute('INSERT INTO prod_order VALUES (?, ?)', (oid, int(form.item.data))) db.commit() return redirect(url_for('orders.update', oid=oid)) db.execute('INSERT INTO orders DEFAULT VALUES') ids = db.execute('SELECT id FROM orders').fetchall() oid = ids[-1]['id'] for _ in range(form.amount.data): db.execute('INSERT INTO prod_order VALUES (?, ?)', (oid, int(form.item.data))) db.commit() return redirect(url_for('orders.update', oid=oid)) return render_template('orders/create.html', oid=oid, form=form)
def update(oid): db = get_db() query = db.execute( 'SELECT m.pid, p.name, p.price ' 'FROM prod_order AS m, products AS p ' 'WHERE m.oid = ? AND m.pid = p.id', (oid, )) return render_template('orders/update.html', query=query, oid=oid)
def create(): form = ProductForm() if form.validate_on_submit(): db = get_db() db.execute('INSERT INTO products (name, price) VALUES (?, ?)', (form.name.data, form.price.data)) db.commit() return redirect(url_for('menu.read')) return render_template('menu/create.html', form=form)
def read(): db = get_db() query = db.execute('SELECT m.oid, o.created, ' 'printf("%.2f", SUM(p.price)) AS sum_prices ' 'FROM prod_order AS m, products AS p, orders AS o ' 'WHERE m.pid = p.id AND m.oid = o.id ' 'GROUP BY m.oid ' 'ORDER BY m.oid') return render_template('orders/view.html', query=query)
def update(id): db = get_db() q = db.execute('SELECT * FROM products WHERE id = ?', (id, )).fetchone() form = ProductForm(data={'name': q['name'], 'price': q['price']}) if form.validate_on_submit(): db.execute('UPDATE products SET name = ?, price = ? WHERE id = ?', (form.name.data, form.price.data, id)) db.commit() return redirect(url_for('menu.read')) return render_template('menu/update.html', form=form, id=id)
def summary(): db = get_db() query = """ SELECT m.oid, p.name, o.created, p.price FROM prod_order AS m, products AS p, orders AS o WHERE m.pid = p.id AND m.oid = o.id ORDER BY m.oid """ df = pd.read_sql(query, db).groupby('oid') s1 = df['name'].apply(list) s2 = df['created'].first() s3 = df['price'].sum() df = pd.concat([s1, s2, s3], axis=1) df.columns = ['Products', 'Created', 'Total'] df.index.rename('Order ID', inplace=True) norders = df.shape[0] tot = df['Total'].sum() return norders, tot, df
def create(): query = get_db().execute('SELECT id FROM orders').fetchall() choices = [(str(q['id']), q['id']) for q in query] class ProductForm(FlaskForm): order = SelectField('Order number:', choices=choices) service = RadioField('Como você se sente em relação ao atendimento?', choices=[('VS', 'Very Satisfied'), ('S', 'Satisfied'), ('I', 'Indifferent'), ('D', 'Dissatisfied')]) food = RadioField('Como você se sente em relação a comida?', choices=[('VS', 'Very Satisfied'), ('S', 'Satisfied'), ('I', 'Indifferent'), ('D', 'Dissatisfied')]) mood = RadioField('Como você se sente em relação ao ambiente?', choices=[('VS', 'Very Satisfied'), ('S', 'Satisfied'), ('I', 'Indifferent'), ('D', 'Dissatisfied')]) courtesy = RadioField('Escolha um aperitivo para sua próxima visita!', choices=[('CW', 'Chicken Wings'), ('FS', 'Fries'), ('SL', 'Salad')]) comment = StringField('Algum comentário adicional?') submit = SubmitField('\u2611') form = ProductForm() if form.validate_on_submit(): pool_answer = { 'order': form.order.data, 'service': form.service.data, 'food': form.food.data, 'mood': form.mood.data, 'courtesy': form.courtesy.data, 'comment': form.comment.data, 'date': datetime.datetime.utcnow() } db = MongoClient().crudy_database db.pools.insert_one(pool_answer) return redirect(url_for('pool.read')) return render_template('pool/create.html', form=form)
def delete(id): db = get_db() db.execute('DELETE FROM products WHERE id = ?', (id, )) db.commit() return redirect(url_for('menu.read'))
def read(): db = get_db() query = db.execute('SELECT * FROM products') return render_template('menu/view.html', query=query)
def delete(oid, pid): db = get_db() db.execute('DELETE FROM prod_order ' 'WHERE oid = ? AND pid = ?', (oid, pid)) db.commit() return redirect(url_for('orders.update', oid=oid))