made settings page, can chnage account type and username and email

This commit is contained in:
deole2 committed 2026-04-07 00:39:38 +01:00
1 parent c2936b15e9
commit 20dc16a217
3 files changed
+108 -1

No files matched your search

+34 -1
View File
@@ -476,4 +476,37 @@ def search_programs(query):
WHERE instr(LOWER(COALESCE(CAST(title AS TEXT), '')), ?1) > 0
OR instr(LOWER(COALESCE(CAST(description AS TEXT), '')), ?1) > 0
""", (query.lower(),)).fetchall()
return [dict(r) for r in rows]
return [dict(r) for r in rows]
def update_user_settings(user_id, username, email, account_type=None):
email = email.strip().lower()
with get_db() as conn:
# Check if username is taken by someone else
existing_username = conn.execute(
"SELECT user_id FROM users WHERE username = ? AND user_id != ?",
(username, user_id)
).fetchone()
if existing_username:
return False, "Username already taken"
# Check if email is taken by someone else
existing_email = conn.execute(
"SELECT user_id FROM users WHERE LOWER(email) = LOWER(?) AND user_id != ?",
(email, user_id)
).fetchone()
if existing_email:
return False, "Email already taken"
# Update the user
if account_type is not None:
conn.execute(
"UPDATE users SET username = ?, email = ?, account_type = ? WHERE user_id = ?",
(username, email, account_type, user_id)
)
else:
conn.execute(
"UPDATE users SET username = ?, email = ? WHERE user_id = ?",
(username, email, user_id)
)
return True, "Settings updated successfully"
+46
View File
@@ -0,0 +1,46 @@
from flask import render_template, Blueprint, g, redirect, url_for, request, flash
from database.db_commands import update_user_settings
page = Blueprint('settings', __name__, url_prefix='/settings')
@page.route('/', methods=['GET', 'POST'])
def home():
if not g.user:
return redirect(url_for('login.login'))
if request.method == 'POST':
username = request.form.get('username', '').strip()
email = request.form.get('email', '').strip()
account_type_display = request.form.get('account_type', '').strip()
# Map display names to database values
account_type_map = {
'private account': 'private_user',
'teacher': 'teacher',
'student': 'student'
}
account_type = account_type_map.get(account_type_display)
if not username or not email or not account_type:
flash('Username, email, and account type are required.', 'error')
return redirect(url_for('settings.home'))
if '@' not in email or '.' not in email:
flash('Please enter a valid email address.', 'error')
return redirect(url_for('settings.home'))
success, message = update_user_settings(g.user['user_id'], username, email, account_type)
flash(message, 'success' if success else 'error')
if success:
return redirect(url_for('settings.home'))
# Map database values to display names for the template
account_type_display_map = {
'private_user': 'private account',
'teacher': 'teacher',
'student': 'student'
}
current_account_type = account_type_display_map.get(g.user.get('account_type', 'private_user'), 'private account')
return render_template('settings/settings.jinja', current_account_type=current_account_type)
+28
View File
@@ -0,0 +1,28 @@
{% extends "base.jinja" %}
{% block title %}Settings{% endblock %}
{% block content %}
<div class="container">
<h1>Settings</h1>
<form method="post" action="{{ url_for('settings.home') }}">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username" value="{{user.username }}" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="{{user.email }}" required>
</div>
<div class="form-group">
<label for="account_type">Account Type:</label>
<select id="account_type" name="account_type" required>
<option value="private account" {% if current_account_type == 'private account' %}selected{% endif %}>Private Account</option>
<option value="teacher" {% if current_account_type == 'teacher' %}selected{% endif %}>Teacher</option>
<option value="student" {% if current_account_type == 'student' %}selected{% endif %}>Student</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Save Changes</button>
</form>
</div>
{% endblock %}