Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from exceptions import PermissionDenied
- """
- Thread:
- A Thread is created with a title, and a first post.
- The owner of the thread is the author of the first post.
- Any user can add a new post to the thread.
- A user can only remove their own post from the thread.
- The thread owner can edit the title and the tags, but other users cannot.
- """
- class Thread:
- def __init__(self, title, first_post): #First post is an object.
- """
- Creates a new thread with a title and an initial first post.
- The author of the first post is also the owner of the thread.
- The owner cannot change once the thread is created.
- """
- self.owner = first_post.author
- self.tags = []
- self.title = title
- self.posts = [first_post]
- def get_owner(self):
- """
- Returns the owner of the thread.
- """
- return self.owner
- def get_title(self):
- """
- Returns the title of the thread.
- """
- return self.title
- def get_tags(self):
- """
- Returns a sorted list of unique tags.
- """
- return sorted(self.tags)
- def get_posts(self):
- """
- Returns a list of posts in this thread, in the order they were published.
- """
- return self.posts
- def publish_post(self, post):
- """
- Adds the given post object into the list of posts.
- """
- self.posts.append(post)
- def remove_post(self, post, by_user):
- """
- Allows the given user to remove the post from this thread.
- Does nothing if the post is not in this thread.
- * raises PermissionDenied if the given user is not the author of the post.
- """
- if post in self.posts:
- if by_user == post.author:
- location = self.posts.index(post)
- del self.posts[location]
- else:
- raise PermissionDenied
- def set_title(self, title, by_user):
- """
- Allows the given user to edit the thread title.
- * raises PermissionDenied if the given user is not the owner of the thread.
- """
- if by_user == self.posts[0].author:
- self.title = title
- else:
- raise PermissionDenied
- def set_tags(self, tag_list, by_user):
- """
- Allows the given user to replace the thread tags (list of strings).
- * raises PermissionDenied if the given user is not the owner of the thread.
- """
- if by_user == self.owner:
- seen = []
- for x in tag_list:
- if x not in seen:
- seen.append(x)
- self.tags = seen
- else:
- raise PermissionDenied
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement