-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
37 lines (30 loc) · 1.19 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
import datetime
import os
# import sys
from flask import Flask, render_template, redirect, request, url_for
from pymongo import MongoClient
from dotenv import load_dotenv
load_dotenv()
def create_app():
app = Flask(__name__)
client = MongoClient(os.environ.get("MONGODB_URI"))
app.db = client.microblog
@app.route('/', methods=["GET", "POST"])
def home():
# print([e for e in app.db.entries.find({})])
if request.method == 'POST':
entry_content = request.form.get('entry')
formatted_date = datetime.datetime.today().strftime("%b %d, %Y")
# entries.append((entry_content, formatted_date))
app.db.entries.insert_one({"content": entry_content , "date": formatted_date})
return redirect(url_for('home'))
entries_with_formatted_date = [
(
entry["content"],
entry["date"],
datetime.datetime.strptime(entry["date"],"%b %d, %Y").strftime("%Y-%m-%d")
)
for entry in app.db.entries.find({})
]
return render_template('home.html', entries=entries_with_formatted_date)
return app