mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-10-26 19:01:39 -04:00
Removing duplicate sorting code.
This commit is contained in:
parent
3f7145c940
commit
fb1f1c6926
1 changed files with 8 additions and 6 deletions
14
wrolpi/db.py
14
wrolpi/db.py
|
|
@ -117,12 +117,14 @@ def get_ranked_models(ranked_ids: List[int], model: Base, session: Session = Non
|
|||
"""
|
||||
Get all objects whose ids are in the `ranked_ids`, order them by their position in `ranked_ids`.
|
||||
"""
|
||||
|
||||
def _(s):
|
||||
r = s.query(model).filter(model.id.in_(ranked_ids)).all()
|
||||
r = sorted(r, key=lambda i: ranked_ids.index(i.id))
|
||||
return r
|
||||
|
||||
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
|
||||
return _(session)
|
||||
|
||||
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))
|
||||
return results
|
||||
return _(session)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue