-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
238 lines (193 loc) · 6.21 KB
/
app.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
from flask import Flask, make_response, jsonify, render_template, request, redirect
from flask_mysqldb import MySQL
from flask_cors import CORS, cross_origin
import MySQLdb
import yaml
import os
from dbConfig import database_config
from commands import get_commands
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
env = ""
if env == "dev":
dev = yaml.load(open('db.yaml'), Loader=yaml.FullLoader)
DATABASE_URL = dev['CLEARDB_DATABASE_URL']
PASSWORD = dev['PASSWORD']
else:
DATABASE_URL = os.environ.get('CLEARDB_DATABASE_URL')
PASSWORD = os.environ.get('PASSWORD')
user, password, host, db = database_config(DATABASE_URL)
app.config['MYSQL_HOST'] = host
app.config['MYSQL_USER'] = user
app.config['MYSQL_PASSWORD'] = password
app.config['MYSQL_DB'] = db
mysql = MySQL(app)
if env == 'dev':
app.SECRET_KEY = dev['SECRET_KEY']
else:
app.SECRET_KEY = os.environ.get("SECRET_KEY")
def get_events(table_name='', for_editing=False):
cur = mysql.connection.cursor()
cur.execute('select * from ALL_EVENTS;')
events = cur.fetchall()
check = -2
if table_name == 'past_events':
check = -1
if table_name == 'ongoing_events':
check = 0
if table_name == 'upcoming_events':
check = 1
event_list = []
if(for_editing):
for i in events:
obj = {
"id": i[0],
"title": i[1],
"description": i[2],
"details": i[3],
"date-of-event": i[4],
"image": i[5],
"club": i[6],
"status": i[7]
}
event_list.append(obj)
else:
for i in events:
if(i[7] == check):
obj = {
"id": i[0],
"title": i[1],
"description": i[2],
"details": i[3],
"date-of-event": i[4],
"image": i[5],
"club": i[6],
}
event_list.append(obj)
response_body = {
"size": len(event_list),
"events": event_list
}
if(for_editing == True):
return response_body
res = make_response(jsonify(response_body), 200)
return res
@app.route('/')
def example():
return render_template('details.html')
@app.route('/past')
@cross_origin()
def past():
return get_events("past_events")
@app.route('/ongoing')
@cross_origin()
def ongoing():
return get_events('ongoing_events')
@app.route('/upcoming')
@cross_origin()
def upcoming():
return get_events('upcoming_events')
@app.route("/new", methods=['GET', 'POST'])
def new():
if request.method == 'GET':
return render_template("new.html")
else:
data = request.form.to_dict()
if(data['password'] == PASSWORD):
table_name = data['event'] + "_events"
if data['event'] == 'past':
status = -1
elif data['event'] == 'ongoing':
status = 0
else:
status = 1
cur = mysql.connection.cursor()
cur.execute("INSERT INTO ALL_EVENTS VALUES (NULL, '{}', '{}', '{}', '{}', '{}', '{}', {});".format(
data['title'], data['description'], data['details'], data['date'], data['image'], data['club'], status))
mysql.connection.commit()
cur.close()
return "New Event Created"
else:
return "Incorrect Password"
@app.route("/edit", methods=['GET', 'POST'])
def edit():
res = get_events('', True)
events = res['events']
size = res['size']
if request.method == 'GET':
if(request.args):
title = request.args.get('title')
fromTitle = []
club = request.args.get('club')
fromClub = []
if(title):
for event in events:
title = title.lower()
if title in event['title'].lower():
fromTitle.append(event)
if(club):
for event in events:
club = club.lower()
if club in event['club'].lower():
fromClub.append(event)
events = fromTitle
for item in fromClub:
present = False
for event in events:
if(event['id'] == item['id']):
present = True
if(not present):
events.append(item)
size = len(events)
return render_template('allEditor.html', events=events, size=size)
else:
data = request.form.to_dict()
if(data['password'] == PASSWORD):
commands = get_commands(data)
cur = mysql.connection.cursor()
for i in commands:
try:
print("Executing: " + i)
cur.execute(i)
except (MySQLdb.Error, MySQLdb.Warning) as e:
print(e)
mysql.connection.commit()
cur.close()
return redirect('/')
else:
return "Incorrect Password"
return 'POST'
@app.route('/search', methods=['GET', 'POST'])
def search():
if request.method == 'GET':
return render_template('search.html')
else:
data = request.form.to_dict()
title = data['title']
club = data['club']
expression = "?"
if(title):
expression += 'title=' + title + '&'
if(club):
expression += 'club=' + club + '&'
return redirect('/edit' + expression)
@app.route('/delete/<id>', methods=['GET', 'POST'])
def delete(id):
if request.method == 'GET':
return render_template("verify.html", id=id)
else:
data = request.form.to_dict()
if(data['password'] == PASSWORD):
cur = mysql.connection.cursor()
cur.execute("DELETE FROM ALL_EVENTS WHERE ID={}".format(id))
mysql.connection.commit()
cur.close()
return redirect('/')
else:
return "INCORRECT PASSWORD"
if __name__ == "__main__":
if(env == 'dev'):
app.run(debug=True)
else:
app.run()