lists.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. from misago.acl import add_acl
  2. from misago.readtracker import make_forums_read_aware
  3. from misago.forums.models import Forum
  4. __all__ = ['get_forums_list', 'get_forum_path']
  5. def get_forums_list(user, parent=None):
  6. if not user.acl['visible_forums']:
  7. return []
  8. if parent:
  9. queryset = parent.get_descendants().order_by('lft')
  10. else:
  11. queryset = Forum.objects.all_forums()
  12. queryset_with_acl = queryset.filter(id__in=user.acl['visible_forums'])
  13. visible_forums = [f for f in queryset_with_acl]
  14. forums_dict = {}
  15. forums_list = []
  16. parent_level = parent.level + 1 if parent else 1
  17. for forum in visible_forums:
  18. forum.subforums = []
  19. forums_dict[forum.pk] = forum
  20. forums_list.append(forum)
  21. if forum.level > parent_level:
  22. forums_dict[forum.parent_id].subforums.append(forum)
  23. add_acl(user, forums_list)
  24. make_forums_read_aware(user, forums_list)
  25. for forum in reversed(visible_forums):
  26. if forum.acl['can_browse']:
  27. forum_parent = forums_dict.get(forum.parent_id)
  28. if forum_parent:
  29. forum_parent.threads += forum.threads
  30. forum_parent.posts += forum.posts
  31. if forum_parent.last_post_on and forum.last_post_on:
  32. parent_last_post = forum_parent.last_post_on
  33. forum_last_post = forum.last_post_on
  34. update_last_thead = parent_last_post < forum_last_post
  35. elif not forum_parent.last_post_on and forum.last_post_on:
  36. update_last_thead = True
  37. else:
  38. update_last_thead = False
  39. if update_last_thead:
  40. forum_parent.last_post_on = forum.last_post_on
  41. forum_parent.last_thread_id = forum.last_thread_id
  42. forum_parent.last_thread_title = forum.last_thread_title
  43. forum_parent.last_thread_slug = forum.last_thread_slug
  44. forum_parent.last_poster_name = forum.last_poster_name
  45. forum_parent.last_poster_slug = forum.last_poster_slug
  46. if not forum.is_read:
  47. forum_parent.is_read = False
  48. flat_list = []
  49. for forum in forums_list:
  50. if forum.role != "category" or forum.subforums:
  51. flat_list.append(forum)
  52. return flat_list
  53. def get_forum_path(forum):
  54. forums_dict = Forum.objects.get_cached_forums_dict()
  55. forum_path = []
  56. while forum.level > 0:
  57. forum_path.append(forum)
  58. forum = forums_dict[forum.parent_id]
  59. return [f for f in reversed(forum_path)]