Advertisement
Guest User

thread.py

a guest
Aug 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. from exceptions import PermissionDenied
  2. """
  3. Thread:
  4.  
  5. A Thread is created with a title, and a first post.
  6. The owner of the thread is the author of the first post.
  7. Any user can add a new post to the thread.
  8. A user can only remove their own post from the thread.
  9. The thread owner can edit the title and the tags, but other users cannot.
  10. """
  11.  
  12. class Thread:
  13. def __init__(self, title, first_post): #First post is an object.
  14. """
  15. Creates a new thread with a title and an initial first post.
  16. The author of the first post is also the owner of the thread.
  17. The owner cannot change once the thread is created.
  18. """
  19. self.owner = first_post.author
  20. self.tags = []
  21. self.title = title
  22. self.posts = [first_post]
  23.  
  24. def get_owner(self):
  25. """
  26. Returns the owner of the thread.
  27. """
  28. return self.owner
  29.  
  30. def get_title(self):
  31. """
  32. Returns the title of the thread.
  33. """
  34. return self.title
  35.  
  36. def get_tags(self):
  37. """
  38. Returns a sorted list of unique tags.
  39. """
  40. return sorted(self.tags)
  41.  
  42. def get_posts(self):
  43. """
  44. Returns a list of posts in this thread, in the order they were published.
  45. """
  46. return self.posts
  47.  
  48. def publish_post(self, post):
  49. """
  50. Adds the given post object into the list of posts.
  51. """
  52. self.posts.append(post)
  53.  
  54. def remove_post(self, post, by_user):
  55. """
  56. Allows the given user to remove the post from this thread.
  57. Does nothing if the post is not in this thread.
  58. * raises PermissionDenied if the given user is not the author of the post.
  59. """
  60.  
  61. if post in self.posts:
  62. if by_user == post.author:
  63. location = self.posts.index(post)
  64. del self.posts[location]
  65. else:
  66. raise PermissionDenied
  67.  
  68. def set_title(self, title, by_user):
  69. """
  70. Allows the given user to edit the thread title.
  71. * raises PermissionDenied if the given user is not the owner of the thread.
  72. """
  73. if by_user == self.posts[0].author:
  74. self.title = title
  75. else:
  76. raise PermissionDenied
  77.  
  78. def set_tags(self, tag_list, by_user):
  79. """
  80. Allows the given user to replace the thread tags (list of strings).
  81. * raises PermissionDenied if the given user is not the owner of the thread.
  82. """
  83. if by_user == self.owner:
  84. seen = []
  85. for x in tag_list:
  86. if x not in seen:
  87. seen.append(x)
  88. self.tags = seen
  89. else:
  90. raise PermissionDenied
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement