Removed circular import for DB

This commit is contained in:
Mart van Santen 2022-03-22 15:56:36 +08:00
parent 755cb03aaf
commit d09d9d77bd
5 changed files with 88 additions and 4 deletions

18
app.py
View file

@ -3,6 +3,7 @@ from flask_jwt_extended import JWTManager
from flask_cors import CORS
from jsonschema.exceptions import ValidationError
from werkzeug.exceptions import BadRequest
from flask_sqlalchemy import SQLAlchemy
# These imports are required
from areas import api_v1
@ -13,6 +14,7 @@ from areas import apps
from areas import auth
from areas import login
from database import db
from helpers import (
BadRequest,
@ -23,14 +25,28 @@ from helpers import (
kratos_error,
global_error,
hydra_error,
KratosUser
KratosUser,
App,
AppRole
)
from config import *
import logging
app = Flask(__name__)
cors = CORS(app)
app.config["SECRET_KEY"] = SECRET_KEY
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
## from database import db
#db = SQLAlchemy()
db.init_app(app)
# Late beceuse of circular import
##
app.logger.setLevel(logging.INFO)

View file

@ -41,9 +41,15 @@ from helpers import (
kratos_error,
global_error,
hydra_error,
KratosUser
KratosUser,
App,
AppRole
)
# This is a circular import and should be solved differently
#from app import db
from database import db
# APIs
# Create HYDRA & KRATOS API interfaces
HYDRA = hydra_client.HydraAdmin(HYDRA_ADMIN_URL)
@ -240,8 +246,7 @@ def consent():
abort(401, description="User not found. Please try again.")
# Get role on this app
#app_obj = db.session.query(App).filter(App.slug == app_id).first()
app_obj = False
app_obj = db.session.query(App).filter(App.slug == app_id).first()
# Default access level
roles = []
@ -253,6 +258,7 @@ def consent():
)
for role_obj in role_objects:
roles.append(role_obj.role)
current_app.logger.info(f"Using '{roles}' when applying consent for {kratos_id}")
# Get claims for this user, provided the current app

7
database.py Normal file
View file

@ -0,0 +1,7 @@
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

View file

@ -2,3 +2,4 @@ from .kratos_api import *
from .error_handler import *
from .hydra_oauth import *
from .kratos import *
from .models import *

54
helpers/models.py Normal file
View file

@ -0,0 +1,54 @@
"""
Implement different models used by Stackspin panel
"""
from flask import current_app
from flask_sqlalchemy import SQLAlchemy
# pylint: disable=cyclic-import
# This is based on the documentation of Flask Alchemy
#from app import db
# We need this import at some point to hook up roles and users
# from sqlalchemy.orm import relationship
from sqlalchemy import ForeignKey, Integer, String
db = SQLAlchemy()
# Pylint complains about too-few-public-methods. Methods will be added once
# this is implemented.
# pylint: disable=too-few-public-methods
class App(db.Model):
"""
The App object, interact with the App database object. Data is stored in
the local database.
"""
id = db.Column(Integer, primary_key=True)
name = db.Column(String(length=64))
slug = db.Column(String(length=64), unique=True)
def __repr__(self):
return f"{self.id} <{self.name}>"
# Pylint complains about too-few-public-methods. Methods will be added once
# this is implemented.
# pylint: disable=too-few-public-methods
class AppRole(db.Model):
"""
The AppRole object, stores the roles Users have on Apps
"""
# pylint: disable=no-member
user_id = db.Column(String(length=64), primary_key=True)
# pylint: disable=no-member
app_id = db.Column(Integer, ForeignKey('app.id'),
primary_key=True)
# pylint: disable=no-member
role = db.Column(String(length=64))
def __repr__(self):
return f"{self.role} for {self.user_id} on {self.app_id}"