Skip to content

Instantly share code, notes, and snippets.

@mikeywaites
Last active December 16, 2015 13:39
Show Gist options
  • Save mikeywaites/5443278 to your computer and use it in GitHub Desktop.
Save mikeywaites/5443278 to your computer and use it in GitHub Desktop.
def get_latest_comments(self, event_ids, limit=3, *args, **kwargs):
"""Get the last 3 comments added and re-orders them into ASC
order
:param event_ids: a list of ints
:returns: RawQuerySet
"""
SQL = """
SELECT * FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY event_id ORDER BY created DESC)
AS rank, sub_comment.* FROM feed_actioncomment sub_comment
) comment
WHERE comment.rank <= %s AND event_id IN (%s) AND is_deleted=false
ORDER BY created ASC
"""
comments = self.raw(SQL, [limit, ','.join(map(str, event_ids))])
return comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment