Fixing video search bug.

This commit is contained in:
Roland 2021-12-31 20:28:51 -07:00
parent a95cbd74c9
commit 3f7145c940
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
2 changed files with 9 additions and 3 deletions

View file

@ -139,8 +139,9 @@ def video_search(
total = results[0]['total'] if results else 0
ranked_ids = [i['id'] for i in results]
results = get_ranked_models(ranked_ids, Video)
results = [i.__json__() for i in results]
with get_db_session() as session:
results = get_ranked_models(ranked_ids, Video, session=session)
results = [i.__json__() for i in results]
return results, total

View file

@ -113,10 +113,15 @@ def get_db_curs(commit: bool = False):
connection.rollback()
def get_ranked_models(ranked_ids: List[int], model: Base) -> List[Base]:
def get_ranked_models(ranked_ids: List[int], model: Base, session: Session = None) -> List[Base]:
"""
Get all objects whose ids are in the `ranked_ids`, order them by their position in `ranked_ids`.
"""
if session:
results = session.query(model).filter(model.id.in_(ranked_ids)).all()
results = sorted(results, key=lambda i: ranked_ids.index(i.id))
return results
with get_db_session() as session:
results = session.query(model).filter(model.id.in_(ranked_ids)).all()
results = sorted(results, key=lambda i: ranked_ids.index(i.id))