def get_assets(id, after=None, page: int=1, limit: int=20): assets = Asset.select().order_by(Asset.created.desc()) where = [Asset.publication==id] if after: where.append(Asset.created > arrow.get(after).datetime) assets = assets.where(*where).paginate(page, limit) return [asset.to_dict() for asset in assets]
def list_(): assets = Asset.select().order_by(Asset.created.desc()) return [{ 'comments_count': asset.comments_count, 'pending_comments_count': asset.pending_comments_count, 'rejected_comments_count': asset.rejected_comments_count, **asset.to_dict() } for asset in assets]
def get_assets_with_comment_stats(id, page: int=1, limit: int=20, after=None): assets = Asset.select( Asset, fn.COUNT(PendingComment.id).alias('total_pending_comments') ).where( Asset.publication == id ).join( PendingComment, JOIN.LEFT_OUTER ).order_by( fn.COUNT(PendingComment.id).desc(), Asset.created.desc() ).group_by( Asset.id ).paginate(page, limit) asset_ids = [asset.id for asset in assets] assets_with_rejected_comments_count = RejectedComment.select( RejectedComment.asset_id, fn.COUNT(RejectedComment.id).alias('total_rejected_comments') ).where( RejectedComment.asset_id << asset_ids ).group_by(RejectedComment.asset_id) rejected_comment_counts = { asset.asset_id: asset.total_rejected_comments for asset in assets_with_rejected_comments_count } assets_with_comments_count = Comment.select( Comment.asset_id, fn.COUNT(Comment.id).alias('total_comments') ).where( Comment.asset_id << asset_ids ).group_by(Comment.asset_id) comment_counts = { asset.id: asset.total_comments for asset in assets_with_comments_count } return [ { 'comments_count': comment_counts.get(asset.id, 0), 'pending_comments_count': asset.total_pending_comments, 'rejected_comments_count': rejected_comment_counts.get(asset.id, 0), 'commenting_closed': asset.commenting_closed, **asset.to_dict() } for asset in assets ]
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.id << asset_ids) & ( (Asset.open_till > arrow.utcnow().datetime) | (Asset.id << commented_assets))) asset_ids = [asset.id for asset in assets] featured_comments = commentlib.get_featured_comments_for_assets( asset_ids, no_of_comments) return { 'assets': [{ 'comments_count': asset.comments_count, 'commenting_closed': asset.commenting_closed, 'featured_comments': featured_comments.get(asset.id, []), **asset.to_dict() } for asset in assets] }
def get_open_assets(): open_assets = Asset.select( Asset.url).where(Asset.open_till > arrow.utcnow().datetime).tuples() open_assets = [asset_url for asset_url, in open_assets] return {'count': len(open_assets), 'open_assets': open_assets}
def get_all(ids): assets = Asset.select().where(Asset.id << ids) return [asset.to_dict() for asset in assets]