-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
31 lines (26 loc) · 914 Bytes
/
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
import os
import datetime
from flask import Flask, render_template, request
from pymongo import MongoClient
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
client = MongoClient(os.environ.get("MONGODB_URI"))
app.db = client.microblog
@app.route("/", methods=["POST", "GET"])
def home():
if request.method == "POST":
entry_content = request.form.get("content")
formatted_date = datetime.datetime.today().strftime("%Y- %m- %d")
app.db.entries.insert_one({"content": entry_content, "date": formatted_date})
entries_with_date = [
(
entry["content"],
entry["date"],
datetime.datetime.strptime(entry["date"], "%Y- %m- %d").strftime("%b %d")
)
for entry in app.db.entries.find({})
]
return render_template("home.html", entries=entries_with_date)
if __name__ == "__main__":
app.run(debug=True)