comment.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. article = None
  14. def __init__(self, content, metadata, settings, source_path, context):
  15. # Strip the path off the full filename.
  16. name = os.path.split(source_path)[1]
  17. if not hasattr(self, 'slug'):
  18. # compute the slug before initializing the base Content object, so
  19. # it doesn't get set there
  20. # This is required because we need a slug containing the file
  21. # extension.
  22. self.slug = slugify(name, settings.get('SLUG_SUBSTITUTIONS', ()))
  23. super(Comment, self).__init__(content, metadata, settings, source_path,
  24. context)
  25. self.replies = []
  26. # Strip the extension from the filename.
  27. name = os.path.splitext(name)[0]
  28. self.avatar = avatars.getAvatarPath(name, metadata)
  29. self.title = "Posted by: {}".format(metadata['author'])
  30. def addReply(self, comment):
  31. self.replies.append(comment)
  32. def getReply(self, slug):
  33. for reply in self.replies:
  34. if reply.slug == slug:
  35. return reply
  36. else:
  37. deepReply = reply.getReply(slug)
  38. if deepReply is not None:
  39. return deepReply
  40. return None
  41. def __lt__(self, other):
  42. return self.date < other.date
  43. def sortReplies(self):
  44. for r in self.replies:
  45. r.sortReplies()
  46. self.replies = sorted(self.replies)
  47. def countReplies(self):
  48. amount = 0
  49. for r in self.replies:
  50. amount += r.countReplies()
  51. return amount + len(self.replies)