-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
247 lines (208 loc) · 9.79 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
from flask import Flask, render_template, request, redirect, session
from core.models import *
from core.helpers import *
import os
from datetime import datetime, timedelta
import validators
from dotenv import load_dotenv
load_dotenv()
SECRET = os.getenv('SECRET')
project = Flask(__name__)
project.secret_key = SECRET
FORBIDDEN_URLS = ["user", "login", "logout", "register", "user/", "login/", "logout/", "register/", "/user", "/login", "/logout", "/register", "/user/", "/login/", "/logout/", "/register/"]
@project.context_processor
def inject_today_date():
return {'now': datetime.now()}
@project.route('/', methods=['POST', 'GET'])
def home():
if request.method == "POST":
print("Debug: Post Request here.")
data = request.form
long_url = data['long_url']
try:
custom_slug = data['custom_slug']
except:
custom_slug = ""
if long_url != "":
if long_url[0:7] != "http://" and long_url[0:8] != "https://":
print(long_url[0:7])
long_url = "http://" + long_url
if not validators.url(long_url):
message = "Invalid URL!"
return render_template('home.html', message=message)
else:
if "kata-flask.herokuapp.com" in long_url:
message = "Cannot shorten URLs from this domain."
return render_template('home.html', message=message)
else:
print("Debug: Recieved Long URL")
if custom_slug != "":
if custom_slug in FORBIDDEN_URLS or "kata-flask.herokuapp.com" in custom_slug:
message = "Invalid custom alias."
return render_template('home.html', message=message)
else:
if check_custom_slug_availability(custom_slug):
short_url = custom_slug
else:
message = "Custom URL already taken."
return render_template('home.html', message=message)
else:
short_url = get_short_url()
print("Debug: Recieved Short URL")
print(f"User sent: {long_url}")
print(f"Short URL: {short_url}")
print(session)
if "user" in session:
print("User is logged in. Fetching user email.")
user_email = (session_collection.find_one({"_id": session["user"]}))["email"]
new_url = {
"user": user_email,
"long_url": long_url,
"short_url": short_url,
"click_count": 0
}
else:
new_url = {
"long_url": long_url,
"short_url": short_url,
"click_count": 0
}
url_collection.insert_one(new_url)
return render_template('home.html', short_url=short_url, long_url=long_url)
else:
message = "Please enter a url!"
return render_template('home.html', message=message)
else:
return render_template('home.html')
@project.route('/user/', methods=['GET'])
def user():
if "user" in session:
user_email = (session_collection.find_one({"_id": session["user"]}))["email"]
user_urls = url_collection.find({"user": user_email})
user_urls = list(user_urls)
print(f"Showing user page for user: {user_email}.")
return render_template('profile.html', user_email=user_email, user_urls=user_urls)
else:
message = "User not logged in. Please login first."
return render_template('login.html', message=message)
@project.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == "POST":
print("POST Request mili")
if 'user' in session:
user_email = (session_collection.find_one({"_id": session["user"]}))["email"]
print(f"User email in session: {user_email}")
message = f"User - {user_email} already logged in."
return render_template('home.html', message=message)
else:
data = request.form
email = data['email']
password = data['password']
if email != "" and password != "":
print(f"User Email: {email}")
print(f"User Password: {password}")
user = user_collection.find_one({"email": email})
if user != None:
print(f"User: {email} found in database.")
if verify_password(user['password'], password):
print("Their passwords matched too.")
message = "Logged in Successfully."
status = "success"
print("Now doing some stuff for session.")
# TODO some stuff here for SESSION
print(f"Now setting the session cookie for the user: {email} ")
new_session = {
"_id": get_unique_session_id(),
"domain": "127.0.0.1",
"email": f"{email}",
"set_date": f"{datetime.now()}",
"expiration": f"{datetime.now()+timedelta(days=1)}"
}
session_collection.insert_one(new_session)
session["user"] = new_session["_id"]
print(f"The system generated session_id: {session['user']} for the user: {email}")
return render_template('home.html', message=message, status=status)
else:
message = "Incorrect Password"
return render_template('login.html', message=message)
else:
message = "User Not Found."
return render_template('login.html', message=message)
else:
message = "Please enter email and password."
return render_template('login.html', message=message)
else:
print("GET Request mili")
if 'user' in session:
user_email = (session_collection.find_one({"_id": session["user"]}))["email"]
print(f"User email in session: {user_email}")
message = f"User - {user_email} already logged in."
return render_template('home.html', message=message)
else:
return render_template('login.html')
@project.route('/logout/', methods=['GET', 'POST'])
def logout():
if 'user' in session:
user = session["user"]
user_email = (session_collection.find_one({"_id": session["user"]}))["email"]
print(f"User email in session: {user_email}")
session_collection.delete_one({"_id": f"{user}"})
session.pop('user', None)
message = "User logged out successfully."
status = "success"
print(f"User - {user_email} logged out.")
return render_template('home.html', message=message, status=status)
else:
message = "User not logged in."
return render_template('login.html', message=message)
@project.route('/register/', methods=['GET', 'POST'])
def register():
if request.method == "POST":
print("POST Request mili")
data = request.form
email = data['email']
password = data['password']
if email != "" and password != "":
print(f"User Email: {email}")
print(f"User Password: {password}")
existing_user = user_collection.find_one({"email": email})
if not existing_user:
new_user = {
"_id": get_unique_user_id(),
"email": email,
"password": get_password_hash(password),
"authenticated":False
}
user_collection.insert_one(new_user)
message = "Account Created Successfully."
status = "success"
return render_template('login.html', message=message, status=status)
else:
message = "Account already exists."
return render_template('register.html', message=message)
else:
message = "Please enter email and password."
return render_template('register.html', message=message)
else:
print("GET Request mili on login page.")
return render_template('register.html')
@project.route('/privacy/', methods=['GET'])
def privacy():
return render_template('privacy.html')
@project.route('/about/', methods=['GET'])
def about():
return render_template('about.html')
FORBIDDEN_URLS = ["user", "login", "logout", "register", "privacy", "about", "user/", "login/", "logout/", "register/", "privacy/", "about/", "/user", "/login", "/logout", "/register", "/privacy", "/about", "/user/", "/login/", "/logout/", "/register/", "/privacy/", "/about/"]
@project.route('/<string:short_url>/', methods=['GET'])
def transport(short_url):
if short_url == "":
return render_template('home.html')
url = url_collection.find_one({"short_url": short_url})
if url != None:
url_collection.update_one({"short_url": short_url}, { "$inc": {"click_count": 1}})
return redirect(url['long_url'])
else:
message = "Sorry, the requested URL was not found."
return render_template('home.html', message=message)
if __name__ == "__main__":
project.run(debug=True)