-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
168 lines (140 loc) · 5.23 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
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
from bottle import get, redirect, request, response, static_file, view, run, default_app
import sqlite3
import jwt
import globals
import random
from crud import (
create_user,
post_tweet,
delete_tweet,
edit_tweet,
login,
profile,
create_profile_img,
)
# get static files
@get("/app.css")
def _():
return static_file("app.css", root=".")
@get("/js/main.js")
def _():
return static_file("/js/main.js", root=".")
@get("/js/profile.js")
def _():
return static_file("/js/profile.js", root=".")
@get("/js/validator.js")
def _():
return static_file("/js/validator.js", root=".")
@get("/img/<image>")
def _(image):
return static_file(image, root="./img")
# routes
@get("/")
@view("index")
def _():
"""
View function for handling the root URL ("/").
This function fetches tweets from the database, along with user information, and prepares data
to be displayed in the index view. It checks if the user is logged in based on the presence of
a valid user token. If the user is logged in, it retrieves the user's details and profile image.
Otherwise, it sets the user as a guest with a default profile image.
Returns:
dict: A dictionary containing the following keys and values:
- "tweets" (list of dicts): A list of dictionaries containing tweet data fetched from the database.
- "logged_user" (str): The name of the logged-in user or "guest" if not logged in.
- "trends" (list): A list of trending topics or keywords.
- "logged_img" (str): The image URL of the logged-in user's profile or a default blank image.
- "suggested_user" (list of dicts): A list of randomly selected tweets to display in the suggested user panel.
Raises:
Exception: If there is an error during database query execution.
Note:
- The function uses a random sampling algorithm to select tweets for the suggested user panel.
- The function uses the globals module to access global variables.
"""
user_token = request.get_cookie("token")
tweets = []
logged_user = "guest"
left_panel_img = "blank.png"
suggested_user = []
try:
db = globals._db_connect("database.sqlite")
tweets = db.execute(
"""SELECT * FROM tweets
JOIN users ON users.user_id = tweets.user_id
ORDER BY tweet_created_at DESC
"""
).fetchall()
if user_token:
user_token_bytes = user_token.encode("utf-8")
decoded_token = jwt.decode(user_token_bytes, "mysecret", algorithms="HS256")
logged_user = decoded_token["name"]
print("Token valid", f"User {logged_user} is logged in")
logged_user_img = db.execute(
globals.GET_LOGGED_USER_IMG_QUERY, (logged_user,)
).fetchone()
left_panel_img = logged_user_img["user_image"]
else:
print("Not logged in")
suggested_user = random.sample(tweets, k=4)
except Exception as ex:
print(ex)
finally:
db.close()
return dict(
tweets=tweets,
logged_user=logged_user,
trends=globals.TRENDS,
logged_img=left_panel_img,
suggested_user=suggested_user,
)
# signup route
@get("/signup")
@view("signup")
def _():
return
# login route
@get("/login")
@view("login")
def _():
return
# logout route
@get("/logout")
def _():
"""
View function for handling the "/logout" URL.
This function is responsible for logging out the user. It first retrieves the user's token from the request's cookies,decodes the token to obtain the user's session information.
It then checks if the session ID from the token is present in the sessions database table and deletes the corresponding row on logout. After successful logout, it clears the token
cookie to log the user out of the system.
Returns:
bottle.HTTPResponse: A redirect response to the root URL ("/") after successful logout.
Raises:
bottle.HTTPResponse: If there is a server error (status code 500) during the logout process.
Note:
- The function relies on external database queries defined in the "globals" module.
- It also uses the "jwt" module to decode the user token.
"""
db = sqlite3.connect("database.sqlite")
try:
user_token = request.get_cookie("token")
decoded_token = jwt.decode(user_token, "mysecret", algorithms="HS256")
print("TOKEN IS", decoded_token["name"])
# check if token id in sessions, and delete that row from sessions on logout
db.execute(
globals.DELETE_SESS_ROW_QUERY, (decoded_token["session_id"],)
).fetchone()
db.commit()
print("LOGGING OUT", decoded_token["session_id"])
response.set_cookie("token", user_token, expires=0)
except Exception as ex:
print(ex)
return globals._send(500, "server_error")
finally:
db.close()
return redirect("/")
try:
import production
application = default_app()
print("***PRODUCTION***")
except Exception as ex:
print("***Server running on development***")
run(host="127.0.0.1", port=3999, debug=True, reloader=True, server="paste")