From f88b6bc6b93388f38c6449f13fa10174b61c28e7 Mon Sep 17 00:00:00 2001 From: Keiran Price Date: Tue, 20 Aug 2019 07:28:57 +0100 Subject: [PATCH] #43: Refactor for readability --- common/models/db_models.py | 45 ++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/common/models/db_models.py b/common/models/db_models.py index 5c39fb2d..d1c50d83 100644 --- a/common/models/db_models.py +++ b/common/models/db_models.py @@ -26,7 +26,7 @@ def to_dict(self): def to_nested_dict(self, includes): """ Given related models return a nested dictionary with the child or parent rows nested. - :param included_relations: string/list/dict - The related models to include. + :param includes: string/list/dict - The related models to include. :return: A nested dictionary with the included models """ dictionary = self.to_dict() @@ -34,23 +34,41 @@ def to_nested_dict(self, includes): includes = includes if type(includes) is list else [includes] for include in includes: if type(include) is str: - related_entity = self.get_related_entity(include) - if not isinstance(related_entity, InstrumentedList): - dictionary[related_entity.__tablename__] = related_entity.to_dict() - else: - for entity in related_entity: - dictionary[f"{entity.__tablename__} {entity.ID}"] = entity.to_dict() + self._nest_string_include(dictionary, include) elif type(include) is dict: - related_entity = self.get_related_entity(list(include)[0]) - if not isinstance(related_entity, InstrumentedList): - dictionary[related_entity.__tablename__] = related_entity.to_nested_dict(include[list(include)[0]]) - else: - for entity in related_entity: - dictionary[f"{entity.__tablename__} {entity.ID}"] = entity.to_nested_dict(include[list(include)[0]]) + self._nest_dictionary_include(dictionary, include) except TypeError: raise BadFilterError(f" Bad include relations provided: {includes}") return dictionary + def _nest_dictionary_include(self, dictionary, include): + """ + Given a dictionary of related entities names, nest the related entities into the given dictionary representation, + of the original entity. + :param dictionary: The dictionary representation of the original entity + :param include: The dictionary of related entity names to be nested. + """ + related_entity = self.get_related_entity(list(include)[0]) + if not isinstance(related_entity, InstrumentedList): + dictionary[related_entity.__tablename__] = related_entity.to_nested_dict(include[list(include)[0]]) + else: + for entity in related_entity: + dictionary[f"{entity.__tablename__} {entity.ID}"] = entity.to_nested_dict(include[list(include)[0]]) + + def _nest_string_include(self, dictionary, include): + """ + Given the name of a single related entity, nest the related entity into the given dictionary representation of + the original entity. + :param dictionary: The dictionary representation of an entity to be nested in. + :param include: The name of the related entity to be nested + """ + related_entity = self.get_related_entity(include) + if not isinstance(related_entity, InstrumentedList): + dictionary[related_entity.__tablename__] = related_entity.to_dict() + else: + for entity in related_entity: + dictionary[f"{entity.__tablename__} {entity.ID}"] = entity.to_dict() + def get_related_entity(self, entity): """ Given a string for the related entity name, return the related entity @@ -558,7 +576,6 @@ class JOB(Base, EntityHelper): backref='JOB') - class KEYWORD(Base, EntityHelper): __tablename__ = 'KEYWORD' __table_args__ = (