-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.original.py
185 lines (131 loc) · 5.82 KB
/
main.original.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from flask import Flask, render_template, request, redirect, url_for, make_response, session
from google.auth.transport import requests
from extensions.my_firestore import store_post, retrieve_all_posts, retrieve_user, get_post, delete_post
import google.oauth2.id_token
app = Flask(__name__)
#
def userSignedIn():
id_token = request.cookies.get("token")
# print(f"id_token: {id_token}")
error_message = None
claims = None
authenticated = False
if id_token:
try:
claims = google.oauth2.id_token.verify_firebase_token(
id_token, firebase_request_adapter
)
authenticated = retrieve_user(claims.get('user_id'))
except ValueError as exc:
error_message = str(exc)
print(f"authenticated: {authenticated}")
return {"error_message": error_message, "claims" : claims, "authenticated": authenticated}
# Home page. If logged in, display both public and private posts as well as edit icons
firebase_request_adapter = requests.Request()
@app.route('/')
def home():
# Verify Firebase auth.
verified = userSignedIn()
raw_posts = retrieve_all_posts()
rendered_posts = list()
for raw_post in raw_posts:
id = raw_post.id
raw_post = raw_post.to_dict()
if raw_post["post"] == None:
post = "Empty Post"
# elif len(raw_post["post"]) > 250:
# post = raw_post["post"][:250] + "...[truncated]"
else: post = raw_post["post"]
title = raw_post.get("title", "Empty Title")
date = raw_post.get("date").strftime("%a, %d %b %Y")
private = raw_post.get("private", "false")
image = raw_post.get("image", "")
# print(f"Title: {title}")
# print(f"Image: {image}")
post_ready = {"title": title, "post": post, "id": id, "date": date, "private": private, "image": image}
if private and verified.get("authenticated"):
# print(f"private: {private}, verified: {verified.get("authenticated")}")
rendered_posts.append(post_ready)
elif not private:
rendered_posts.append(post_ready)
resp = make_response(render_template("index.html", user_data=verified["claims"], posts=rendered_posts))
if verified.get('authenticated'):
return resp
else:
resp.delete_cookie(key='token')
return resp
# submit post page
@app.route("/post", methods=["GET", "POST"])
def post():
print("Navigating to /post")
verified = userSignedIn()
if request.method == "POST" and verified.get('authenticated'):
# POST
print("/post 'POST' request")
# get the title and post from the form submission
title = request.form.get("title")
post = request.form.get("post")
private = request.form.get("private")
user_id = verified["claims"]["user_id"]
post_id = request.form.get("post_id", None)
# if (post_id == None):
# post_id = ""
if not post_id == "":
print(f"post_id: {post_id}")
print(f"Post private: {private}")
print(f"title: {title}")
print(f"post: {post}")
if private == "on":
private_ret = "checked"
private = True
else:
private_ret = ""
private = False
ret_post = {"post": post, "title": title, "private": private_ret}
show_alert = title == "" or post == ""
print(f"show_alert Status: {show_alert}")
if not show_alert:
store_post({"title": title, "post": post, "private": private, "post_id": post_id}, user_id)
resp = make_response(render_template("post.html", post=ret_post, show_alert=show_alert, user_data=verified["claims"], edit=show_alert))
elif request.method == "GET" and verified.get('authenticated'):
# "Authenticated GET"
edit = request.args.get("edit")
ret_post = {"title": "","post": "", "private": ""}
resp = make_response(render_template("post.html", post=ret_post, show_alert=False, user_data=verified["claims"], edit=edit))
else:
# "Unauthenticated GET response"
resp = make_response(redirect("/"))
return resp
# edit/read post
@app.route("/post/<post_id>", methods=["GET"])
def view_post(post_id):
verified = userSignedIn()
authenticated = verified.get('authenticated')
print(f"/post/{post_id}. Authenticated: {authenticated}")
complete_post = get_post(post_id)
post_id = complete_post.id
complete_post = complete_post.to_dict()
complete_post["post_id"] = post_id
edit = False
private = False
if (complete_post["private"]):
complete_post["private"] = "checked"
private = True
else:
complete_post["private"] = ""
if request.args.get("edit") == "true" and authenticated:
edit = True
print(f"Edit: {edit}")
if private and authenticated:
return render_template("post.html", post=complete_post, show_alert=None, user_data=verified["claims"], edit=edit)
else:
return render_template("post.html", post=complete_post, show_alert=None, user_data=verified["claims"], edit=edit)
@app.route("/post/<post_id>/delete", methods=["POST"])
def dele_post(post_id):
verified = userSignedIn()
print(f"DELETE. post_id: {post_id}")
if request.method == "POST" and verified.get("authenticated"):
delete_post(post_id)
return redirect("/")
if __name__ == "__main__":
app.run(host="127.0.0.1", port=8081, debug=True)