lists.py 2.4 KB

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