root.py 6.1KB

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