generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
97 lines (80 loc) · 2.39 KB
/
app.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
FreeFrom Application Python module
Version 1.1.1
"""
# Import dependencies
import os
from bson import errors
from flask import (Flask, render_template)
# Import Blueprints
from userauth import userauth
from products import products
from allergens import allergens
from categories import categories
from mail import mail
# Import PyMongo database instance
from database import mongo
# Import environment variables, if running locally
if os.path.exists("env.py"):
import env
# Initiate instance of Flask application
app = Flask(__name__)
# Blueprints
# Blueprint for user authentication
app.register_blueprint(userauth)
# Blueprint for products
app.register_blueprint(products)
# Blueprint for allergens
app.register_blueprint(allergens)
# Blueprint for categories
app.register_blueprint(categories)
# Blueprint for contact
app.register_blueprint(mail)
# Configure database access variables
app.config["MONGO_DBNAME"] = os.environ.get("MONGO_DBNAME")
app.config["MONGO_URI"] = os.environ.get("MONGO_URI")
app.secret_key = os.environ.get("SECRET_KEY")
# Initiate instance of PyMongo
mongo.init_app(app)
@app.route("/")
def home():
"""
Route for home
"""
# Get categories collection from database
categories = mongo.db.categories.find().sort("name", 1)
# Get allergens collection from database
allergens = mongo.db.allergens.find().sort("name", 1)
if categories and allergens:
return render_template(
"home.html", categories=categories, allergens=allergens)
else:
print("Could not connect to the Mongo DB")
@app.errorhandler(Exception)
def error_generic(e):
"""
Generic error handler
"""
errstr = "something went wrong"
return render_template('error.html', error=errstr), 500
@app.errorhandler(404)
def error_page_not_found(e):
"""
Error handler for page not found
"""
errstr = "looks like you've lost your way"
return render_template('error.html', error=errstr), 404
@app.errorhandler(errors.InvalidId)
def error_invalid_id(e):
"""
Error handler for invalid bson id
"""
errstr = "product not found"
return render_template('error.html', error=errstr), 500
if __name__ == "__main__":
app.run(host=os.environ.get("IP"),
port=int(os.environ.get("PORT")),
# Note - change to debug=True for running locally
debug=False)