-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
217 lines (159 loc) · 5.43 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
from flask import Flask, render_template, request, Response, redirect, session, url_for, g, send_file, jsonify, flash
from glob import glob
import os.path
import json
import hashlib
from werkzeug.utils import secure_filename
from database import CheckCredentials, AddUser, AddGroup, AddNewEvent, subscribeGroup, updateFreetime, addTaskToDB, fetchEventsFromDB, updateGoingStatusToDB
app = Flask(__name__)
app.secret_key = os.urandom(24)
isUser = True
@app.before_request
def before_request():
g.user = None
if 'user' in session:
g.user = session['user']
@app.route('/', methods=['GET'])
def main():
if not g.user:
return render_template('index.html')
if (isUser):
return redirect(url_for('profile'))
else:
return redirect(url_for('group'))
@app.route('/login', methods=['GET', 'POST'])
def login():
global isUser
if g.user:
return main()
if request.method == 'POST':
s = request.form.to_dict()['login_info']
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
h = hashlib.md5(d['password'].encode())
hashed_password = h.hexdigest()
checkResult = CheckCredentials(d['username'], hashed_password);
if (checkResult == "False"):
return ("False")
session['user'] = d['username']
if (checkResult == 'user'):
isUser = True
return ("True")
if (checkResult == 'group'):
isUser = False
return ("True")
print(isUser)
##returns template if its not a POST
return render_template('index.html')
@app.route('/createUser', methods=['GET', 'POST'])
def createUser():
s = request.form.to_dict()['json_string']
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
h = hashlib.md5(d['password'].encode())
hashed_password = h.hexdigest()
if AddUser(d['username'], hashed_password):
##starts session with this user
session['user'] = d['username']
else:
return "False"
return render_template('index.html')
@app.route('/createGroup', methods=['GET', 'POST'])
def createGroup():
s = request.form.to_dict()['json_string']
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
h = hashlib.md5(d['password'].encode())
hashed_password = h.hexdigest()
if AddGroup(d['username'], hashed_password):
##starts session with this user
session['user'] = d['username']
else:
return "False"
return render_template('index.html')
@app.route('/createNewEvent', methods=['GET', 'POST'])
def createNewEvent():
s = request.form.to_dict()['json_string']
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
if AddNewEvent(g.user, d['title'], d['timeFrom'], d['timeTo'], d['date'], d['address']):
return "True"
else:
return "False"
@app.route('/profile', methods=['GET', 'POST'])
def profile():
if g.user and isUser:
return render_template('profile.html', user=g.user)
return main()
@app.route('/group', methods=['GET', 'POST'])
def group():
if g.user and not isUser:
return render_template('addEvent.html', user=g.user)
return main()
@app.route('/logout')
def logout():
session.pop('user', None)
return redirect(url_for('login'))
# @app.route('/user/images')
# def getCurrentUserImages():
# """Gets all images that belong to current user
#
# Returns:
# str: JSON list of image URLs
# """
# if not g.user:
# return redirect(url_for('login'))
# return getUserImages(g.user)
# @app.route('/user/profile')
# def getCurrentUserProfile():
# if not g.user:
# return redirect(url_for('login'))
# return getUserProfile(g.user)
# @app.route('/getcurrentuser', methods=["GET"])
# def getCurrentUser():
# """Gets current logged in user
#
# Returns:
# str: JSON object containing the current user
# """
# return jsonify({
# 'username': g.user
# })
@app.route('/subscribe', methods=["POST"])
def subscribe():
s = request.form.to_dict()['json_string']
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
group = d['groupID']
subscribeGroup(g.user, group)
return "None"
@app.route('/setFreetime', methods=["POST"])
def setFreetime():
s = request.form.to_dict()['json_string']
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
freetime = d['freetime']
updateFreetime(g.user, freetime)
return "None"
@app.route('/addTask', methods=["POST"])
def addTask():
s = request.form.to_dict()['json_string']
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
taskName = d['taskName']
taskDuration = d['taskDuration']
addTaskToDB(g.user, taskName, taskDuration)
return "None"
@app.route('/fetchEvents', methods=["GET"])
def fetchEvents():
return fetchEventsFromDB(g.user)
@app.route('/updateGoingStatus', methods=["POST"])
def updateGoingStatus():
s = request.form.to_dict()['json_string']
json_acceptable_string = s.replace("'", "\"")
d = json.loads(json_acceptable_string)
eventID = d['eventID']
going = d['going']
return updateGoingStatusToDB(g.user, eventID, going)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=True)