Database Reference
In-Depth Information
'posted'
:
datetime
.
utcnow
(),
'author'
:
author_info
,
'text'
:
comment_text
})
To insert a comment for a system with threaded comments, we first need to generate the ap-
propriate
slug
and
full_slug
values based on the parent comment:
posted
=
datetime
.
utcnow
()
# generate the unique portions of the slug and full_slug
slug_part
=
generate_pseudorandom_slug
()
full_slug_part
=
posted
.
strftime
(
'%Y.%m.
%d
.%H.%M.%S'
)
+
':'
+
slug_part
# load the parent comment (if any)
iif
parent_slug
:
parent
=
db
.
comments
.
find_one
(
{
'node_id'
:
node_id
,
'slug'
:
parent_slug
})
slug
=
parent
[
'slug'
]
+
'/'
+
slug_part
full_slug
=
parent
[
'full_slug'
]
+
'/'
+
full_slug_part
else
else
:
slug
=
slug_part
full_slug
=
full_slug_part
# actually insert the comment
db
.
comments
.
insert
({
'node_id'
:
node_id
,
'slug'
:
slug
,
'full_slug'
:
full_slug
,
'posted'
:
posted
,
'author'
:
author_info
,
'text'
:
comment_text
})
Operation: View paginated comments
To view comments that are not threaded, we just need to select all comments participating in
a discussion and sort by the
posted
field. For example:
cursor
=
db
.
comments
.
find
({
'node_id'
:
node_id
})
cursor
=
cursor
.
sort
(
'posted'
)
cursor
=
cursor
.
skip
(
page_num
*
page_size
)
cursor
=
cursor
.
limit
(
page_size
)