root.py 6.2KB

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