-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.old3
95 lines (79 loc) · 3.2 KB
/
app.old3
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from flask import Flask, render_template, request, redirect, url_for, flash, session
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from config import Config
from sqlalchemy import text # Import text for raw SQL queries
import mysql.connector
# Initialize Flask app from configuration
app = Flask(__name__)
app.config.from_object(Config)
# Initialize extensions
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
# Test connection (Create tables if they don’t exist)
with app.app_context():
db.create_all()
# User model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(120), nullable=False)
role = db.Column(db.String(20), nullable=False, default='user') # Role for admin/user differentiation
# Flask-Admin setup
admin = Admin(app, name='Super Agency Admin', template_mode='bootstrap4')
admin.add_view(ModelView(User, db.session)) # Add User model to admin interface
# APP TEST - TEMP
@app.route('/test', methods=['GET', 'POST'])
def test():
try:
# Execute raw SQL using text() for explicit raw query
result = db.session.execute(text("SELECT superName, superEmail FROM superTest LIMIT 1")).fetchone()
if result:
# Extract fields from the query result
super_name, super_email = result
return render_template('test.html', super_name=super_name, super_email=super_email)
else:
return "No data found in the table."
except Exception as e:
print(f"Error occurred: {e}")
return f"Error occurred: {e}"
# Route: Home
@app.route('/')
def home():
return render_template('index.html')
######## LOGIN ########
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username).first()
if user and bcrypt.check_password_hash(user.password, password):
flash('Logged in successfully!', 'success')
return redirect(url_for('home'))
flash('Invalid credentials, please try again.', 'danger')
return render_template('login.html')
# Route: Register
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
# Add user to database
new_user = User(username=username, password=hashed_password)
db.session.add(new_user)
db.session.commit()
flash('Account created successfully! You can now log in.', 'success')
return redirect(url_for('login'))
return render_template('register.html')
# For Debug Mode
if __name__ == '__main__':
with app.app_context():
db.create_all() # Ensure tables are created
app.run(debug=True) # Enable debug mode only for the server
# Print the URL map to verify routes
print("Registered routes:")
print(app.url_map)