-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.py
188 lines (148 loc) · 5.39 KB
/
web.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Bits & Books - A bookstore built with Flask
# Author: Ryan McGowan
#
# Version:
version = "0.1.1"
import os
from model.database import init_db
from helper.forms import LoginForm, RegistrationForm, SearchForm
from model.models import User
from helper.model_helper import ModelHelper
from helper.user_manager import login_manager, check_login
from functions.utils import debug_str
from functions.config import read_system_config, init_logging
from flask import (Flask, send_from_directory, render_template, redirect,
request, url_for, flash)
from flaskext.login import (login_user, login_required, logout_user,
current_user)
from sys import argv
#Create the app
app = Flask(__name__)
#Run various initilization functions.
read_system_config(app, argv)
init_logging(app)
db = init_db(app)
mhelper = ModelHelper(app, db)
#Required by Flask-Login extension
login_manager.setup_app(app)
#Always send the logged_in/is_admin status to a template
@app.context_processor
def inject_user():
"""Tell the view whether someone is logged in or not"""
return {'logged_in': current_user.is_active(),
'is_admin': current_user.is_active() and current_user.is_admin()}
#For browsers that do not support the link tag for favicon
@app.route('/favicon.ico')
def favicon():
"""Reroute requests to the favicon to the correct location in static."""
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/')
def index():
"""Render the site homepage, which is also the search page."""
form = SearchForm()
search = request.args.get("search")
results = None
if search:
results = mhelper.get_search_results(search)
return render_template("index.html", form=form, results=results)
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect(request.args.get("next") or url_for("index"))
@app.route('/login', methods=["GET", "POST"])
def login():
if not current_user.is_active():
to_page = request.args.get("next")
if to_page == url_for("logout"):
to_page = False
form = LoginForm()
if form.is_submitted():
if form.validate():
user = check_login(form.username.data, form.password.data)
if user:
login_user(user, form.remember_me.data)
flash("Logged in successfully.", "success")
return redirect(to_page or url_for("index"))
else:
flash("Invalid Username/Password", "error")
else:
for key, msg in form.errors.items():
flash("Could not validate " + key + ": " + ", ".join(msg),
"error")
return render_template("login.html", form=form)
else:
flash("You are already logged in!", "info")
return redirect(request.args.get("next") or url_for("index"))
@app.route('/register', methods=["GET", "POST"])
def register():
if not current_user.is_active():
to_page = request.args.get("next")
if to_page == url_for("logout"):
to_page = False
form = RegistrationForm()
if form.is_submitted():
if form.validate():
try:
db.session.add(User(form.email.data, form.username.data,
form.password.data, form.name.data, form.phone.data))
db.session.commit()
flash("Congratualtions, " + (form.name.data or
form.username.data) + "! You have registered\
successfully!", "success")
return redirect(to_page or url_for("index"))
except:
if not app.debug:
flash("We're sorry, we were unable to register you.\
Feel free to try again.", "error")
else:
raise
else:
for key, msg in form.errors.items():
flash("Could not validate " + key + ": " + ", ".join(msg),
"error")
return render_template("register.html", form=form)
else:
flash("You are already logged in!", "info")
return redirect(request.args.get("next") or url_for("index"))
@app.route('/view/history')
def view_history():
pass
@app.route('/view/order/<int:order_id>')
def view_order():
pass
@app.route('/view/cart')
def view_cart():
pass
@app.route('/view/profile')
@app.route('/view/profile/<int:user_id>')
def edit_profile():
users = User.get().all()
return debug_str(users)
@app.route('/add/book/<int:book_id>')
def add_book_to_cart():
pass
@app.route('/create/book')
def create_book():
pass
@app.route('/view/inventory/<int:inventory_id>')
def view_inventory():
pass
@app.route('/view/inventory_order/<int:inventory_order_id>')
def view_inventory_order():
pass
@app.route('/create/inventory_order')
def create_inventory_order():
pass
def run_application():
if app.is_production:
address = "0.0.0.0"
port = int(os.environ.get("PORT", 5000))
else:
address = "127.0.0.1"
port = int(os.environ.get("PORT", 5000))
app.logger.info("Starting Bits & Books " + version)
app.run(address, port)
if __name__ == '__main__':
run_application()