Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from post import Post
- from thread import Thread
- """
- Forum:
- The Forum contains a list of threads.
- Users can search for threads by tag.
- Users can search for posts by author.
- """
- class Forum:
- def __init__(self):
- """
- Perform initialisation of a new forum object, as needed.
- """
- self.theThread = None
- self.forum = [] #[{title: title of thread author: threads author ,tags: threads tags ,thread: thread object}]
- def get_threads(self):
- """
- Returns a list of threads in the forum, in the order that they were published.
- """
- return [x['thread'] for x in self.forum]
- def publish(self, title, content, author):
- """
- Creates a new thread with the given title and adds it to the forum.
- The content and author are provided to allow you to create the first post object.
- Forum threads are stored in the order that they are published.
- Returns the new thread.
- """
- #pass to thread title and post object.
- postObj = Post(content,author)
- self.theThread = Thread(title,postObj)
- self.forum.append({'title':title, 'author':author,'thread':self.theThread})
- return self.theThread
- def search_by_tag(self, tag):
- """
- Searches all forum threads for any that contain the given tag.
- Returns a list of matching thread objects in the order they were published.
- """
- return [x['thread'] for x in self.forum if tag in x['thread'].tags]
- def search_by_author(self, author):
- """
- Searches all forum threads for posts by the given author.
- Returns a list of matching post objects in any order you like.
- """
- posts = []
- for thread in self.forum:
- for post in thread['thread'].posts:
- if post.author == author:
- posts.append(post)
- return posts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement