Spaces:
Runtime error
Runtime error
Upload app (2).py
Browse files- app (2).py +75 -0
app (2).py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, redirect, url_for, session, flash
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Create Flask app
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Set secret key (use env var in production)
|
| 10 |
+
app.secret_key = os.environ.get("SECRET_KEY", os.urandom(24))
|
| 11 |
+
|
| 12 |
+
# In-memory user "database" (for demo/demo purposes only — do NOT use in production)
|
| 13 |
+
users = {}
|
| 14 |
+
|
| 15 |
+
@app.route('/')
|
| 16 |
+
def home():
|
| 17 |
+
if 'username' in session:
|
| 18 |
+
return redirect(url_for('dashboard'))
|
| 19 |
+
return redirect(url_for('login'))
|
| 20 |
+
|
| 21 |
+
@app.route('/signup', methods=['GET', 'POST'])
|
| 22 |
+
def signup():
|
| 23 |
+
if request.method == 'POST':
|
| 24 |
+
username = request.form.get('username')
|
| 25 |
+
password = request.form.get('password')
|
| 26 |
+
|
| 27 |
+
if username in users:
|
| 28 |
+
flash('Username already exists! Try logging in.', 'error')
|
| 29 |
+
return redirect(url_for('signup'))
|
| 30 |
+
|
| 31 |
+
users[username] = {'password': password, 'created_at': datetime.utcnow()}
|
| 32 |
+
flash('Signup successful! Please login.', 'success')
|
| 33 |
+
return redirect(url_for('login'))
|
| 34 |
+
|
| 35 |
+
return render_template('signup.html')
|
| 36 |
+
|
| 37 |
+
@app.route('/login', methods=['GET', 'POST'])
|
| 38 |
+
def login():
|
| 39 |
+
if request.method == 'POST':
|
| 40 |
+
username = request.form.get('username')
|
| 41 |
+
password = request.form.get('password')
|
| 42 |
+
|
| 43 |
+
user = users.get(username)
|
| 44 |
+
if user and user['password'] == password:
|
| 45 |
+
session['username'] = username
|
| 46 |
+
flash(f'Welcome back, {username}!', 'success')
|
| 47 |
+
return redirect(url_for('dashboard'))
|
| 48 |
+
else:
|
| 49 |
+
flash('Invalid username or password', 'error')
|
| 50 |
+
return redirect(url_for('login'))
|
| 51 |
+
|
| 52 |
+
return render_template('login.html')
|
| 53 |
+
|
| 54 |
+
@app.route('/dashboard')
|
| 55 |
+
def dashboard():
|
| 56 |
+
if 'username' not in session:
|
| 57 |
+
flash('Please login to access dashboard.', 'error')
|
| 58 |
+
return redirect(url_for('login'))
|
| 59 |
+
|
| 60 |
+
username = session['username']
|
| 61 |
+
portfolio_data = {
|
| 62 |
+
'projects': ['Project A', 'Project B', 'Project C'],
|
| 63 |
+
'last_login': datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
|
| 64 |
+
}
|
| 65 |
+
return render_template('dashboard.html', username=username, portfolio=portfolio_data)
|
| 66 |
+
|
| 67 |
+
@app.route('/logout')
|
| 68 |
+
def logout():
|
| 69 |
+
session.pop('username', None)
|
| 70 |
+
flash('You have logged out.', 'success')
|
| 71 |
+
return redirect(url_for('login'))
|
| 72 |
+
|
| 73 |
+
# Only used if running directly (not with Gunicorn)
|
| 74 |
+
if __name__ == '__main__':
|
| 75 |
+
app.run(host='0.0.0.0', port=8080)
|