Skip to content

Commit

Permalink
Use the new scoped_session SessionManager
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Aug 1, 2019
1 parent 3106203 commit 12affc7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
24 changes: 3 additions & 21 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,18 @@
import logging
from abc import ABC, abstractmethod

from sqlalchemy import create_engine, asc, desc
from sqlalchemy.orm import sessionmaker
from sqlalchemy import asc, desc

from common.constants import Constants
from common.exceptions import MissingRecordError, BadFilterError, BadRequestError
from common.session_manager import session_manager

log = logging.getLogger()


class SessionManager(object):
_session = None

@staticmethod
def get_icat_db_session():
"""
Checks if a session exists, if it does it returns the session if not a new one is created
:return: ICAT DB session
"""
log.info(" Getting ICAT DB session")
if SessionManager._session is None:
engine = create_engine(Constants.DATABASE_URL)
Session = sessionmaker(bind=engine)
SessionManager._session = Session()
return SessionManager._session


class Query(ABC):
@abstractmethod
def __init__(self, table):
self.session = SessionManager.get_icat_db_session()
self.session = session_manager.get_icat_db_session()
self.table = table
self.base_query = self.session.query(table)
self.is_limited = False
Expand Down
9 changes: 6 additions & 3 deletions common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@
from flask_restful import reqparse
from sqlalchemy.exc import IntegrityError

from common.database_helpers import SessionManager
from common.exceptions import MissingRecordError, BadFilterError, AuthenticationError, BadRequestError
from common.models.db_models import SESSION

from common.session_manager import session_manager

log = logging.getLogger()


def requires_session_id(method):
"""
Decorator for endpoint resources that makes sure a valid session_id is provided in requests to that endpoint
:param method: The method for the endpoint
:returns a 403, "Forbidden" if a valid session_id is not provided with the request
"""
log.info("")

@wraps(method)
def wrapper_requires_session(*args, **kwargs):
log.info(" Authenticating consumer")
try:
session = SessionManager.get_icat_db_session()
session = session_manager.get_icat_db_session()
query = session.query(SESSION).filter(
SESSION.ID == get_session_id_from_auth_header()).first()
if query is not None:
log.info(" Closing DB session")
session.close()
session.close()
log.info(" Consumer authenticated")
return method(*args, **kwargs)
else:
Expand All @@ -38,6 +40,7 @@ def wrapper_requires_session(*args, **kwargs):
return "Forbidden", 403
except AuthenticationError:
return "Forbidden", 403

return wrapper_requires_session


Expand Down

0 comments on commit 12affc7

Please sign in to comment.