Skip to content

Commit

Permalink
Merge pull request #66 from scrolltech/FetchingFeaturedAssets
Browse files Browse the repository at this point in the history
Fetching featured assets by asset ids
  • Loading branch information
shon committed Oct 23, 2019
2 parents 6a6fb73 + 38e816f commit d153fda
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setup_routes(factory):
factory.get('/assets/{id}/replies')(assetlib.get_replies)
factory.get('/assets/{id}/meta')(assetlib.get_meta)
factory.get('/assets/meta')(assetlib.get_assets_meta)
factory.get('/assets/comments/featured')(assetlib.list_with_featured_comments)
factory.get('/assets/comments/featured')(assetlib.get_with_featured_comments)

comment_handlers = (commentlib.list_, None, None, commentlib.get, commentlib.update, None)
factory.map_resource('/comments/', handlers=comment_handlers)
Expand Down
18 changes: 10 additions & 8 deletions app/libs/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,22 @@ def get_comment_view(id, comment_id, user_id: user_id=None):
return view


def list_with_featured_comments(no_of_comments=1, after=None, limit=10):
after = after or arrow.utcnow().datetime
def get_with_featured_comments(asset_ids, no_of_comments=1):
commented_assets = Comment.select(
Comment.asset_id
).where(
Comment.asset_id << asset_ids
).distinct()
assets = Asset.select(
).order_by(
Asset.created.desc()
).where(
(Asset.created < arrow.get(after).datetime) &
(Asset.id << asset_ids) &
(
(Asset.open_till > arrow.utcnow().datetime) |
(Asset.id << (Comment.select(Comment.asset_id)))
(Asset.id << commented_assets)
)
).limit(
limit
).execute()
)
asset_ids = [asset.id for asset in assets]
featured_comments = commentlib.get_featured_comments_for_assets(asset_ids, no_of_comments)

Expand All @@ -275,4 +277,4 @@ def list_with_featured_comments(no_of_comments=1, after=None, limit=10):
for asset in assets
]
}
list_.groups_required = [groups.moderator.value]
get_with_featured_comments.groups_required = [groups.moderator.value]

0 comments on commit d153fda

Please sign in to comment.