comment.py 1.6 KB

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