Skip to content

Commit adc0c8d

Browse files
committed
Create change password and email feature
1 parent affc4a9 commit adc0c8d

File tree

3 files changed

+93
-32
lines changed

3 files changed

+93
-32
lines changed

My_path_team9/student/forms.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from flask_wtf import FlaskForm
22
from wtforms import RadioField, SelectField, SelectMultipleField, widgets, SubmitField
33
from wtforms import StringField, PasswordField, SubmitField
4-
from wtforms.validators import DataRequired, Length, EqualTo
4+
from wtforms.validators import DataRequired, Length, EqualTo, Email, Optional
55
from wtforms.validators import URL
66

77
class MultiCheckboxField(SelectMultipleField):
@@ -114,9 +114,15 @@ class SurveyForm(FlaskForm):
114114

115115

116116
class SettingsForm(FlaskForm):
117-
username = StringField('New Username', validators=[DataRequired(), Length(min=3, max=64)])
118-
submit = SubmitField('Update')
119-
117+
username = StringField('Username', validators=[DataRequired()])
118+
email = StringField('Email', validators=[DataRequired(), Email()])
119+
current_password = PasswordField('Current Password', validators=[Optional()])
120+
new_password = PasswordField('New Password', validators=[Optional()])
121+
confirm_password = PasswordField('Confirm New Password', validators=[
122+
Optional(),
123+
EqualTo('new_password', message='Passwords must match')
124+
])
125+
submit = SubmitField('Save Changes')
120126
class VideoSubmissionForm(FlaskForm):
121127
video_link = StringField('Video Link', validators=[
122128
DataRequired(), URL(), Length(max=500)

My_path_team9/student/routes.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,39 @@ def settings():
1515
if current_user.role != 'student':
1616
return "Access denied", 403
1717

18-
form = SettingsForm()
18+
form = SettingsForm(obj=current_user)
1919

2020
if form.validate_on_submit():
21-
existing_user = User.query.filter_by(username=form.username.data).first()
22-
if existing_user and existing_user.id != current_user.id:
23-
flash('This username is already taken.', 'warning')
24-
else:
25-
current_user.username = form.username.data
26-
db.session.commit()
27-
flash('Username updated successfully.', 'success')
21+
# Check for username/email conflicts
22+
if User.query.filter(User.username == form.username.data, User.id != current_user.id).first():
23+
flash('Username already taken.', 'warning')
2824
return redirect(url_for('student.settings'))
2925

26+
if User.query.filter(User.email == form.email.data, User.id != current_user.id).first():
27+
flash('Email already in use.', 'warning')
28+
return redirect(url_for('student.settings'))
29+
30+
current_user.username = form.username.data
31+
current_user.email = form.email.data
32+
33+
# Change password only if fields are filled
34+
if form.new_password.data:
35+
if not form.current_password.data:
36+
flash('Current password is required to change your password.', 'warning')
37+
elif not current_user.verify_password(form.current_password.data):
38+
flash('Current password is incorrect.', 'danger')
39+
else:
40+
try:
41+
current_user.password = form.new_password.data
42+
flash('Password changed successfully.', 'success')
43+
except ValueError as ve:
44+
flash(str(ve), 'danger')
45+
return redirect(url_for('student.settings'))
46+
47+
db.session.commit()
48+
flash('Profile updated successfully.', 'success')
49+
return redirect(url_for('student.settings'))
50+
3051
return render_template('settings.html', form=form, current_user=current_user)
3152

3253
@student_bp.route('/survey', methods=['GET', 'POST'])
Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,60 @@
1-
{% extends "base.html" %}
1+
{% extends 'base.html' %}
22

3-
{% block title %}Profile Settings{% endblock %}
3+
{% block title %}Settings{% endblock %}
44

55
{% block content %}
6-
<div class="container">
7-
<h2 class="mb-4">Update Your Profile</h2>
8-
<div class="card">
9-
<form method="POST" action="">
10-
{{ form.hidden_tag() }}
11-
12-
<div class="mb-3">
13-
<label for="username" class="form-label">New Username</label>
14-
{{ form.username(class="form-control", id="username") }}
15-
{% for error in form.username.errors %}
16-
<div class="text-danger">{{ error }}</div>
17-
{% endfor %}
18-
</div>
19-
20-
<div class="mb-3">
21-
{{ form.submit(class="btn btn-primary") }}
22-
</div>
23-
</form>
6+
<div class="container mt-4">
7+
<h2>Settings</h2>
8+
9+
<!-- Flash messages -->
10+
{% with messages = get_flashed_messages(with_categories=true) %}
11+
{% if messages %}
12+
{% for category, message in messages %}
13+
<div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
14+
{{ message }}
15+
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
16+
</div>
17+
{% endfor %}
18+
{% endif %}
19+
{% endwith %}
20+
21+
<form method="POST">
22+
{{ form.hidden_tag() }}
23+
24+
<div class="mb-3">
25+
{{ form.username.label(class="form-label") }}
26+
{{ form.username(class="form-control") }}
2427
</div>
28+
29+
<div class="mb-3">
30+
{{ form.email.label(class="form-label") }}
31+
{{ form.email(class="form-control") }}
32+
</div>
33+
34+
<hr>
35+
36+
<h5>Change Password</h5>
37+
<div class="mb-3">
38+
{{ form.current_password.label(class="form-label") }}
39+
{{ form.current_password(class="form-control") }}
40+
</div>
41+
<div class="mb-3">
42+
{{ form.new_password.label(class="form-label") }}
43+
{{ form.new_password(class="form-control") }}
44+
</div>
45+
<div class="mb-3">
46+
{{ form.confirm_password.label(class="form-label") }}
47+
{{ form.confirm_password(class="form-control") }}
48+
</div>
49+
50+
<button type="submit" class="btn btn-primary mt-3">{{ form.submit.label.text }}</button>
51+
</form>
2552
</div>
2653
{% endblock %}
54+
{% with messages = get_flashed_messages(with_categories=true) %}
55+
{% if messages %}
56+
{% for category, message in messages %}
57+
<div class="alert alert-{{ category }}">{{ message }}</div>
58+
{% endfor %}
59+
{% endif %}
60+
{% endwith %}

0 commit comments

Comments
 (0)