-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
132 lines (97 loc) · 4.04 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
from flask import Flask, render_template, request, redirect, url_for, session, jsonify, g, flash
import os
from models import User, Course
import database
import functionalties
app = Flask(__name__)
app.secret_key = '123'
courses = database.load_courses()
saved_courses = []
@app.route("/", methods=['GET', 'POST'])
def index():
random_courses = functionalties.generate_courses(courses)
if request.method == 'POST':
if 'signupbtn' in request.form:
username = request.form['username']
password = request.form['password']
email = request.form['email']
# Create a new user in the database
database.add_user_details(username, password, email)
flash("Sign up Successful!", "signup")
flash("You can proceed to Login.", "signup")
return redirect(url_for('index'))
elif 'loginbtn' in request.form:
username = request.form['username']
password = request.form['password']
# Validate credentials and perform login logic.
user = database.check_user_login(username, password)
if user:
session['user_id'] = user.id # Store only user_id in session
flash('Login Successful!', 'login')
return redirect(url_for('userpage'))
else:
flash("Invalid Login Details!", "failure")
return redirect(url_for('index'))
return render_template('index.html', random_courses=random_courses)
@app.route("/user", methods=['GET', 'POST'])
def userpage():
random_courses = functionalties.generate_courses(courses)
if 'user_id' in session:
user_id = session['user_id']
user = database.get_user(user_id) # Retrieve user information from database
if user:
saved_courses = database.load_bookmark_list(user_id)
return render_template('User_page.html', username=user.username, random_courses=random_courses, saved_courses=saved_courses, user_id=user_id)
else:
flash("Please login to access this page!", "warning")
return redirect(url_for('index')) # Invalidate session if user not found
else:
return redirect(url_for('index'))
@app.route("/logout", methods=['POST'])
def logout():
# Clear the user session
session.clear()
return redirect(url_for('index'))
@app.route('/search' , methods=['GET'])
def search():
#Get the query to search for
query = request.args.get('query', '')
#Load courses from database
courses = database.load_courses()
#Search and return json
search_titles = functionalties.search_courses(courses, query)
return jsonify(results=search_titles)
@app.route('/save' , methods=['POST'])
def save():
#Retrieve the course url and username.
data = request.get_json(force=True)
course_id = data.get('id')
user_id = data.get('user_id')
#Save the course id to the database
database.save_course(user_id, course_id)
#return jsonify(results=saved_courses)
return redirect(url_for('userpage'))
@app.route("/delete", methods=['POST'])
def delete():
#Retrieve the course url and username.
data = request.get_json(force=True)
course_id = data.get('id')
user_id = data.get('user_id')
#delete course
#app.logger.info(f"Deleting bookmark for user_id: {user_id}, course_id: {course_id}")
database.delete_bookmark(user_id, course_id)
return redirect(url_for('userpage'))
@app.route('/courses_api')
def list_courses():
courses_list = [{
"id" : course.id,
"title" : course.title.encode('utf-8').decode('cp1252', 'ignore'),
"url" : course.url.encode('utf-8').decode('cp1252', 'ignore'),
"instructor" : course.instructor.encode('utf-8').decode('cp1252', 'ignore')
} for course in courses]
return jsonify(courses_list)
@app.route('/landing_page', methods=['GET'])
def landingPage():
return render_template('landing_page.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)