-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd_bot.py
More file actions
282 lines (233 loc) · 9.88 KB
/
std_bot.py
File metadata and controls
282 lines (233 loc) · 9.88 KB
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
from flask import Flask, render_template, request, jsonify, redirect, url_for, session
from pymongo import MongoClient
from flask_bcrypt import Bcrypt
from test2 import StudentAcademicChatbot
import json, os
from datetime import datetime, timedelta
import secrets
from dotenv import load_dotenv
from signup import signup_app
from flask_dance.contrib.google import make_google_blueprint, google
from flask_dance.consumer import oauth_authorized, oauth_error
from flask_login import LoginManager, UserMixin, login_user, logout_user, current_user
from mail import send_email # Import both functions
load_dotenv()
# Initialize Flask and Bcrypt
app = Flask(__name__)
bcrypt = Bcrypt(app)
app.secret_key = "your-secret-key"
app.register_blueprint(signup_app, url_prefix='/signup')
# Initialize Flask-Login
login_manager = LoginManager()
login_manager.init_app(app)
# User model for Flask-Login
class User(UserMixin):
def __init__(self, id, email):
self.id = id
self.email = email
# Load user callback for Flask-Login
@login_manager.user_loader
def load_user(user_id):
user_data = users_collection.find_one({"email": user_id})
if user_data:
return User(user_data["email"], user_data["email"])
return None
# Google OAuth blueprint
google_blueprint = make_google_blueprint(
client_id=os.getenv("GOOGLE_CLIENT_ID"),
client_secret=os.getenv("GOOGLE_CLIENT_SECRET"),
scope=["profile", "email"],
redirect_to="google_login"
)
app.register_blueprint(google_blueprint, url_prefix="/login")
# Connect to MongoDB
MONGODB_URI = os.getenv("MONGODB_URI")
# Connect to MongoDB Atlas
client = MongoClient(MONGODB_URI)
db = client.CampusConnect # Adjust database name if needed
collection = db.std_data # Adjust collection name if needed
# Initialize chatbot
academic_chatbot = StudentAcademicChatbot()
@app.route('/')
def home():
return render_template('login.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# Check if request is JSON (from AJAX) or form data
if request.is_json:
data = request.get_json()
username = data.get('username')
password = data.get('password')
else:
username = request.form.get('username')
password = request.form.get('password')
if not username or not password:
if request.is_json:
return jsonify({"error": "Username and password are required"}), 400
return render_template('login.html', error="Username and password are required")
try:
# Check if user exists
user = db.campus_connect.find_one({"username": username})
if user and bcrypt.check_password_hash(user["password"], password):
# Set session
session['logged_in'] = True
session['username'] = username
# Return different responses based on request type
if request.is_json:
return jsonify({"success": True, "redirect": url_for('chatbot')}), 200
return redirect(url_for('chatbot'))
else:
if request.is_json:
return jsonify({"error": "Invalid username or password"}), 401
return render_template('login.html', error="Invalid username or password")
except Exception as e:
print(f"Database error: {e}")
if request.is_json:
return jsonify({"error": "Database error occurred"}), 500
return render_template('login.html', error="Database error occurred")
return render_template('login.html')
@app.route('/logout')
def logout():
# Clear the session
session.clear()
# Redirect to login page
return redirect(url_for('home'))
@app.route('/chatbot', methods=['GET', 'POST'])
def chatbot():
# Check if user is logged in
if not session.get('logged_in'):
return redirect(url_for('home'))
return render_template('test.html')
@app.route('/chat', methods=['GET', 'POST'])
def chat():
# Check if user is logged in
if not session.get('logged_in'):
return jsonify({"error": "Unauthorized"}), 401
data = request.json
query = data.get('message', '')
student_id = data.get('student_id')
try:
response = academic_chatbot.chat(query, student_id)
return jsonify({"message": response})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/forgot-password', methods=['GET'])
def forgot_password_page():
return render_template('forgot_password.html')
@app.route('/api/reset-password', methods=['POST'])
def reset_password():
data = request.json
email = data.get('email')
if not email:
return jsonify({"error": "Email address is required."}), 400
try:
send_email(
subject="Password Reset Request",
body=(
"Dear User,\n\n"
"We received a request to reset your password. If you did not initiate this request, please ignore this email.\n\n"
"To reset your password, please click the link below:\n"
"http://example.com/reset-password?email={email}\n\n"
"This link will expire in 24 hours. If you need further assistance, contact our support team.\n\n"
"Best regards,\n"
"Campus Connect Team"
).format(email=email),
to_email=email
)
return jsonify({"message": "Password reset link has been sent."}), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/reset-password', methods=['GET', 'POST'])
def reset_password_endpoint():
if request.method == 'POST':
data = request.json
token = data.get('token')
new_password = data.get('new_password')
# Validate token and find user
user = campus_connect_collection.find_one({"reset_token": token})
if not user or user['reset_token_expiry'] < datetime.utcnow():
return jsonify({'error': 'Invalid or expired token'}), 400
# Hash new password
hashed_password = bcrypt.generate_password_hash(new_password).decode('utf-8')
# Update user password and clear reset token
campus_connect_collection.update_one(
{"reset_token": token},
{
"$set": {
"password": hashed_password,
"reset_token": None,
"reset_token_expiry": None
}
}
)
return jsonify({'message': 'Password has been reset successfully.'}), 200
return render_template('reset_password.html')
@app.route("/login/google")
def google_login():
if not google.authorized:
return redirect(url_for("google.login"))
resp = google.get("/oauth2/v2/userinfo")
if not resp.ok:
return "Failed to fetch user info from Google.", 400
user_info = resp.json()
email = user_info["email"]
# Check if user exists in the database
user = users_collection.find_one({"email": email})
if not user:
# Create a new user in the database
users_collection.insert_one({
"email": email,
"username": user_info.get("name", email.split("@")[0]),
"password": None # No password for Google-authenticated users
})
# Log the user in
user_obj = User(email, email)
login_user(user_obj)
session['logged_in'] = True
session['username'] = email
return redirect(url_for("chatbot"))
@app.route('/compare_students', methods=['GET'])
def compare_students_route():
student1 = request.args.get('student1')
student2 = request.args.get('student2')
if not student1 or not student2:
return jsonify({'error': 'Please provide both student IDs'}), 400
try:
# Use the chatbot's retrieve_student_data method
student_data1 = academic_chatbot.retrieve_student_data(student1)
student_data2 = academic_chatbot.retrieve_student_data(student2)
if not student_data1 or not student_data2:
return jsonify({'error': 'One or both student IDs are invalid'}), 404
comparison_data = {
'Student 1': {
'name': student1.split(' - ')[0], # Extract name from ID
'GPA': student_data1.get('academic_details', {}).get('academic_performance', {}).get('cgpa', 'N/A'),
'Credits': student_data1.get('academic_details', {}).get('credits_completed', 'N/A'),
'Attendance': student_data1.get('academic_details', {}).get('attendance', {}).get('percentage', 'N/A')
},
'Student 2': {
'name': student2.split(' - ')[0], # Extract name from ID
'GPA': student_data2.get('academic_details', {}).get('academic_performance', {}).get('cgpa', 'N/A'),
'Credits': student_data2.get('academic_details', {}).get('credits_completed', 'N/A'),
'Attendance': student_data2.get('academic_details', {}).get('attendance', {}).get('percentage', 'N/A')
}
}
return jsonify(comparison_data)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/handle_common_details_query', methods=['GET'])
def handle_common_details_query():
try:
# Use the method from your StudentAcademicChatbot class
common_details = academic_chatbot.handle_common_details_query()
# Add ranking based on CGPA
sorted_students = sorted(common_details, key=lambda x: x['cgpa'], reverse=True)
for i, student in enumerate(sorted_students, 1):
student['rank'] = i
return jsonify(sorted_students)
except Exception as e:
print(f"Error: {e}")
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)