-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
119 lines (94 loc) · 3.57 KB
/
Copy pathapp.py
File metadata and controls
119 lines (94 loc) · 3.57 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from flask import Flask, render_template, request, redirect, url_for, session, flash
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
import os
# ---------- APP SETUP ----------
app = Flask(__name__, instance_relative_config=True)
app.secret_key = "nexgenhome_secret_key"
# Ensure instance folder exists
os.makedirs(app.instance_path, exist_ok=True)
# ---------- DATABASE ----------
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.instance_path, 'users.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# ---------- MODEL ----------
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
password_hash = db.Column(db.String(200), nullable=False)
@property
def password(self):
raise AttributeError("Password is write-only!")
@password.setter
def password(self, plain_password):
self.password_hash = generate_password_hash(plain_password)
def verify_password(self, plain_password):
return check_password_hash(self.password_hash, plain_password)
# Create tables if not exist
with app.app_context():
db.create_all()
# ---------- HOME ----------
@app.route('/')
def home():
return render_template('index.html')
# ---------- LOGIN ----------
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
user_input = request.form['username']
password = request.form['password']
user = User.query.filter(
(User.username == user_input) | (User.email == user_input)
).first()
if user and user.verify_password(password):
session['user_id'] = user.id
session['username'] = user.username
return redirect(url_for('room'))
flash("Invalid login details")
return render_template('login.html')
# ---------- SIGNUP ----------
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
if User.query.filter_by(username=username).first():
flash("Username already exists")
return render_template('signup.html')
if User.query.filter_by(email=email).first():
flash("Email already exists")
return render_template('signup.html')
user = User(username=username, email=email)
user.password = password
db.session.add(user)
db.session.commit()
session['user_id'] = user.id
session['username'] = user.username
return redirect(url_for('room'))
return render_template('signup.html')
# ---------- ROOM ----------
@app.route('/room')
def room():
if 'user_id' not in session:
flash("Please login to access rooms.")
return redirect(url_for('login'))
return render_template('Room.html')
# ---------- ABOUT ----------
@app.route('/aboutUS', endpoint='about_us')
def aboutUS():
return render_template('aboutUS.html')
# ---------- CONTACT ----------
@app.route('/contactUS', endpoint='contact_us')
def contactUS():
return render_template('contactUS.html')
# ---------- LOGOUT ----------
@app.route('/logout')
def logout():
session.clear()
flash("You have been logged out.")
return redirect(url_for('home'))
# ---------- RUN ----------
if __name__ == '__main__':
app.run(debug=True)