-
Notifications
You must be signed in to change notification settings - Fork 37
/
app.py
140 lines (118 loc) · 4.41 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
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
#!/usr/bin/env python3
import os
import datetime
from flask import Flask, render_template, request, redirect, url_for
import pymongo
from bson.objectid import ObjectId
from dotenv import load_dotenv
load_dotenv() # load environment variables from .env file
def create_app():
"""
Create and configure the Flask application.
returns: app: the Flask application object
"""
app = Flask(__name__)
cxn = pymongo.MongoClient(os.getenv("MONGO_URI"))
db = cxn[os.getenv("MONGO_DBNAME")]
try:
cxn.admin.command("ping")
print(" *", "Connected to MongoDB!")
except Exception as e:
print(" * MongoDB connection error:", e)
@app.route("/")
def home():
"""
Route for the home page.
Returns:
rendered template (str): The rendered HTML template.
"""
docs = db.messages.find({}).sort("created_at", -1)
return render_template("index.html", docs=docs)
@app.route("/create", methods=["POST"])
def create_post():
"""
Route for POST requests to the create page.
Accepts the form submission data for a new document and saves the document to the database.
Returns:
redirect (Response): A redirect response to the home page.
"""
name = request.form["fname"]
message = request.form["fmessage"]
doc = {
"name": name,
"message": message,
"created_at": datetime.datetime.utcnow(),
}
db.messages.insert_one(doc)
return redirect(url_for("home"))
@app.route("/edit/<post_id>")
def edit(post_id):
"""
Route for GET requests to the edit page.
Displays a form users can fill out to edit an existing record.
Args:
post_id (str): The ID of the post to edit.
Returns:
rendered template (str): The rendered HTML template.
"""
doc = db.messages.find_one({"_id": ObjectId(post_id)})
return render_template("edit.html", doc=doc)
@app.route("/edit/<post_id>", methods=["POST"])
def edit_post(post_id):
"""
Route for POST requests to the edit page.
Accepts the form submission data for the specified document and updates the document in the database.
Args:
post_id (str): The ID of the post to edit.
Returns:
redirect (Response): A redirect response to the home page.
"""
name = request.form["fname"]
message = request.form["fmessage"]
doc = {
"name": name,
"message": message,
"created_at": datetime.datetime.utcnow(),
}
db.messages.update_one({"_id": ObjectId(post_id)}, {"$set": doc})
return redirect(url_for("home"))
@app.route("/delete/<post_id>")
def delete(post_id):
"""
Route for GET requests to the delete page.
Deletes the specified record from the database, and then redirects the browser to the home page.
Args:
post_id (str): The ID of the post to delete.
Returns:
redirect (Response): A redirect response to the home page.
"""
db.messages.delete_one({"_id": ObjectId(post_id)})
return redirect(url_for("home"))
@app.route("/delete-by-content/<post_name>/<post_message>", methods=["POST"])
def delete_by_content(post_name, post_message):
"""
Route for POST requests to delete all post by their author's name and post message.
Deletes the specified record from the database, and then redirects the browser to the home page.
Args:
post_name (str): The name of the author of the post.
post_message (str): The contents of the message of the post.
Returns:
redirect (Response): A redirect response to the home page.
"""
db.messages.delete_many({"name": post_name, "message": post_message})
return redirect(url_for("home"))
@app.errorhandler(Exception)
def handle_error(e):
"""
Output any errors - good for debugging.
Args:
e (Exception): The exception object.
Returns:
rendered template (str): The rendered HTML template.
"""
return render_template("error.html", error=e)
return app
if __name__ == "__main__":
FLASK_PORT = os.getenv("FLASK_PORT", "5000")
app = create_app()
app.run(port=FLASK_PORT)