-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
420 lines (331 loc) · 13.2 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import mysql.connector
from flask import Flask, request, jsonify, session, render_template, url_for, redirect, flash, send_from_directory, Response
from datetime import datetime
import os
from werkzeug.utils import secure_filename
import mimetypes
app = Flask(__name__)
app.secret_key = 'LAXARUN'
app.config['UPLOAD_FOLDER'] = 'static/resources'
@app.route('/')
def home():
return render_template('homepage.html')
@app.route('/faculty')
def faculty():
return render_template('facultydetails.html')
@app.route('/logout')
def logout():
session.clear()
return redirect('/')
@app.route('/login', methods=['GET', 'POST'])
def login():
if 'email' in session and 'actor' in session:
return redirect('/')
if request.method == "POST":
email = request.form['email']
password = request.form['password']
role = request.form.get('role')
# Assuming you have a database connection
logindb = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='sf_logininfo'
)
cursor = logindb.cursor()
query = "SELECT email, actor FROM main WHERE email = %s AND password = %s AND actor = %s"
cursor.execute(query, (email, password, role))
result = cursor.fetchone()
cursor.close()
logindb.close()
if result:
session['email'] = email
session['actor'] = role
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return jsonify({'success': True})
else:
return redirect('/') # Redirect to homepage after successful login
else:
error_message = "Invalid credentials or role selection. Please try again."
flash(error_message, 'error') # Flash error message
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
return jsonify({'success': False, 'error': error_message})
else:
return render_template('login.html')
return render_template('login.html')
def get_faculty_name(email):
facultydb = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='sf_facultyinfo'
)
cursor = facultydb.cursor()
query = "SELECT Name FROM main WHERE email = %s"
cursor.execute(query, (email,))
result = cursor.fetchone()
cursor.close()
facultydb.close()
if result:
return result[0]
else:
return None
@app.route('/create_announcement', methods=['POST'])
def create_announcement():
if 'email' in session and 'actor' in session and session['actor'] == 'Faculty':
# Get the content from the form submission
content = request.form.get('content')
email = session['email']
# actor = session['actor']
name = get_faculty_name(email)
current_datetime = datetime.now()
# Format the date and time to match MySQL's DATE and TIME formats
mysql_date_format = current_datetime.strftime('%Y-%m-%d')
mysql_time_format = current_datetime.strftime('%H:%M:%S')
announcementdb = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='sf_announcement'
)
cursor = announcementdb.cursor()
insert_query = "INSERT INTO main (Name, Announcement, Date, Time) VALUES (%s, %s, %s, %s)"
values = (name, content, mysql_date_format, mysql_time_format)
cursor.execute(insert_query, values)
announcementdb.commit()
cursor.close()
announcementdb.close()
return redirect(url_for('announcements'))
else:
return redirect('/')
@app.route('/announcements', methods=['GET', 'POST'])
def announcements():
if 'email' not in session or 'actor' not in session:
return redirect('/')
try:
announcementdb = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='sf_announcement'
)
cursor = announcementdb.cursor()
select_query = "SELECT Id, Name, Announcement, Date, Time FROM main ORDER BY Date DESC, Time DESC"
cursor.execute(select_query)
result = cursor.fetchall()
announcements = []
for row in result:
id, name, content, date, time = row
announcement = {
'id': id,
'name': name,
'content': content,
'date': date,
'time': time
}
announcements.append(announcement)
return render_template('announcement.html', announcements=announcements, get_faculty_name = get_faculty_name)
except Exception as e:
# Handle any errors here
return render_template('error.html', error_message=str(e))
finally:
if announcementdb.is_connected():
cursor.close()
announcementdb.close()
@app.route('/delete_announcement/<int:announcement_id>', methods=['GET', 'POST'])
def delete_announcement(announcement_id):
if 'email' in session and 'actor' in session and session['actor'] == 'Faculty':
try:
# Connect to the database
announcementdb = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='sf_announcement'
)
cursor = announcementdb.cursor()
# Delete the announcement from the database
delete_query = "DELETE FROM main WHERE Id = %s"
cursor.execute(delete_query, (announcement_id,))
announcementdb.commit()
return "Success", 200
except Exception as e:
print(e)
return "Error", 500 # Return an error response
finally:
cursor.close()
announcementdb.close()
else:
return "Unauthorized", 403
@app.route('/resources')
def resources():
if 'email' not in session or 'actor' not in session:
return redirect('/')
db_connection = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='sf_resources'
)
cursor = db_connection.cursor()
select_query = "SELECT * FROM main ORDER BY Date DESC, Time DESC"
cursor.execute(select_query)
result = cursor.fetchall()
cursor.close()
db_connection.close()
resources = []
for row in result:
id, name, title, description, Filenames, date, time = row
resource = {
'id': id,
'name': name,
'title': title,
'description': description,
'Filenames': Filenames,
'date': date,
'time': time
}
resources.append(resource)
return render_template('resources.html', resources=resources, get_faculty_name=get_faculty_name)
@app.route('/upload_resource', methods=['POST'])
def upload_resource():
if 'email' not in session or 'actor' not in session:
return redirect('/')
if request.method == 'POST':
try:
name = get_faculty_name(session['email'])
title = request.form.get('title')
description = request.form.get('description')
files = request.files.getlist('files[]')
folder_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(name))
os.makedirs(folder_path, exist_ok=True)
current_datetime = datetime.now()
mysql_date_format = current_datetime.strftime('%Y-%m-%d')
mysql_time_format = current_datetime.strftime('%H:%M:%S')
db_connection = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='sf_resources'
)
filenames_with_paths = []
for file in files:
if file.filename:
filename = secure_filename(file.filename)
file_path = os.path.join(folder_path, filename)
file.save(file_path)
filenames_with_paths.append(filename)
# Convert filenames_with_paths list to a string
filenames_str = ', '.join(filenames_with_paths)
cursor = db_connection.cursor()
insert_query = "INSERT INTO main (Name, Title, Description, Filenames, Date, Time) VALUES (%s, %s, %s, %s, %s, %s)"
cursor.execute(insert_query, (name, title, description, filenames_str, mysql_date_format, mysql_time_format))
db_connection.commit()
cursor.close()
db_connection.close()
return redirect('/resources')
except Exception as e:
print(e)
return render_template('error.html', error_message=str(e))
return redirect('/')
# @app.route('/download_resource/<int:resource_id>/<filename>')
# def download_resource(resource_id, filename):
# db_connection = mysql.connector.connect(
# host='localhost',
# user='root',
# password='',
# database='sf_resources'
# )
# cursor = db_connection.cursor()
# select_query = "SELECT Name, FolderPath FROM resources WHERE id = %s"
# cursor.execute(select_query, (resource_id,))
# resource = cursor.fetchone()
# if resource:
# name, folder_path = resource
# folder_path = os.path.join(folder_path, secure_filename(name))
# file_path = os.path.join(folder_path, secure_filename(filename))
# cursor.close()
# db_connection.close()
# return send_from_directory(folder_path, filename=filename, as_attachment=True)
# cursor.close()
# db_connection.close()
# return render_template('error.html', error_message='Resource not found')
@app.route('/download_resource/<int:resource_id>/<filename>')
def download_resource(resource_id, filename):
db_connection = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='sf_resources'
)
cursor = db_connection.cursor()
select_query = "SELECT Name, Filenames FROM main WHERE id = %s"
cursor.execute(select_query, (resource_id,))
resource = cursor.fetchone()
print(resource)
if resource:
name, filenames_str = resource
filenames = [filename.strip() for filename in filenames_str.split(',')]
print(filenames)
folder_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(name))
print(folder_path)
if filename in filenames:
file_path = os.path.join(folder_path, filename)
cursor.close()
db_connection.close()
# Determine the MIME type based on the file extension
mime_type = mimetypes.guess_type(file_path)[0] or 'application/octet-stream'
# Create a Flask Response object to send the file with correct MIME type
response = Response()
response.headers['Content-Type'] = mime_type
response.headers['Content-Disposition'] = f'attachment; filename={filename}'
response.headers['Content-Length'] = os.path.getsize(file_path)
response.data = open(file_path, 'rb').read()
return response
# return send_from_directory(directory=folder_path, filename=filename, as_attachment=True)
cursor.close()
db_connection.close()
return "File not found", 404
@app.route('/delete_resource/<int:resource_id>', methods=['GET', 'POST'])
def delete_resource(resource_id):
if 'email' not in session or 'actor' not in session:
return "Unauthorized", 403
db_connection = mysql.connector.connect(
host='localhost',
user='root',
password='',
database='sf_resources'
)
cursor = db_connection.cursor()
try:
# Get faculty name and filenames associated with the resource
select_query = "SELECT Name, Filenames FROM main WHERE id = %s"
cursor.execute(select_query, (resource_id,))
resource = cursor.fetchone()
if resource is None:
return "Resource not found", 404
faculty_name = resource[0]
filenames_str = resource[1]
filenames = filenames_str.split(', ') # Split the string into a list of filenames
# Delete the resource record from the database
delete_query = "DELETE FROM main WHERE id = %s"
cursor.execute(delete_query, (resource_id,))
db_connection.commit()
# Delete the associated files from the folder
folder_path = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(faculty_name))
for filename in filenames:
file_path = os.path.join(folder_path, filename)
if os.path.exists(file_path):
os.remove(file_path)
return "Success", 200
except Exception as e:
print(e)
return "Error", 500
finally:
cursor.close()
db_connection.close()
@app.route('/discussion')
def discussion():
return render_template('discussionforum.html')
if __name__ == '__main__':
app.run(debug=True)