Skip to content

Commit

Permalink
#154: Add function to get instance of DB model entity from a name
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Oct 22, 2020
1 parent c2e7497 commit 17f9eec
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion common/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from sqlalchemy.orm import relationship
from sqlalchemy.orm.collections import InstrumentedList

from common.exceptions import FilterError, DatabaseError
from common.exceptions import FilterError, DatabaseError, ApiError

Base = declarative_base()

Expand Down Expand Up @@ -58,6 +58,25 @@ class EntityHelper(object):
EntityHelper class that contains methods to be shared across all entities
"""

@staticmethod
def get_entity_object_from_name(entity_name):
"""
From an entity name, this function gets a Python version of that entity for the
database backend
:param entity_name: Name of the entity to fetch a version from this model
:type entity_name: :class:`str`
:return: Object of the entity requested (e.g.
:class:`common.database.models.INVESTIGATIONINSTRUMENT`)
:raises: KeyError: If an entity model cannot be found as a class in this model
"""
try:
return globals()[entity_name.upper()]
except KeyError:
raise ApiError(
f"Entity class cannot be found. Please create a class for {entity_name}"
)

def to_dict(self):
"""
Turns the columns and values of an entity into a dictionary
Expand Down

0 comments on commit 17f9eec

Please sign in to comment.