root.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # -*- coding: utf-8 -*-
  2. """Main Controller"""
  3. import pboard
  4. import tg
  5. from tg import expose, flash, require, url, lurl, request, redirect, tmpl_context
  6. from tg.i18n import ugettext as _, lazy_ugettext as l_
  7. from tg import predicates
  8. from pboard.lib.auth import can_read
  9. import tgext.admin.tgadminconfig as tgat
  10. import tgext.admin.controller as tgac
  11. from pboard.lib.base import BaseController
  12. from pboard.controllers.error import ErrorController
  13. from pboard.lib import dbapi as pld
  14. from pboard.controllers import api as pca
  15. from pboard.controllers import apipublic as pcap
  16. from pboard.controllers import debug as pbcd
  17. from pboard import model as pm
  18. import pboard.model.data as pbmd
  19. __all__ = ['RootController']
  20. class RootController(BaseController):
  21. """
  22. The root controller for the pboard application.
  23. All the other controllers and WSGI applications should be mounted on this
  24. controller. For example::
  25. panel = ControlPanelController()
  26. another_app = AnotherWSGIApplication()
  27. Keep in mind that WSGI applications shouldn't be mounted directly: They
  28. must be wrapped around with :class:`tg.controllers.WSGIAppController`.
  29. """
  30. admin = tgac.AdminController(
  31. pm,
  32. pm.DBSession,
  33. config_type = tgat.TGAdminConfig
  34. )
  35. api = pca.PODApiController()
  36. debug = pbcd.DebugController()
  37. error = ErrorController()
  38. public_api = pcap.PODPublicApiController()
  39. def _before(self, *args, **kw):
  40. tmpl_context.project_name = "pboard"
  41. @expose('pboard.templates.index')
  42. def index(self):
  43. """Handle the front-page."""
  44. return dict()
  45. @expose('pboard.templates.about')
  46. def about(self):
  47. """Handle the about-page."""
  48. return dict()
  49. @expose('pboard.templates.login')
  50. def login(self, came_from=lurl('/')):
  51. """Start the user login."""
  52. login_counter = request.environ.get('repoze.who.logins', 0)
  53. if login_counter > 0:
  54. flash(_('Wrong credentials'), 'warning')
  55. return dict(page='login', login_counter=str(login_counter),
  56. came_from=came_from)
  57. @expose()
  58. def post_login(self, came_from=lurl('/')):
  59. """
  60. Redirect the user to the initially requested page on successful
  61. authentication or redirect her back to the login page if login failed.
  62. """
  63. if not request.identity:
  64. login_counter = request.environ.get('repoze.who.logins', 0) + 1
  65. redirect('/login',
  66. params=dict(came_from=came_from, __logins=login_counter))
  67. userid = request.identity['repoze.who.userid']
  68. flash(_('Welcome back, %s!') % userid)
  69. redirect(came_from)
  70. @expose()
  71. def post_logout(self, came_from=lurl('/')):
  72. """
  73. Redirect the user to the initially requested page on logout and say
  74. goodbye as well.
  75. """
  76. flash(_('We hope to see you soon!'))
  77. redirect(came_from)
  78. @expose('pboard.templates.dashboard')
  79. @require(predicates.in_group('user', msg=l_('Please login to access this page')))
  80. def dashboard(self):
  81. loCurrentUser = pld.PODStaticController.getCurrentUser()
  82. loApiController = pld.PODUserFilteredApiController(loCurrentUser.user_id)
  83. loLastModifiedNodes = loApiController.getLastModifiedNodes(10)
  84. loWhatsHotNodes = loApiController.getNodesByStatus('hot', 5)
  85. loActionToDoNodes = loApiController.getNodesByStatus('actiontodo', 5)
  86. return dict(last_modified_nodes=loLastModifiedNodes, whats_hot_nodes=loWhatsHotNodes, action_to_do_nodes = loActionToDoNodes)
  87. @expose('pboard.templates.document')
  88. #@require(predicates.in_group('user', msg=l_('Please login to access this page')))
  89. @require(can_read())
  90. def document(self, node_id=0, version=0, came_from=lurl('/'), highlight=''):
  91. """show the user dashboard"""
  92. loCurrentUser = pld.PODStaticController.getCurrentUser()
  93. loApiController = pld.PODUserFilteredApiController(loCurrentUser.user_id)
  94. llAccessibleNodes = loApiController.getListOfAllowedNodes()
  95. liNodeId = int(node_id)
  96. liVersionId = int(version)
  97. loCurrentNode = None
  98. loNodeStatusList = None
  99. try:
  100. loNodeStatusList = pbmd.PBNodeStatus.getChoosableList()
  101. if liVersionId:
  102. row = dict(pm.DBSession.execute("select * from pod_nodes_history where node_id=:node_id and version_id=:version_id", {"node_id":liNodeId, "version_id":liVersionId}).first().items())
  103. del(row['version_id'])
  104. loCurrentNode = pbmd.PBNode(**row)
  105. else:
  106. loCurrentNode = loApiController.getNode(liNodeId)
  107. except Exception as e:
  108. flash(_('Document not found'), 'error')
  109. return dict(
  110. current_user=loCurrentUser,
  111. current_node=loCurrentNode,
  112. allowed_nodes=llAccessibleNodes,
  113. node_status_list = loNodeStatusList,
  114. keywords = highlight,
  115. user_specific_group_rights = pld.PODStaticController.getUserDedicatedGroupRightsOnNode(node_id),
  116. real_group_rights = pld.PODStaticController.getRealGroupRightsOnNode(node_id)
  117. )
  118. @expose('pboard.templates.search')
  119. @require(predicates.in_group('user', msg=l_('Please login to access this page')))
  120. def search(self, keywords=''):
  121. loCurrentUser = pld.PODStaticController.getCurrentUser()
  122. loApiController = pld.PODUserFilteredApiController(loCurrentUser.user_id)
  123. loFoundNodes = loApiController.searchNodesByText(keywords.split())
  124. return dict(search_string=keywords, found_nodes=loFoundNodes)
  125. @expose('pboard.templates.create_account')
  126. def create_account(self):
  127. return dict()