root.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # -*- coding: utf-8 -*-
  2. """Main Controller"""
  3. import tg
  4. from tg import expose, flash, require, url, lurl, request, redirect, tmpl_context
  5. from tg.i18n import ugettext as _, lazy_ugettext as l_
  6. from tg import predicates
  7. from pboard import model
  8. from pboard.model import DBSession, metadata
  9. from tgext.admin.tgadminconfig import TGAdminConfig
  10. from tgext.admin.controller import AdminController
  11. from pboard.lib.base import BaseController
  12. from pboard.controllers.error import ErrorController
  13. import pboard.model as pbm
  14. import pboard.controllers as pbc
  15. from pboard.lib import dbapi as pld
  16. from pboard.controllers import api as pbca
  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 = AdminController(model, DBSession, config_type=TGAdminConfig)
  32. api = pbca.PODApiController()
  33. debug = pbcd.DebugController()
  34. error = ErrorController()
  35. public_api = pbca.PODPublicApiController()
  36. def _before(self, *args, **kw):
  37. tmpl_context.project_name = "pboard"
  38. @expose('pboard.templates.index')
  39. def index(self):
  40. """Handle the front-page."""
  41. return dict()
  42. @expose('pboard.templates.login')
  43. def login(self, came_from=lurl('/')):
  44. """Start the user login."""
  45. login_counter = request.environ.get('repoze.who.logins', 0)
  46. if login_counter > 0:
  47. flash(_('Wrong credentials'), 'warning')
  48. return dict(page='login', login_counter=str(login_counter),
  49. came_from=came_from)
  50. @expose()
  51. def post_login(self, came_from=lurl('/')):
  52. """
  53. Redirect the user to the initially requested page on successful
  54. authentication or redirect her back to the login page if login failed.
  55. """
  56. if not request.identity:
  57. login_counter = request.environ.get('repoze.who.logins', 0) + 1
  58. redirect('/login',
  59. params=dict(came_from=came_from, __logins=login_counter))
  60. userid = request.identity['repoze.who.userid']
  61. flash(_('Welcome back, %s!') % userid)
  62. redirect(came_from)
  63. @expose()
  64. def post_logout(self, came_from=lurl('/')):
  65. """
  66. Redirect the user to the initially requested page on logout and say
  67. goodbye as well.
  68. """
  69. flash(_('We hope to see you soon!'))
  70. redirect(came_from)
  71. @expose('pboard.templates.dashboard')
  72. @require(predicates.in_group('user', msg=l_('Please login to access this page')))
  73. def dashboard(self):
  74. loCurrentUser = pld.PODStaticController.getCurrentUser()
  75. loApiController = pld.PODUserFilteredApiController(loCurrentUser.user_id)
  76. loLastModifiedNodes = loApiController.getLastModifiedNodes(10)
  77. loWhatsHotNodes = loApiController.getNodesByStatus('hot', 5)
  78. loActionToDoNodes = loApiController.getNodesByStatus('actiontodo', 5)
  79. return dict(last_modified_nodes=loLastModifiedNodes, whats_hot_nodes=loWhatsHotNodes, action_to_do_nodes = loActionToDoNodes)
  80. @expose('pboard.templates.document')
  81. @require(predicates.in_group('user', msg=l_('Please login to access this page')))
  82. def document(self, node=0, came_from=lurl('/')):
  83. """show the user dashboard"""
  84. loCurrentUser = pld.PODStaticController.getCurrentUser()
  85. loApiController = pld.PODUserFilteredApiController(loCurrentUser.user_id)
  86. # loRootNodeList = pbm.DBSession.query(pbmd.PBNode).filter(pbmd.PBNode.parent_id==None).order_by(pbmd.PBNode.node_order).all()
  87. loRootNodeList = loApiController.buildTreeListForMenu()
  88. liNodeId = int(node)
  89. loCurrentNode = None
  90. loNodeStatusList = None
  91. try:
  92. loNodeStatusList = pbmd.PBNodeStatus.getList()
  93. loCurrentNode = loApiController.getNode(liNodeId)
  94. except Exception, e:
  95. flash(_('Document not found'), 'error')
  96. # FIXME - D.A - 2013-11-07 - Currently, the code build a new item if no item found for current user
  97. # the correct behavior should be to redirect to setup page
  98. if loCurrentNode is not None and "%s"%loCurrentNode.node_id!=node:
  99. redirect(tg.url('/document/%i'%loCurrentNode.node_id))
  100. if loCurrentNode is None:
  101. loCurrentNode = loApiController.getNode(0) # try to get an item
  102. if loCurrentNode is not None:
  103. flash(_('Document not found. Randomly showing item #%i')%(loCurrentNode.node_id), 'warning')
  104. redirect(tg.url('/document/%i'%loCurrentNode.node_id))
  105. else:
  106. flash(_('Your first document has been automatically created'), 'info')
  107. loCurrentNode = loApiController.createDummyNode()
  108. pm.DBSession.flush()
  109. redirect(tg.url('/document/%i'%loCurrentNode.node_id))
  110. return dict(root_node_list=loRootNodeList, current_node=loCurrentNode, node_status_list = loNodeStatusList)