fix: implemented db object to utilize in views

This commit is contained in:
iritmaximus
2023-02-04 18:09:32 +02:00
parent 57ef8989ec
commit 26cf0c8b91
4 changed files with 38 additions and 0 deletions
+15
View File
@@ -1,11 +1,26 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import getenv
import psycopg2
from flaskr.views import general
from flaskr.views import forum
from flaskr.views import users
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
dbname = getenv("POSTGRES_DB")
user = getenv("POSTGRES_USER")
password = getenv("POSTGRES_PASSWORD")
app.config[
"SQLALCHEMY_DATABASE_URI"
] = f"postgresql://{user}:{password}@db:5432/{dbname}"
db = SQLAlchemy(app)
app.register_blueprint(general.mod)
app.register_blueprint(forum.mod)
+16
View File
@@ -0,0 +1,16 @@
import flaskr
def create_db():
print("Initializing the db...")
def test_db():
print("TESTING THE DB ***********************")
with flaskr.app.app_context():
result = flaskr.db.session.execute("SELECT * FROM test").fetchall()
print(result)
return "Working lol"
+6
View File
@@ -1,4 +1,5 @@
from flask import Blueprint
from flaskr import db
mod = Blueprint("general", __name__)
@@ -8,6 +9,11 @@ def test_backend():
return "This is the working backend"
@mod.route("/db")
def test_db_route():
return db.test_db()
@mod.route("/login")
def login():
return "Login to the site"
+1
View File
@@ -1,5 +1,6 @@
click==8.1.3
Flask==2.2.2
Flask-SQLAlchemy==3.0.3
greenlet==2.0.1
gunicorn==20.1.0
itsdangerous==2.1.2