Advertisement
Guest User

post.py

a guest
Aug 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. from exceptions import PermissionDenied
  2. """
  3. Post:
  4.  
  5. A Post is created by a particular author.
  6. The author is able to edit the content, but other users cannot.
  7. Any user should be able to bump the upvotes, but only once per user.
  8. Thread:
  9. """
  10.  
  11. class Post:
  12. def __init__(self, content, author):
  13. """
  14. Creates a new thread with a title and an initial first post.
  15. The author of the first post at the time of thread creation is the owner of the thread.
  16. The owner cannot change once the thread is created.
  17. """
  18. self.content = content
  19. self.author = author
  20. self.upvotes = 0
  21. self.upvoted = []
  22.  
  23. def get_author(self):
  24. """
  25. Returns the author of the post.
  26. """
  27. return self.author
  28.  
  29. def get_content(self):
  30. """
  31. Returns the content of the post.
  32. """
  33. return self.content
  34.  
  35. def get_upvotes(self):
  36. """
  37. Returns a single integer representing the total number of upvotes.
  38. """
  39. return self.upvotes
  40.  
  41. def set_content(self, content, by_user):
  42. """
  43. Called when the given user wants to update the content.
  44. * raises PermissionDenied if the given user is not the author.
  45. """
  46. if by_user != self.author:
  47. raise PermissionDenied
  48. else:
  49. self.content = content
  50.  
  51. def upvote(self, by_user):
  52. """
  53. Called when the given user wants to upvote this post.
  54. A user can only perform an up vote *once*.
  55. """
  56. if by_user not in self.upvoted:
  57. self.upvoted.append(by_user)
  58. self.upvotes += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement