finyally get the DB done
just wannya go home
This commit is contained in:
parent
0e8121fcdb
commit
8c270bd24b
3 changed files with 60 additions and 67 deletions
59
app/app.py
59
app/app.py
|
@ -1,14 +1,14 @@
|
||||||
from urllib import request
|
from flask import request, redirect, session, render_template, send_from_directory, Flask
|
||||||
|
|
||||||
from flask import *
|
|
||||||
from os import path, walk
|
from os import path, walk
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine, Column, Integer, String
|
||||||
from sqlalchemy.orm import scoped_session,sessionmaker
|
from sqlalchemy.orm import scoped_session,sessionmaker
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy.sql import text
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read('config.ini')
|
config.read('config.ini')
|
||||||
|
@ -20,8 +20,18 @@ databaseUsername = str(config['DATABASE']['username'])
|
||||||
databasePassword = str(config['DATABASE']['password'])
|
databasePassword = str(config['DATABASE']['password'])
|
||||||
databaseName = str(config['DATABASE']['name'])
|
databaseName = str(config['DATABASE']['name'])
|
||||||
|
|
||||||
engine=create_engine("postgresql://" + databaseUsername + ":" + databasePassword + "@localhost/" + databaseName)
|
engine = create_engine(f"postgresql://{databaseUsername}:{databasePassword}@localhost/{databaseName}")
|
||||||
db=scoped_session(sessionmaker(bind=engine))
|
db = scoped_session(sessionmaker(bind=engine))
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = 'users'
|
||||||
|
id = Column(Integer, primary_key=True)
|
||||||
|
username = Column(String, unique=True, nullable=False)
|
||||||
|
password = Column(String, nullable=False)
|
||||||
|
|
||||||
|
Base.metadata.create_all(engine)
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
@ -40,14 +50,47 @@ def home():
|
||||||
return render_template('index.j2', instanceLocation=instanceLocation, instanceBranding=instanceBranding)
|
return render_template('index.j2', instanceLocation=instanceLocation, instanceBranding=instanceBranding)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/auth/login/')
|
@app.route('/auth/login/', methods=['GET', 'POST'])
|
||||||
def login():
|
def login():
|
||||||
|
if request.method == 'POST':
|
||||||
|
username = request.form['username']
|
||||||
|
password = encrypt(request.form['password'])
|
||||||
|
|
||||||
|
user = db.execute(text("SELECT * FROM users WHERE username = :username AND password = :password"),
|
||||||
|
{"username": username, "password": password}).fetchone()
|
||||||
|
|
||||||
|
if user:
|
||||||
|
session['user_id'] = user.id
|
||||||
|
return redirect('/')
|
||||||
|
else:
|
||||||
|
return "Invalid credentials!"
|
||||||
return render_template('login.j2', instanceLocation=instanceLocation, instanceBranding=instanceBranding)
|
return render_template('login.j2', instanceLocation=instanceLocation, instanceBranding=instanceBranding)
|
||||||
|
|
||||||
@app.route('/auth/register/')
|
@app.route('/auth/register/', methods=['GET', 'POST'])
|
||||||
def register():
|
def register():
|
||||||
|
if request.method == 'POST':
|
||||||
|
username = request.form['username']
|
||||||
|
password = encrypt(request.form['password'])
|
||||||
|
print(username)
|
||||||
|
|
||||||
|
# check if username exists
|
||||||
|
existing_user = db.execute(text("SELECT * FROM users WHERE username = :username"),
|
||||||
|
{"username": username}).fetchone()
|
||||||
|
if existing_user:
|
||||||
|
return "User already exists!"
|
||||||
|
|
||||||
|
# insert new user
|
||||||
|
db.execute(text("INSERT INTO users (username, password) VALUES (:username, :password)"),
|
||||||
|
{"username": username, "password": password})
|
||||||
|
db.commit()
|
||||||
|
return redirect('/auth/login/') # redirect to login page after successful registration
|
||||||
return render_template('register.j2', instanceLocation=instanceLocation, instanceBranding=instanceBranding)
|
return render_template('register.j2', instanceLocation=instanceLocation, instanceBranding=instanceBranding)
|
||||||
|
|
||||||
|
@app.route('/auth/logout/')
|
||||||
|
def logout():
|
||||||
|
session.pop('user_id', None)
|
||||||
|
return redirect('/')
|
||||||
|
|
||||||
@app.route('/assets/css/index.css')
|
@app.route('/assets/css/index.css')
|
||||||
def index_css():
|
def index_css():
|
||||||
return send_from_directory('static/assets/css', 'index.css')
|
return send_from_directory('static/assets/css', 'index.css')
|
||||||
|
|
|
@ -11,19 +11,11 @@
|
||||||
<h1>
|
<h1>
|
||||||
Login to {{ instanceLocation }}'s Nyxask!
|
Login to {{ instanceLocation }}'s Nyxask!
|
||||||
</h1>
|
</h1>
|
||||||
<label for="name">Username: </label>
|
<form method="POST">
|
||||||
<input
|
<input type="text" name="username" placeholder="Username" required>
|
||||||
type="text"
|
<input type="password" name="password" placeholder="Password" required>
|
||||||
id="name"
|
<button type="submit">Login</button>
|
||||||
name="name"
|
</form>
|
||||||
size="10" />
|
|
||||||
<br>
|
|
||||||
<label for="password">Password: </label>
|
|
||||||
<input
|
|
||||||
type="password"
|
|
||||||
id="password"
|
|
||||||
name="password"
|
|
||||||
size="10" />
|
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -11,52 +11,10 @@
|
||||||
<h1>
|
<h1>
|
||||||
Register at {{ instanceLocation }}
|
Register at {{ instanceLocation }}
|
||||||
</h1>
|
</h1>
|
||||||
<form>
|
<form method="POST">
|
||||||
<label>
|
<input type="text" name="username" placeholder="Username" required>
|
||||||
<input
|
<input type="password" name="password" placeholder="Password" required>
|
||||||
placeholder="username"
|
<button type="submit">register</button>
|
||||||
type="text"
|
|
||||||
name="username"
|
|
||||||
size="10"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<label>
|
|
||||||
<input
|
|
||||||
placeholder="password"
|
|
||||||
type="password"
|
|
||||||
name="password"
|
|
||||||
size="10"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label>
|
|
||||||
<input
|
|
||||||
placeholder="confirm password"
|
|
||||||
type="password"
|
|
||||||
name="confirm"
|
|
||||||
size="10"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<label>
|
|
||||||
Agree to the rules
|
|
||||||
<input
|
|
||||||
type="checkbox"
|
|
||||||
name="scales"
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<input
|
|
||||||
type="submit"
|
|
||||||
value="Register"
|
|
||||||
/>
|
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|
Loading…
Add table
Reference in a new issue