def get_categories_by_names(session, top_name, child_names):
'''
Get the top level category by name, and a list of child categories
directly underneath it by their names.
This is a utility function that serves some common functionality in
our various categorization functions. Probably not useful outside
of this module.
'''
try:
top_category = (session.query(Category)
.filter(Category.parent == None)
.filter(Category.name == top_name)
.one())
except MultipleResultsFound as ex:
ex.message = ('Multiple top categories named "{}" found.'
.format(top_name))
ex.args = (ex.message, )
raise ex
except NoResultFound:
ex.message = ('Top category "{}" not found.'.format(top_name))
ex.args = (ex.message, )
raise ex
child_categories = [c for c in top_category.children
if c.name in child_names]
return top_category, child_categories
评论列表
文章目录