-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
34 lines (27 loc) · 1.08 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Utility functions that don't handle paging. Keep
all the helper functions here and the app handlers
in the main script.
"""
from bson.objectid import ObjectId
from flask import render_template
"""
Get posts from the database in reverse order.
"""
def get_posts(client, channel, start, length):
cursor = client.messages.find({"channel": channel}, skip=int(start), limit=int(length), sort=[("postNumber", -1)])
messages = list(cursor)[::-1]
return messages
def get_one_post(client, ID):
return client.messages.find_one({"_id": ObjectId(ID)})
# Get all the comments for a post
def get_comments(client, ID, start, length):
references = client.comments.find({"refPost": ObjectId(ID)})
basePostIDs = [reference["basePost"] for reference in references]
basePosts = client.messages.find({ "_id": { "$in": basePostIDs } }, skip=int(start), limit=int(length), sort=[("time", -1)])
comments = list(basePosts)[::-1]
return comments
def getPostsHTML(posts):
return render_template("posts.html", messages=posts)
def channelDoesExist(client, channel):
return channel in client.channels.distinct("_id")