-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
145 lines (133 loc) · 4.49 KB
/
app.py
File metadata and controls
145 lines (133 loc) · 4.49 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
143
144
145
from flask import Flask, render_template, make_response, request as rq, redirect
from dotenv import load_dotenv as LoadEnvVariables
from os import environ
from utils import MakeRequest, ListPossibleStartYears
from base64 import b64encode
from db import NPCS
from main import *
from threading import Thread
LoadEnvVariables()
app = Flask(__name__)
db = NPCS()
app.config['SECRET_KEY'] = environ.get("FLASK_KEY")
socketio.init_app(app)
client_auth = f"{environ.get('OAUTH_CLIENT_ID')}:{environ.get('OAUTH_CLIENT_SECRET')}"
client_auth = b64encode(client_auth.encode()).decode()
def addNotionAuth(res):
db.get_table("Person")._Create({
"homepage": res["duplicated_template_id"],
"person_id": res["owner"]["user"]["id"],
"start_year": GetAcademicYear()
})
db.get_table("NotionApp")._Create({
"access_token": res["access_token"],
"token_type": res["token_type"],
"bot_id": res["bot_id"],
"person_id": res["owner"]["user"]["id"]
})
db.get_table("NotionWorkspace")._Create({
"name": res["workspace_name"],
"icon": res["workspace_icon"],
"workspace_id": res["workspace_id"],
"bot_id": res["bot_id"]
})
db._EndTransaction()
@app.route('/', methods = ["GET"])
def index():
variables = {"loggedIn": False}
# Check if a person is logged in
notionID = rq.cookies.get('notionID', "")
if not notionID:
output = make_response(render_template("index.html", **variables))
return output
variables["loggedIn"] = True
person = db.get_table("Person")._Retrieve({"person_id": notionID})[0]
variables["startYears"] = ListPossibleStartYears()
variables["currentStartYear"] = person["start_year"]
# Get all modules
modules = db.get_table("Modules")._Retrieve({"person_id": notionID})
variables["modules"] = {}
for module in modules:
key = AcaYearToText(module["year"], person["start_year"])
module_data = {
"moduleID": module['module_id'],
"pushed": module['pushed'],
"moduleNotionID": module['module_notion_id'].replace("-", "") if module["module_notion_id"] else None
}
variables["modules"].setdefault(key, []).append(module_data)
variables["homepage"] = person['homepage'].replace("-","")
print(variables)
output = make_response(render_template("index.html", **variables))
db._EndTransaction()
return output
@app.route('/newcourse', methods = ["POST"])
def newcourse():
notionID = rq.cookies.get('notionID', "")
if not notionID:
return redirect("/")
form = rq.form.to_dict()
pushed = 1 if form.get("push", False) else 0
db.get_table("Modules")._Create({
"module_id": form["code"],
"year": GetAcademicYear(),
"pushed": pushed,
"person_id":notionID
})
if pushed:
Thread(
target=ParseModules,
args=([form["code"]], db, notionID),
daemon=True
).start()
db._EndTransaction()
return redirect("/")
@app.route('/notioned', methods = ["GET"])
def notioned():
code = rq.args.get('code', default = "", type = str)
res = MakeRequest(
"POST",
"https://api.notion.com/v1/oauth/token",
"OAuth2 request",
data={
"grant_type":"authorization_code",
"code":code,
"redirect_uri":"https://notionpoolcs.onrender.com/notioned"
},
headers={
"Authorization": f"Basic {client_auth}",
"Content-Type": "application/json"
}
)
addNotionAuth(res)
output = make_response(redirect("/"))
output.set_cookie("notionID", res["owner"]["user"]["id"])
return output
@app.route('/pushcourse', methods = ["POST"])
def pushcourse():
notionID = rq.cookies.get('notionID', "")
if not notionID:
return redirect("/")
form = rq.form.to_dict()
Thread(
target=ParseModules,
args=([form["code"]], db, notionID),
daemon=True
).start()
return redirect("/")
@app.route('/changestartyear', methods = ["POST"])
def changestartyear():
notionID = rq.cookies.get('notionID', "")
if not notionID:
return redirect("/")
form = rq.form.to_dict()
db.get_table("Person")._Update({
"start_year":form["startYear"]
},{"person_id": notionID})
return redirect("/")
if __name__ == "__main__":
# from test import sampleRes
# addNotionAuth(sampleRes)
socketio.run(
app=app,
debug=True
)