-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
142 lines (119 loc) · 5.82 KB
/
app.py
File metadata and controls
142 lines (119 loc) · 5.82 KB
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
import json
from json.decoder import JSONDecoder
from flask import Flask, render_template, request, redirect, send_from_directory
import requests
from types import SimpleNamespace, resolve_bases
import random
import os
app = Flask(__name__)
hostURL = "https://startupguidedeveloper.herokuapp.com"
loggedInUsers = {}
accessToken = ""
refreshToken = ""
class user():
def __init__(self,username, email, accessToken, refreshToken) -> None:
self.username = username
self.email = email
self.accessToken = accessToken
self.refreshToken = refreshToken
class item (object):
def __init__(self, _id,name,category, description, link, thumbnail ) -> None:
self.name = name
self._id = _id
self.category = category
self.description = description
self.link = link
self.thumbnail = thumbnail
# def decodeObject(obj):
# if '__type__' in obj and obj['__type__'] == 'item':
# return item(obj['name'], obj['_id'], obj['category'], obj['description'], obj['link'], obj['thumbnail'])
# return obj
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/')
def connection():
return render_template('base.html')
@app.route('/authentication', methods = ['GET', 'POST'])
def authentication():
return render_template('login.html')
@app.route('/login', methods = ['GET', 'POST'])
def login():
if request.method == "POST":
data = {"email" : request.form['email'], "name" : request.form['Username'], "password" : request.form['password']}
headers = {"content-type" : "application/json"}
r = requests.post(f"{hostURL}/api/auth/login", json=data, headers = headers)
if r.status_code == 200:
newUser = user(username=request.form['Username'], email=request.form['email'], accessToken= data['accessToken'], refreshToken=data['refreshToken'])
newUserID = random.randint(1000000, 9999999)
loggedInUsers[newUserID] = newUser
return redirect(f'/homepage/{data["accessToken"]}/{newUserID}')
else :
return redirect(f"/error/{r.status_code}/{r.text}")
else :
return "Sorry this route is not accessable"
@app.route('/error/<statuscode>/<error>', methods = ['POST', 'GET'])
def error(statuscode, error):
return render_template('errorPage.html', statuscode = statuscode, error = error )
@app.route('/homepage/<accessToken>/<userID>', methods = ['GET', 'POST'])
def home(accessToken, userID):
if (loggedInUsers[int(userID)].accessToken == accessToken):
user = loggedInUsers[int(userID)]
r = requests.get(f"{hostURL}/api/getAllBooks")
books = []
for element in r.json():
newItem = item(element['_id'],element['name'],element['category'], element['description'], element['link'], element['thumbnail'])
books.append(newItem)
r = requests.get(f"{hostURL}/api/getAllBlogs")
blogs = []
for element in r.json():
newItem = item(element['_id'],element['name'],element['category'], element['description'], element['link'], element['thumbnail'])
blogs.append(newItem)
r = requests.get(f"{hostURL}/api/getAllNewsletters")
newsletters = []
for element in r.json():
newItem = item(element['_id'],element['name'],element['category'], element['description'], element['link'], element['thumbnail'])
newsletters.append(newItem)
r = requests.get(f"{hostURL}/api/getAllStartupStories")
startupStories = []
for element in r.json():
newItem = item(element['_id'],element['name'],element['category'], element['description'], element['link'], element['thumbnail'])
startupStories.append(newItem)
r = requests.get(f"{hostURL}/api/getAllYoutubeChannels")
youtubeChannels = []
for element in r.json():
newItem = item(element['_id'],element['name'],element['category'], element['description'], element['link'], element['thumbnail'])
youtubeChannels.append(newItem)
r = requests.get(f"{hostURL}/api/getAllPodcasts")
podcasts = []
for element in r.json():
newItem = item(element['_id'],element['name'],element['category'], element['description'], element['link'], element['thumbnail'])
podcasts.append(newItem)
r = requests.get(f"{hostURL}/api/getAllTweets")
tweets = []
for element in r.json():
newItem = item(element['_id'],element['name'],element['category'], element['description'], element['link'], element['thumbnail'])
tweets.append(newItem)
return render_template('Homepage.html',userID = userID ,user = user, books = books, blogs = blogs, newsletters = newsletters, startupStories = startupStories, youtubeChannels = youtubeChannels, podcasts = podcasts, tweets = tweets)
else :
return "Sorry, detected token from malicious route"
@app.route('/addItem/<token>/<userID>' ,methods = ['POST', 'GET'])
def addItem(token, userID):
if request.method == 'POST':
print(request.form['name'])
body = {
"name" : request.form['name'],
"category" : request.form['category'],
"thumbnail" : request.form['thumbnail'],
"link" : request.form['link'],
"description" : request.form['description']
}
headers = {
"content-type" : "application/json",
"authorization" : f"Bearer {token}"
}
response = requests.post(f"{hostURL}/api/add{request.form['category']}", json = body, headers=headers)
return render_template('addItemPage.html', token = token, userID = userID)
if __name__ == "__main__" :
app.run(debug=True)