-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
fetch_ordered_annotations
into a new annotation service
- Loading branch information
Jon Betts
committed
Apr 5, 2023
1 parent
4696df6
commit 90ed652
Showing
13 changed files
with
219 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Iterable, List, Optional | ||
|
||
from sqlalchemy.orm import subqueryload | ||
|
||
from h.models import Annotation | ||
|
||
|
||
class AnnotationService: | ||
def __init__(self, db_session): | ||
self._db = db_session | ||
|
||
def get_annotations_by_id( | ||
self, ids: List[str], eager_load: Optional[List] = None | ||
) -> Iterable[Annotation]: | ||
""" | ||
Get annotations in the same order as the provided ids. | ||
:param ids: the list of annotation ids | ||
:param eager_load: A list of annotatiopn relationships to eager load | ||
like `Annotation.document` | ||
""" | ||
|
||
if not ids: | ||
return [] | ||
|
||
query = self._db.query(Annotation).filter(Annotation.id.in_(ids)) | ||
|
||
if eager_load: | ||
query = query.options(subqueryload(prop) for prop in eager_load) | ||
|
||
return sorted(query, key=lambda annotation: ids.index(annotation.id)) | ||
|
||
|
||
def service_factory(_context, request): | ||
return AnnotationService(db_session=request.db) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.