views.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. """
  3. flaskbb.plugins.portal.views
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. This module contains the portal view.
  6. :copyright: (c) 2014 by the FlaskBB Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from flask import Blueprint, current_app, flash, request
  10. from flask_babelplus import gettext as _
  11. from flask_login import current_user
  12. from flaskbb.utils.helpers import render_template
  13. from flaskbb.forum.models import Topic, Post, Forum
  14. from flaskbb.user.models import User, Group
  15. from flaskbb.utils.helpers import time_diff, get_online_users
  16. from flaskbb.utils.settings import flaskbb_config
  17. portal = Blueprint("portal", __name__, template_folder="templates")
  18. def inject_portal_link():
  19. return render_template("navigation_snippet.html")
  20. @portal.route("/")
  21. def index():
  22. page = request.args.get('page', 1, type=int)
  23. try:
  24. forum_ids = flaskbb_config["PLUGIN_PORTAL_FORUM_IDS"]
  25. except KeyError:
  26. forum_ids = []
  27. flash(_("Please install the plugin first to configure the forums "
  28. "which should be displayed."), "warning")
  29. group_ids = [group.id for group in current_user.groups]
  30. forums = Forum.query.filter(Forum.groups.any(Group.id.in_(group_ids)))
  31. # get the news forums - check for permissions
  32. news_ids = [f.id for f in forums.filter(Forum.id.in_(forum_ids)).all()]
  33. news = Topic.query.filter(Topic.forum_id.in_(news_ids)).\
  34. order_by(Topic.id.desc()).\
  35. paginate(page, flaskbb_config["TOPICS_PER_PAGE"], True)
  36. # get the recent topics from all to the user available forums (not just the
  37. # configured ones)
  38. all_ids = [f.id for f in forums.all()]
  39. recent_topics = Topic.query.filter(Topic.forum_id.in_(all_ids)).\
  40. order_by(Topic.last_updated.desc()).\
  41. limit(flaskbb_config.get("PLUGIN_PORTAL_RECENT_TOPICS", 10))
  42. user_count = User.query.count()
  43. topic_count = Topic.query.count()
  44. post_count = Post.query.count()
  45. newest_user = User.query.order_by(User.id.desc()).first()
  46. # Check if we use redis or not
  47. if not current_app.config["REDIS_ENABLED"]:
  48. online_users = User.query.filter(User.lastseen >= time_diff()).count()
  49. online_guests = None
  50. else:
  51. online_users = len(get_online_users())
  52. online_guests = len(get_online_users(guest=True))
  53. return render_template("index.html", news=news,
  54. recent_topics=recent_topics,
  55. user_count=user_count, topic_count=topic_count,
  56. post_count=post_count, newest_user=newest_user,
  57. online_guests=online_guests,
  58. online_users=online_users)