-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiou.py
269 lines (220 loc) · 8.37 KB
/
iou.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import os
from flask import Flask, render_template, json, request, redirect, url_for, session, send_file
from flask_mysqldb import MySQL
import MySQLdb.cursors
import pymysql
import re
from io import BytesIO
from datetime import date, timedelta, datetime
from util.Calendar import Calendar
from util.IO import IO
app = Flask(__name__)
app.secret_key = "get dogged on"
#Change these values to ours
"""
app.config['MySQL_HOST'] = "localhost"
app.config['MySQL_USER'] = "noahf"
app.config['MySQL_PASSWORD'] = "1"
app.config['MySQL_DB'] = "IOU_DB"
"""
print(app)
# mysql = MySQL(app)
mysql = pymysql.connect(database ='IOU_DB',
host='localhost',
user='noahf',
password='1')
class calPage:
def __init__(self):
self.firstDay = date.today()
cp = calPage()
@app.route('/', methods=['GET','POST'])
def main():
msg = ''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
username = request.form['username']
password = request.form['password']
cursor = mysql.cursor() #mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM USERNAME WHERE username = %s AND password = %s', (username, password))
account = cursor.fetchone()
#Change keys to noahs database
if account:
session['LoggedIn'] = True
# session['id'] = account['id']
session['username'] = account[0]
session['firstName'] = account[1]
session['lastName'] = account[2]
return redirect(url_for('home'))
else:
msg = 'Incorrect username/password'
return render_template('index.html', msg=msg)
@app.route('/register', methods=['GET','POST'])
def register():
msg = ''
if request.method == 'POST' and 'lastName' in request.form and 'firstName' in request.form and 'username' in request.form and 'password' in request.form and 'email' in request.form:
username = request.form['username']
password = request.form['password']
email = request.form['email']
lastName = request.form['lastName']
firstName = request.form['firstName']
# Check if account exists using MySQL
cursor = mysql.cursor() #mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM USERNAME WHERE username = %s', (username,))
account = cursor.fetchone()
# If account exists show error and validation checks
if account:
msg = 'Account already exists!'
elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
msg = 'Invalid email address!'
elif not re.match(r'[A-Za-z0-9]+', username):
msg = 'Username must contain only characters and numbers!'
elif not username or not password or not email or not firstName or not lastName:
msg = 'Please fill out the form!'
else:
# Account doesnt exists and the form data is valid, now insert new account into accounts table
cursor.execute('INSERT INTO USERNAME VALUES (%s, %s, %s, %s, %s, false)', (username, firstName, lastName, email, password))
mysql.commit() #.connection.commit()
msg = 'You have successfully registered!'
elif request.method == 'POST':
msg = 'PLEASE PLEASE, do the form!!!'
return render_template('register.html', msg=msg)
@app.route('/home')
def home():
msg = ""
if 'LoggedIn' in session:
user = session['username']
try:
io = IO(user)
io.queryOweTable()
except ValueError as v:
msg = v
return render_template('home.html', username=user, msg=msg, firstName=session['firstName'])
return redirect(url_for('login'))
@app.route('/table')
def table():
# converting csv to html
msg = ""
user = session['username']
io = IO(user)
fig = io.queryOweTable()
img = BytesIO()
fig.savefig(img)
img.seek(0)
return send_file(img, mimetype='image/png')
@app.route('/tableReq')
def tableReq():
# converting csv to html
msg = ""
user = session['username']
io = IO(user)
fig = io.queryRequestTable()
img = BytesIO()
fig.savefig(img)
img.seek(0)
return send_file(img, mimetype='image/png')
@app.route('/prev')
def prev():
cp.firstDay = cp.firstDay - timedelta(days=7)
return redirect(url_for('home'))
@app.route('/next')
def next():
cp.firstDay = cp.firstDay + timedelta(days=7)
return redirect(url_for('home'))
@app.route('/prevReq')
def prevReq():
cp.firstDay = cp.firstDay - timedelta(days=7)
return redirect(url_for('viewRequests'))
@app.route('/nextReq')
def nextReq():
cp.firstDay = cp.firstDay + timedelta(days=7)
return redirect(url_for('viewRequests'))
@app.route('/fig')
def fig():
user = session['username']
cal = Calendar(user)
fig = cal.plotEvents(cp.firstDay)
img = BytesIO()
fig.savefig(img)
img.seek(0)
return send_file(img, mimetype='image/png')
@app.route('/figReq')
def figReq():
user = session['username']
cal = Calendar(user)
fig = cal.plotEvents(cp.firstDay, defaultUserName=user)
img = BytesIO()
fig.savefig(img)
img.seek(0)
return send_file(img, mimetype='image/png')
@app.route('/home/event', methods=['GET','POST'])
def event():
msg = ""
if request.method == 'POST' and 'eventName' in request.form and 'startTime' in request.form and 'endTime' in request.form and 'startDate' in request.form:
eventName = request.form['eventName']
startTime = request.form['startTime']
endTime = request.form['endTime']
startDate = request.form['startDate']
# Add to SQL db
try:
io = IO(session['username'])
io.writeNewEvent('EVENT_TABLE', eventName, startTime, endTime, startDate)
msg = 'Event Added'
except ValueError as v:
msg = v
elif request.method == 'POST':
msg = 'PLEASE PLEASE, do the form!!!'
return render_template('event.html', msg = msg)
@app.route('/home/eventRemove', methods=['GET', 'POST'])
def eventRemove():
msg = ""
if request.method == 'POST' and 'eventName' in request.form and 'startDate' in request.form:
eventName = request.form['eventName']
startDate = request.form['startDate']
# Add to SQL db
io = IO(session['username'])
io.removeEvent(eventName, startDate)
msg = 'Event Removed'
elif request.method == 'POST':
msg = 'PLEASE PLEASE, do the form!!!'
return render_template('eventRemove.html', msg = msg)
@app.route('/home/requestMake', methods=['GET', 'POST'])
def requestMake():
msg = ""
if request.method == 'POST' and 'eventName' in request.form and 'startTime' in request.form and 'endTime' in request.form and 'startDate' in request.form:
eventName = request.form['eventName']
startTime = request.form['startTime']
endTime = request.form['endTime']
startDate = request.form['startDate']
# Add to SQL db
io = IO(session['username'])
io.addRequest(startDate, startTime, endTime, eventName)
msg = 'Request Made'
elif request.method == 'POST':
msg = 'PLEASE PLEASE, do the form!!!'
return render_template('requestMake.html', msg=msg)
@app.route('/home/viewRequests')
def viewRequests():
return render_template('viewRequests.html')
@app.route('/home/viewRequests/fulfill', methods=['GET', 'POST'])
def fulfill():
msg = ''
if request.method == 'POST' and 'startDate' in request.form and 'eventName' in request.form and 'otherFirstName' in request.form and 'otherLastName' in request.form:
otherFirst = request.form['otherFirstName']
otherLast = request.form['otherLastName']
startDate = request.form['startDate']
eventName = request.form['eventName']
io = IO(session['username'])
io.fulfill(eventName, startDate, otherFirst, otherLast)
msg = 'Fulfilled Event!'
elif request.method == 'POST':
msg = 'PLEASE PLEASE, do the time!!!'
return render_template('fulfill.html', msg=msg)
@app.route('/logout')
def logout():
usr = session['username']
#os.remove(f'images/calendar-{usr}')
session.pop('LoggedIn', None)
#session.pop('id', None)
session.pop('username', None)
return redirect(url_for('main'))
if __name__ == "__main__":
app.run()