comment.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. """
  3. Author: Bernhard Scheirle
  4. """
  5. from __future__ import unicode_literals
  6. import os
  7. from pelican.contents import Content
  8. from pelican.utils import slugify
  9. from . import avatars
  10. class Comment(Content):
  11. mandatory_properties = ('author', 'date')
  12. default_template = 'None'
  13. def __init__(self, content, metadata, settings, source_path, context):
  14. # Strip the path off the full filename.
  15. name = os.path.split(source_path)[1]
  16. if not hasattr(self, 'slug'):
  17. # compute the slug before initializing the base Content object, so
  18. # it doesn't get set there
  19. # This is required because we need a slug containing the file
  20. # extension.
  21. self.slug = slugify(name, settings.get('SLUG_SUBSTITUTIONS', ()))
  22. super(Comment, self).__init__(content, metadata, settings, source_path,
  23. context)
  24. self.replies = []
  25. # Strip the extension from the filename.
  26. name = os.path.splitext(name)[0]
  27. self.avatar = avatars.getAvatarPath(name, metadata)
  28. self.title = "Posted by: {}".format(metadata['author'])
  29. def addReply(self, comment):
  30. self.replies.append(comment)
  31. def getReply(self, slug):
  32. for reply in self.replies:
  33. if reply.slug == slug:
  34. return reply
  35. else:
  36. deepReply = reply.getReply(slug)
  37. if deepReply is not None:
  38. return deepReply
  39. return None
  40. def __lt__(self, other):
  41. return self.metadata['date'] < other.metadata['date']
  42. def sortReplies(self):
  43. for r in self.replies:
  44. r.sortReplies()
  45. self.replies = sorted(self.replies)
  46. def countReplies(self):
  47. amount = 0
  48. for r in self.replies:
  49. amount += r.countReplies()
  50. return amount + len(self.replies)