-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (57 loc) · 2.38 KB
/
Copy pathapp.py
File metadata and controls
73 lines (57 loc) · 2.38 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
from flask import Flask, render_template, request, redirect, url_for
import os
app = Flask(__name__)
# File to store user data
USER_DATA_FILE = 'user_data.txt'
# Function to read user data from the file
def read_user_data():
if not os.path.exists(USER_DATA_FILE):
return []
with open(USER_DATA_FILE, 'r') as file:
users = file.readlines()
users = [user.strip().split(":") for user in users] # Split username and password by ':'
return users
# Function to write new user data to the file
def write_user_data(username, password, role):
with open(USER_DATA_FILE, 'a') as file:
file.write(f"{username}:{password}:{role}\n")
@app.route('/')
def login():
return render_template('index.html')
@app.route('/login', methods=['POST'])
def login_post():
username = request.form['username']
password = request.form['password']
role = request.form['role'] # Get the role (student or teacher)
users = read_user_data()
for user in users:
if user[0] == username and user[1] == password:
if user[2] == 'student' and role == 'student':
return redirect(url_for('student_dashboard')) # Redirect to student dashboard
elif user[2] == 'teacher' and role == 'teacher':
return redirect(url_for('teacher_dashboard')) # Redirect to teacher dashboard
return "Invalid credentials or role. Please try again."
@app.route('/create-account')
def create_account():
return render_template('create_account.html')
@app.route('/create-account', methods=['POST'])
def create_account_post():
username = request.form['username']
password = request.form['password']
role = request.form['role'] # User's role when creating the account
# Check if the username already exists
users = read_user_data()
for user in users:
if user[0] == username:
return "Username already exists. Please choose another."
# Store the new user data
write_user_data(username, password, role)
return "Account successfully created!"
@app.route('/student-dashboard')
def student_dashboard():
return render_template('student_dashboard.html')
@app.route('/teacher-dashboard')
def teacher_dashboard():
return render_template('teacher_dashboard.html')
if __name__ == '__main__':
app.run(debug=True)