(feat): Added PostgreSQL connection string encoding.
This commit is contained in:
parent
32dcace985
commit
51f154ab73
5 changed files with 45 additions and 32 deletions
|
|
@ -1,5 +1,22 @@
|
|||
import os
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask_bcrypt import Bcrypt
|
||||
from dotenv import load_dotenv
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
load_dotenv()
|
||||
|
||||
PG_USER = quote_plus(os.getenv('PG_USER'))
|
||||
PG_PASSWORD = quote_plus(os.getenv('PG_PASSWORD'))
|
||||
PG_HOST = os.getenv('PG_HOST')
|
||||
PG_PORT = os.getenv('PG_PORT')
|
||||
PG_DATABASE = os.getenv('PG_DATABASE')
|
||||
|
||||
DATABASE_URI = f"postgresql+psycopg2://{PG_USER}:{PG_PASSWORD}@{PG_HOST}:{PG_PORT}/{PG_DATABASE}"
|
||||
|
||||
db = SQLAlchemy()
|
||||
bcrypt = Bcrypt()
|
||||
|
||||
def init_db(app):
|
||||
"""Initialize the SQLAlchemy app with the configuration."""
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
db.init_app(app)
|
||||
|
|
|
|||
|
|
@ -1,8 +1,23 @@
|
|||
from models import db
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
|
||||
class User(db.Model):
|
||||
__tablename__ = 'users'
|
||||
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||
password = db.Column(db.String(128), nullable=False)
|
||||
|
||||
def __init__(self, username, password, hash_password=True):
|
||||
self.username = username
|
||||
# Optionally hash the password automatically.
|
||||
if hash_password:
|
||||
self.password = generate_password_hash(password, method="pbkdf2:sha256")
|
||||
else:
|
||||
self.password = password
|
||||
|
||||
def check_password(self, password):
|
||||
return check_password_hash(self.password, password)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<User {self.username}>"
|
||||
Loading…
Add table
Add a link
Reference in a new issue