Advertisement
Guest User

forum.py

a guest
Aug 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. from post import Post
  2. from thread import Thread
  3. """
  4. Forum:
  5.  
  6. The Forum contains a list of threads.
  7. Users can search for threads by tag.
  8. Users can search for posts by author.
  9. """
  10.  
  11. class Forum:
  12. def __init__(self):
  13. """
  14. Perform initialisation of a new forum object, as needed.
  15. """
  16. self.theThread = None
  17. self.forum = [] #[{title: title of thread author: threads author ,tags: threads tags ,thread: thread object}]
  18.  
  19. def get_threads(self):
  20. """
  21. Returns a list of threads in the forum, in the order that they were published.
  22. """
  23. return [x['thread'] for x in self.forum]
  24.  
  25. def publish(self, title, content, author):
  26. """
  27. Creates a new thread with the given title and adds it to the forum.
  28. The content and author are provided to allow you to create the first post object.
  29. Forum threads are stored in the order that they are published.
  30. Returns the new thread.
  31. """
  32. #pass to thread title and post object.
  33. postObj = Post(content,author)
  34. self.theThread = Thread(title,postObj)
  35. self.forum.append({'title':title, 'author':author,'thread':self.theThread})
  36. return self.theThread
  37.  
  38. def search_by_tag(self, tag):
  39. """
  40. Searches all forum threads for any that contain the given tag.
  41. Returns a list of matching thread objects in the order they were published.
  42. """
  43. return [x['thread'] for x in self.forum if tag in x['thread'].tags]
  44.  
  45. def search_by_author(self, author):
  46. """
  47. Searches all forum threads for posts by the given author.
  48. Returns a list of matching post objects in any order you like.
  49. """
  50. posts = []
  51. for thread in self.forum:
  52. for post in thread['thread'].posts:
  53. if post.author == author:
  54. posts.append(post)
  55. return posts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement