forked from getpelican/pelican-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment.py
66 lines (51 loc) · 1.88 KB
/
comment.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
"""
Author: Bernhard Scheirle
"""
from __future__ import unicode_literals
import os
from pelican.contents import Content
from pelican.utils import slugify
from . import avatars
class Comment(Content):
mandatory_properties = ('author', 'date')
default_template = 'None'
article = None
def __init__(self, content, metadata, settings, source_path, context):
# Strip the path off the full filename.
name = os.path.split(source_path)[1]
if not hasattr(self, 'slug'):
# compute the slug before initializing the base Content object, so
# it doesn't get set there
# This is required because we need a slug containing the file
# extension.
self.slug = slugify(name, settings.get('SLUG_SUBSTITUTIONS', ()))
super(Comment, self).__init__(content, metadata, settings, source_path,
context)
self.replies = []
# Strip the extension from the filename.
name = os.path.splitext(name)[0]
self.avatar = avatars.getAvatarPath(name, metadata)
self.title = "Posted by: {}".format(metadata['author'])
def addReply(self, comment):
self.replies.append(comment)
def getReply(self, slug):
for reply in self.replies:
if reply.slug == slug:
return reply
else:
deepReply = reply.getReply(slug)
if deepReply is not None:
return deepReply
return None
def __lt__(self, other):
return self.date < other.date
def sortReplies(self):
for r in self.replies:
r.sortReplies()
self.replies = sorted(self.replies)
def countReplies(self):
amount = 0
for r in self.replies:
amount += r.countReplies()
return amount + len(self.replies)