-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
433 lines (363 loc) · 14.1 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
421
422
423
424
425
426
427
428
429
430
431
432
433
from flask import Flask, render_template, redirect, url_for, request, session, flash, jsonify, send_from_directory, abort, current_app
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
from werkzeug.security import check_password_hash, generate_password_hash
from psycopg2.extras import RealDictCursor
from datetime import timedelta
import os
import pandas as pd
from db.db_connection import get_connection
from report_generator import generate_reports
app = Flask(__name__)
app.secret_key = os.getenv('FLASK_SECRET_KEY')
# Configure upload folder
app.config['UPLOAD_FOLDER'] = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'generated_reports')
# Set up Flask-Login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
# Set session timeout to 30 minutes
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=30)
def file_exists(file_name):
"""Check if the file exists in the upload folder."""
file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], file_name)
return os.path.isfile(file_path)
@app.context_processor
def utility_processor():
"""Add utility functions to the Jinja2 template context."""
return dict(file_exists=file_exists)
class User(UserMixin):
def __init__(self, id, username, admin=False):
self.id = id
self.username = username
self.admin = admin
@login_manager.user_loader
def load_user(user_id):
conn = get_connection()
if conn:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute("SELECT id, username, admin FROM users WHERE id = %s", (user_id,))
user = cursor.fetchone()
conn.close()
else:
user = None
return User(user['id'], user['username'], user['admin']) if user else None
@app.route('/')
@login_required
def dashboard():
conn = get_connection()
last_api_call = None
if conn:
try:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute("SELECT * FROM api_calls ORDER BY created_at DESC LIMIT 1")
last_api_call = cursor.fetchone()
finally:
conn.close()
return render_template('dashboard.html', last_api_call=last_api_call)
@app.route('/api_calls')
@login_required
def api_calls():
conn = get_connection()
if conn:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute("SELECT * FROM api_calls ORDER BY created_at DESC")
api_calls = cursor.fetchall()
conn.close()
else:
api_calls = []
return render_template('api_calls.html', api_calls=api_calls)
@app.route('/stats')
@login_required
def stats():
return render_template('stats.html')
@app.route('/api/stats', methods=['POST'])
@login_required
def get_filtered_stats():
data = request.json
start_date = data.get('start_date')
end_date = data.get('end_date')
device_type = data.get('device_type')
conn = get_connection()
stats = {}
filters = []
if conn:
try:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
query = """
SELECT COUNT(*) AS total_visits,
AVG(visit_duration) AS avg_visit_duration,
device_type, browser_name
FROM daily_visits WHERE TRUE
"""
if start_date and end_date:
query += " AND server_time BETWEEN %s AND %s"
filters.extend([start_date, end_date])
query += " GROUP BY device_type, browser_name"
cursor.execute(query, tuple(filters))
stats = cursor.fetchall()
except Exception as e:
return jsonify({'error': str(e)}), 500
finally:
conn.close()
return jsonify(stats), 200
@app.route('/users', methods=['GET', 'POST'])
@login_required
def users():
conn = get_connection()
users = []
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
hashed_password = generate_password_hash(password)
if conn:
try:
with conn.cursor() as cursor:
cursor.execute(
"INSERT INTO users (username, encrypted_password) VALUES (%s, %s)",
(username, hashed_password)
)
conn.commit()
finally:
conn.close()
return redirect(url_for('manage_users'))
if conn:
try:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute("SELECT * FROM users ORDER BY id ASC")
users = cursor.fetchall()
finally:
conn.close()
return render_template('users.html', users=users)
@app.route('/settings')
@login_required
def settings():
return render_template('settings.html', users=users)
@app.route('/users/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_user(id):
conn = get_connection()
user = None
if request.method == 'POST':
username = request.form['username']
if conn:
try:
with conn.cursor() as cursor:
cursor.execute(
"UPDATE users SET username = %s WHERE id = %s",
(username, id)
)
conn.commit()
finally:
conn.close()
return redirect(url_for('users'))
if conn:
try:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute("SELECT id, username FROM users WHERE id = %s", (id,))
user = cursor.fetchone()
finally:
conn.close()
return render_template('edit_user.html', user=user)
@app.route('/users/update/<int:id>', methods=['POST'])
@login_required
def update_user(id):
username = request.form['username']
conn = get_connection()
if conn:
try:
with conn.cursor() as cursor:
cursor.execute(
"UPDATE users SET username = %s WHERE id = %s",
(username, id)
)
conn.commit()
finally:
conn.close()
return redirect(url_for('users'))
@app.route('/users/new', methods=['GET', 'POST'])
@login_required
def new_user():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
hashed_password = generate_password_hash(password, method='pbkdf2:sha256')
conn = get_connection()
if conn:
try:
with conn.cursor() as cursor:
cursor.execute(
"INSERT INTO users (username, encrypted_password) VALUES (%s, %s)",
(username, hashed_password)
)
conn.commit()
finally:
conn.close()
return redirect(url_for('users'))
return render_template('new_user.html')
@app.route('/users/delete/<int:id>', methods=['POST'])
@login_required
def delete_user(id):
conn = get_connection()
if conn:
try:
with conn.cursor() as cursor:
cursor.execute("DELETE FROM users WHERE id = %s", (id,))
conn.commit()
finally:
conn.close()
return redirect(url_for('users'))
@app.route('/course_contents')
@login_required
def course_contents():
conn = get_connection()
if conn:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute("SELECT * FROM course_contents")
course_contents = cursor.fetchall()
conn.close()
else:
course_contents = []
return render_template('course_contents.html', course_contents=course_contents)
@app.route('/create_course_content', methods=['GET', 'POST'])
@login_required
def create_course_content():
if request.method == 'POST':
type_of = request.form['type_of']
language = request.form['language']
lecture_title = request.form['lecture_title']
lecture_no = request.form.get('lecture_no')
youtube_link = request.form.get('youtube_link')
ex_number = request.form.get('ex_number')
ex_instruction = request.form.get('ex_instruction')
conn = get_connection()
if conn:
with conn.cursor() as cursor:
cursor.execute("""
INSERT INTO course_contents (type_of, language, lecture_title, lecture_no, youtube_link, ex_number, ex_instruction)
VALUES (%s, %s, %s, %s, %s, %s, %s)
""", (type_of, language, lecture_title, lecture_no, youtube_link, ex_number, ex_instruction))
conn.commit()
conn.close()
flash('Course content created successfully!', 'success')
return redirect(url_for('course_contents'))
return render_template('create_course_content.html')
@app.route('/edit_course_content/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_course_content(id):
conn = get_connection()
course_content = None
if conn:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute("SELECT * FROM course_contents WHERE id = %s", (id,))
course_content = cursor.fetchone()
conn.close()
if request.method == 'POST':
type_of = request.form['type_of']
language = request.form['language']
lecture_title = request.form['lecture_title']
lecture_no = request.form.get('lecture_no')
youtube_link = request.form.get('youtube_link')
ex_number = request.form.get('ex_number')
ex_instruction = request.form.get('ex_instruction')
conn = get_connection()
if conn:
with conn.cursor() as cursor:
cursor.execute("""
UPDATE course_contents
SET type_of = %s, language = %s, lecture_title = %s, lecture_no = %s, youtube_link = %s, ex_number = %s, ex_instruction = %s
WHERE id = %s
""", (type_of, language, lecture_title, lecture_no, youtube_link, ex_number, ex_instruction, id))
conn.commit()
conn.close()
flash('Course content updated successfully!', 'success')
return redirect(url_for('course_contents'))
return render_template('edit_course_content.html', course_content=course_content)
@app.route('/delete_course_content/<int:id>', methods=['POST'])
@login_required
def delete_course_content(id):
conn = get_connection()
if conn:
with conn.cursor() as cursor:
cursor.execute("DELETE FROM course_contents WHERE id = %s", (id,))
conn.commit()
conn.close()
flash('Course content deleted successfully!', 'success')
return redirect(url_for('course_contents'))
@app.route('/process_selection', methods=['POST'])
@login_required
def process_selection():
selected_ids = request.form.getlist('selected_courses')
if not selected_ids:
flash('No selections made. Please select at least one course content.')
return redirect(url_for('course_contents'))
conn = get_connection()
if conn:
try:
selected_ids_str = ','.join(selected_ids)
query = f"SELECT * FROM course_contents WHERE id IN ({selected_ids_str})"
df = pd.read_sql_query(query, conn)
finally:
conn.close()
num_reports = generate_reports(df)
flash(f'{num_reports} reports generated successfully!')
return redirect(url_for('course_contents'))
@app.route('/download/<file_name>')
def download_file(file_name):
if file_exists(file_name):
return send_from_directory(current_app.config['UPLOAD_FOLDER'], file_name, as_attachment=True)
else:
abort(404)
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('dashboard'))
error_message = None
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
conn = get_connection()
if conn:
try:
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
cursor.execute(
"SELECT id, username, encrypted_password, sign_in_count FROM users WHERE username = %s",
(username,))
user = cursor.fetchone()
if user:
if check_password_hash(user['encrypted_password'], password):
user_obj = User(user['id'], user['username'])
login_user(user_obj)
session.permanent = True
with conn.cursor() as cursor:
cursor.execute(
"UPDATE users SET sign_in_count = sign_in_count + 1, last_sign_in_at = NOW() WHERE id = %s",
(user['id'],))
conn.commit()
return redirect(url_for('dashboard'))
else:
error_message = 'Incorrect password. Please try again.'
else:
error_message = 'Username not found. Please try again.'
except Exception as e:
error_message = f"Database error: {str(e)}"
finally:
conn.close()
else:
error_message = 'Database connection error.'
return render_template('login.html', error_message=error_message)
@app.route('/logout', methods=['POST'])
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
@app.errorhandler(404)
def page_not_found(error):
return render_template('errors/404.html'), 404
@app.errorhandler(500)
def internal_server_error(error):
return render_template('errors/500.html'), 500
@app.before_request
def update_session_timeout():
session.modified = True
if __name__ == "__main__":
app.run(debug=True)