-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonfilter.py
38 lines (29 loc) · 884 Bytes
/
jsonfilter.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
35
36
37
38
import json
class CommentJSONFilter:
"""
Creates a prepared JSON list of comments in the
form of a filter for Vue components
"""
def __init__(self, comments):
self.comments = comments
self.prepared_list = []
def _extract_comment(self, c):
data = {
'date': c.locale_date,
'author': c.author.name,
'url': c.url,
'slug': c.slug,
'content': c.content,
'email': c.email,
}
if c.replies:
data['replies'] = [
self._extract_comment(com) for com in c.replies
]
return data
def _iterate_comments(self):
for c in self.comments:
self.prepared_list.append(self._extract_comment(c))
def run(self):
self._iterate_comments()
return json.dumps(self.prepared_list)