root.py 5.9KB

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. import tgext.admin.tgadminconfig as tgat
  9. import tgext.admin.controller as tgac
  10. from pboard.lib.base import BaseController
  11. from pboard.controllers.error import ErrorController
  12. from pboard.lib import dbapi as pld
  13. from pboard.controllers import api as pbca
  14. from pboard.controllers import debug as pbcd
  15. from pboard import model as pm
  16. import pboard.model.data as pbmd
  17. __all__ = ['RootController']
  18. class RootController(BaseController):
  19. """
  20. The root controller for the pboard application.
  21. All the other controllers and WSGI applications should be mounted on this
  22. controller. For example::
  23. panel = ControlPanelController()
  24. another_app = AnotherWSGIApplication()
  25. Keep in mind that WSGI applications shouldn't be mounted directly: They
  26. must be wrapped around with :class:`tg.controllers.WSGIAppController`.
  27. """
  28. admin = tgac.AdminController(
  29. pm,
  30. pm.DBSession,
  31. config_type = tgat.TGAdminConfig
  32. )
  33. api = pbca.PODApiController()
  34. debug = pbcd.DebugController()
  35. error = ErrorController()
  36. public_api = pbca.PODPublicApiController()
  37. def _before(self, *args, **kw):
  38. tmpl_context.project_name = "pboard"
  39. @expose('pboard.templates.index')
  40. def index(self):
  41. """Handle the front-page."""
  42. return dict()
  43. @expose('pboard.templates.about')
  44. def about(self):
  45. """Handle the about-page."""
  46. return dict()
  47. @expose('pboard.templates.login')
  48. def login(self, came_from=lurl('/')):
  49. """Start the user login."""
  50. login_counter = request.environ.get('repoze.who.logins', 0)
  51. if login_counter > 0:
  52. flash(_('Wrong credentials'), 'warning')
  53. return dict(page='login', login_counter=str(login_counter),
  54. came_from=came_from)
  55. @expose()
  56. def post_login(self, came_from=lurl('/')):
  57. """
  58. Redirect the user to the initially requested page on successful
  59. authentication or redirect her back to the login page if login failed.
  60. """
  61. if not request.identity:
  62. login_counter = request.environ.get('repoze.who.logins', 0) + 1
  63. redirect('/login',
  64. params=dict(came_from=came_from, __logins=login_counter))
  65. userid = request.identity['repoze.who.userid']
  66. flash(_('Welcome back, %s!') % userid)
  67. redirect(came_from)
  68. @expose()
  69. def post_logout(self, came_from=lurl('/')):
  70. """
  71. Redirect the user to the initially requested page on logout and say
  72. goodbye as well.
  73. """
  74. flash(_('We hope to see you soon!'))
  75. redirect(came_from)
  76. @expose('pboard.templates.dashboard')
  77. @require(predicates.in_group('user', msg=l_('Please login to access this page')))
  78. def dashboard(self):
  79. loCurrentUser = pld.PODStaticController.getCurrentUser()
  80. loApiController = pld.PODUserFilteredApiController(loCurrentUser.user_id)
  81. loLastModifiedNodes = loApiController.getLastModifiedNodes(10)
  82. loWhatsHotNodes = loApiController.getNodesByStatus('hot', 5)
  83. loActionToDoNodes = loApiController.getNodesByStatus('actiontodo', 5)
  84. return dict(last_modified_nodes=loLastModifiedNodes, whats_hot_nodes=loWhatsHotNodes, action_to_do_nodes = loActionToDoNodes)
  85. @expose('pboard.templates.document')
  86. @require(predicates.in_group('user', msg=l_('Please login to access this page')))
  87. def document(self, node=0, came_from=lurl('/'), highlight=''):
  88. """show the user dashboard"""
  89. loCurrentUser = pld.PODStaticController.getCurrentUser()
  90. loApiController = pld.PODUserFilteredApiController(loCurrentUser.user_id)
  91. # loRootNodeList = pbm.DBSession.query(pbmd.PBNode).filter(pbmd.PBNode.parent_id==None).order_by(pbmd.PBNode.node_order).all()
  92. loRootNodeList = loApiController.buildTreeListForMenu(pbmd.PBNodeStatus.getVisibleIdsList())
  93. liNodeId = int(node)
  94. loCurrentNode = None
  95. loNodeStatusList = None
  96. try:
  97. loNodeStatusList = pbmd.PBNodeStatus.getChoosableList()
  98. loCurrentNode = loApiController.getNode(liNodeId)
  99. except Exception as e:
  100. flash(_('Document not found'), 'error')
  101. # FIXME - D.A - 2013-11-07 - Currently, the code build a new item if no item found for current user
  102. # the correct behavior should be to redirect to setup page
  103. if loCurrentNode is not None and "%s"%loCurrentNode.node_id!=node:
  104. redirect(tg.url('/document/%i'%loCurrentNode.node_id))
  105. if loCurrentNode is None:
  106. loCurrentNode = loApiController.getNode(0) # try to get an item
  107. if loCurrentNode is not None:
  108. flash(_('Document not found. Randomly showing item #%i')%(loCurrentNode.node_id), 'warning')
  109. redirect(tg.url('/document/%i'%loCurrentNode.node_id))
  110. else:
  111. flash(_('Your first document has been automatically created'), 'info')
  112. loCurrentNode = loApiController.createDummyNode()
  113. pm.DBSession.flush()
  114. redirect(tg.url('/document/%i'%loCurrentNode.node_id))
  115. return dict(
  116. root_node_list=loRootNodeList,
  117. current_node=loCurrentNode,
  118. node_status_list = loNodeStatusList,
  119. keywords = highlight
  120. )
  121. @expose('pboard.templates.search')
  122. @require(predicates.in_group('user', msg=l_('Please login to access this page')))
  123. def search(self, keywords=''):
  124. loCurrentUser = pld.PODStaticController.getCurrentUser()
  125. loApiController = pld.PODUserFilteredApiController(loCurrentUser.user_id)
  126. loFoundNodes = loApiController.searchNodesByText(keywords.split())
  127. return dict(search_string=keywords, found_nodes=loFoundNodes)