-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.py
75 lines (57 loc) · 2.39 KB
/
routes.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
import datetime
import uuid
from flask import Blueprint, render_template, request, redirect, url_for, current_app
pages = Blueprint("habits", __name__, template_folder="templates", static_folder="static")
@pages.context_processor
def add_calc_date_range():
def date_range(start: datetime.datetime):
dates = [start + datetime.timedelta(days=diff) for diff in range(-3, 4)]
return dates
return {"date_range": date_range}
def today_at_midnight():
today = datetime.datetime.today()
# by default time, min, sec are 0 - midnight
return datetime.datetime(today.year, today.month, today.day)
@pages.route("/")
def index():
date_str = request.args.get("date")
if date_str:
selected_date = datetime.datetime.fromisoformat(date_str)
else:
selected_date = today_at_midnight()
# find 'added' property, and then pass a query, that has a key of $lte less than or equal to
# then we're passing the 'selected_data' as the value, it give us all habits
# they added date either today or earlier
habits_on_date = current_app.db.habits.find({"added": {"$lte": selected_date}})
# read habit completions from MongoDB
completions = [habit["habit"] for habit in current_app.db.completions.find({"date": selected_date})]
return render_template(
"index.html",
habits=habits_on_date,
completions=completions,
title="Habit Tracker - Home",
selected_date=selected_date,
)
@pages.route("/add", methods=["GET", "POST"])
def add_habit():
today = today_at_midnight()
if request.form:
current_app.db.habits.insert_one(
{"_id": uuid.uuid4().hex, "added": today, "name": request.form.get("habit")}
)
return render_template(
"add_habit.html",
title="Habit Tracker - Add Habit",
selected_date=today
)
@pages.post("/complete")
def complete():
date_string = request.form.get("date")
date = datetime.datetime.fromisoformat(date_string)
# habitId linking two collections in MongoDB
habit = request.form.get("habitId")
# adding a new completion to MongoDB
current_app.db.completions.insert_one({"date": date, "habit": habit})
# With url_for say habits.index because accessing a sub route of the blueprint
# For two endpoints in the same blueprint you can do habits.index = .index
return redirect(url_for(".index", date=date_string))