From 424af3e2cb5c5fbf77ac25fb1210e7ac7e2fa057 Mon Sep 17 00:00:00 2001 From: Feliciaan De Palmenaer Date: Fri, 27 Mar 2015 21:57:37 +0100 Subject: [PATCH] Added orders form (simple bootstrap), using new bootstrap package --- app/app.py | 2 ++ app/foodbot.py | 1 + app/forms.py | 24 ++++++++++++++++++++++ app/requirements.txt | 1 + app/templates/about.html | 2 +- app/templates/errors/401.html | 2 +- app/templates/errors/404.html | 2 +- app/templates/home.html | 2 +- app/templates/home_loggedin.html | 2 +- app/templates/layout.html | 35 +++++++++++++++----------------- app/templates/order.html | 4 +++- app/templates/order_form.html | 9 ++++++++ app/templates/orders.html | 18 ++++++++++++++++ app/templates/stats.html | 2 +- app/views.py | 29 +++++++++++++++++++++++--- 15 files changed, 106 insertions(+), 29 deletions(-) create mode 100644 app/forms.py create mode 100644 app/templates/order_form.html create mode 100644 app/templates/orders.html diff --git a/app/app.py b/app/app.py index 11bac36..c4e09cd 100644 --- a/app/app.py +++ b/app/app.py @@ -1,8 +1,10 @@ from flask import Flask +from flask.ext.bootstrap import Bootstrap from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_object('config.Configuration') +Bootstrap(app) db = SQLAlchemy(app) diff --git a/app/foodbot.py b/app/foodbot.py index f723257..92df91e 100644 --- a/app/foodbot.py +++ b/app/foodbot.py @@ -10,6 +10,7 @@ from app import app, db from admin import admin from login import login_manager from models import * +from forms import * from utils import * from views import * diff --git a/app/forms.py b/app/forms.py new file mode 100644 index 0000000..5e21f2f --- /dev/null +++ b/app/forms.py @@ -0,0 +1,24 @@ +from datetime import datetime, timedelta +from flask_wtf import Form +from wtforms import SelectField, DateTimeField, validators, SubmitField +from models import User, Location + +__author__ = 'feliciaan' + + +class OrderForm(Form): + courrier_id = SelectField('Courrier', coerce=int) + location_id = SelectField('Location', coerce=int, validators=[validators.optional()]) + starttime = DateTimeField('Starttime', default=datetime.now) + stoptime = DateTimeField('Stoptime') + submit_button = SubmitField('Submit Form') + + def populate(self): + self.courrier_id.choices = [(0, None)] + \ + [(u.id, u.username) for u in User.query.order_by('username')] + self.location_id.choices = [(l.id, l.name) + for l in Location.query.order_by('name')] + if self.stoptime.data is None: + self.stoptime.data = datetime.now() + timedelta(hours=1) + + diff --git a/app/requirements.txt b/app/requirements.txt index 44bb7c4..b101d8f 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -3,6 +3,7 @@ Flask-Admin==1.0.9 Flask-Login==0.2.11 Flask-SQLAlchemy==2.0 Flask-WTF==0.10.3 +Flask-Bootstrap==3.3.2.1 Jinja2==2.7.2 MarkupSafe==0.23 SQLAlchemy==0.9.8 diff --git a/app/templates/about.html b/app/templates/about.html index b412cf7..b2732d5 100644 --- a/app/templates/about.html +++ b/app/templates/about.html @@ -1,7 +1,7 @@ {% extends "layout.html" -%} {% set active_page = "about" -%} -{% block content %} +{% block container %}

About

This is an About page for FoodBot. Don't I look good? Oh stop, you're making me blush.

{% endblock %} \ No newline at end of file diff --git a/app/templates/errors/401.html b/app/templates/errors/401.html index b8cb43e..59d7815 100644 --- a/app/templates/errors/401.html +++ b/app/templates/errors/401.html @@ -1,6 +1,6 @@ {% extends "layout.html" -%} -{% block content %} +{% block container %}

Unauthorized

You're not authorized to look to this page!

diff --git a/app/templates/errors/404.html b/app/templates/errors/404.html index 2a1e549..534a1e1 100644 --- a/app/templates/errors/404.html +++ b/app/templates/errors/404.html @@ -1,6 +1,6 @@ {% extends "layout.html" -%} -{% block content %} +{% block container %}

Page Not Found

What you were looking for is just not there.

diff --git a/app/templates/home.html b/app/templates/home.html index eefe505..f2bd54d 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -1,6 +1,6 @@ {% extends "layout.html" -%} {% set active_page = "home" -%} -{% block content %} +{% block container %}

