Spaces:
Sleeping
Sleeping
| from flask import Flask, render_template, request, redirect, url_for, session | |
| import sqlite3 | |
| import os | |
| from werkzeug.security import generate_password_hash, check_password_hash | |
| app = Flask(__name__) | |
| app.secret_key = 'your_secret_key' # Replace with a secure key | |
| # Database setup | |
| def get_db_connection(): | |
| conn = sqlite3.connect('traffic_management.db') | |
| conn.row_factory = sqlite3.Row | |
| return conn | |
| # Create database and users table if not exists | |
| def init_db(): | |
| conn = get_db_connection() | |
| conn.execute(''' | |
| CREATE TABLE IF NOT EXISTS users ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| username TEXT UNIQUE NOT NULL, | |
| password TEXT NOT NULL | |
| ) | |
| ''') | |
| conn.commit() | |
| conn.close() | |
| # Initialize database | |
| init_db() | |
| # Upload folder setup | |
| UPLOAD_FOLDER = 'uploads' | |
| app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
| if not os.path.exists(UPLOAD_FOLDER): | |
| os.makedirs(UPLOAD_FOLDER) | |
| # Routes | |
| def index(): | |
| return render_template('index.html') | |
| def main_dashboard(): | |
| if 'username' not in session: | |
| session['referrer'] = 'main' | |
| return redirect(url_for('login')) | |
| return render_template('main.html') | |
| def login(): | |
| if request.method == 'POST': | |
| username = request.form['username'] | |
| password = request.form['password'] | |
| conn = get_db_connection() | |
| user = conn.execute('SELECT * FROM users WHERE username = ?', (username,)).fetchone() | |
| conn.close() | |
| if user and check_password_hash(user['password'], password): | |
| session['username'] = user['username'] | |
| redirect_page = session.pop('referrer', 'index') | |
| return redirect(url_for(redirect_page)) | |
| else: | |
| return render_template('login.html', error="Invalid username or password.") | |
| else: | |
| if 'referrer' not in session and request.referrer and 'login' not in request.referrer and 'signup' not in request.referrer: | |
| session['referrer'] = request.referrer.split('/')[-1] or 'index' | |
| return render_template('login.html') | |
| def signup(): | |
| if request.method == 'POST': | |
| username = request.form['username'] | |
| password = request.form['password'] | |
| confirm_password = request.form['confirm_password'] | |
| if password != confirm_password: | |
| return "<script>alert('Passwords do not match!'); window.history.back();</script>" | |
| hashed_password = generate_password_hash(password) | |
| conn = get_db_connection() | |
| try: | |
| conn.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, hashed_password)) | |
| conn.commit() | |
| return redirect(url_for('login')) | |
| except sqlite3.IntegrityError: | |
| return "<script>alert('Username already exists!'); window.history.back();</script>" | |
| finally: | |
| conn.close() | |
| return render_template('signup.html') | |
| def logout(): | |
| session.clear() | |
| return redirect(url_for('index')) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860, debug=True) |