Welcome to FoodBot

This is the home page for FoodBot

diff --git a/app/templates/home_loggedin.html b/app/templates/home_loggedin.html index 26bbc22..8207d05 100644 --- a/app/templates/home_loggedin.html +++ b/app/templates/home_loggedin.html @@ -1,5 +1,5 @@ {% extends "home.html" %} -{% block content %} +{% block container %} {{ super() }}
diff --git a/app/templates/layout.html b/app/templates/layout.html index be3a550..fd0ca1d 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -1,5 +1,8 @@ +{% extends "bootstrap/base.html" %} + {% set navbar = [ ('home', 'Home'), + ('orders', 'Orders'), ('about', 'About'), ('stats', 'Stats'), ] -%} @@ -7,19 +10,15 @@ {% set navbar = navbar + [('admin.index', 'Admin')] -%} {% endif -%} {% set active_page = active_page|default('index') -%} - - - - - - - FoodBot - +{% block title %} + FoodBot +{% endblock %} +{% block styles %} + {{ super() }} - - - +{% endblock %} +{% block navbar %} +{% endblock %} + +{% block content -%}
- {% block content %} - {% endblock %} + {% block container -%} + {%- endblock %}
- - - - - - \ No newline at end of file +{%- endblock %} diff --git a/app/templates/order.html b/app/templates/order.html index 78329cc..7485461 100644 --- a/app/templates/order.html +++ b/app/templates/order.html @@ -1,5 +1,7 @@ {% extends "layout.html" %} -{% block content %} +{% set active_page = "orders" -%} + +{% block container %}

Order {{ order.id }}

diff --git a/app/templates/order_form.html b/app/templates/order_form.html new file mode 100644 index 0000000..178b34d --- /dev/null +++ b/app/templates/order_form.html @@ -0,0 +1,9 @@ +{% extends "layout.html" %} +{% set active_page = "orders" -%} + +{% import "bootstrap/wtf.html" as wtf %} +{% block container %} +
+ {{ wtf.quick_form(form, action=url_for('order_create'), button_map={'submit_button': 'primary'}) }} +
+{% endblock %} \ No newline at end of file diff --git a/app/templates/orders.html b/app/templates/orders.html new file mode 100644 index 0000000..d764d07 --- /dev/null +++ b/app/templates/orders.html @@ -0,0 +1,18 @@ +{% extends 'layout.html' %} +{% set active_page = "orders" -%} + +{% block container %} +
+
+

Open orders:

+ +
+ +
+{% endblock %} \ No newline at end of file diff --git a/app/templates/stats.html b/app/templates/stats.html index d6ae270..5518a47 100644 --- a/app/templates/stats.html +++ b/app/templates/stats.html @@ -1,6 +1,6 @@ {% extends "layout.html" -%} {% set active_page = "stats" -%} -{% block content %} +{% block container %}

Stats bruh

TOP 4

{% endblock %} \ No newline at end of file diff --git a/app/views.py b/app/views.py index 6bf3794..879019e 100644 --- a/app/views.py +++ b/app/views.py @@ -1,8 +1,9 @@ -from flask import url_for, render_template, abort +from flask import url_for, render_template, abort, redirect, request from flask.ext.login import current_user, login_required from datetime import datetime -from app import app +from app import app, db +from forms import OrderForm from models import Order @@ -10,7 +11,7 @@ from models import Order @app.route('/') def home(): if not current_user.is_anonymous(): - orders = Order.query.filter(Order.stoptime > datetime.now()).all() + orders = Order.query.filter((Order.stoptime > datetime.now()) | (Order.stoptime == None)).all() return render_template('home_loggedin.html', orders=orders) return render_template('home.html') @@ -34,6 +35,28 @@ def order(id): return render_template('order.html', order=order) return abort(404) +@app.route('/order/create', methods=['GET', 'POST']) +@login_required +def order_create(): + orderForm = OrderForm() + orderForm.populate() + if orderForm.validate_on_submit(): + order = Order() + orderForm.populate_obj(order) + db.session.add(order) + db.session.commit() + print(order.id) + return redirect(url_for('home')) + + return render_template('order_form.html', form=orderForm) + +@app.route('/order') +@login_required +def orders(): + orders = Order.query.filter((Order.stoptime > datetime.now()) | (Order.stoptime == None)).all() + return render_template('orders.html', orders=orders) + + if app.debug: # add route information @app.route('/routes') @login_required