root.py 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. """Main Controller"""
  3. from tg import expose, flash, require, url, lurl, request, redirect, tmpl_context
  4. from tg.i18n import ugettext as _, lazy_ugettext as l_
  5. from tg import predicates
  6. from pboard import model
  7. from pboard.controllers.secure import SecureController
  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. __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. secc = SecureController()
  29. admin = AdminController(model, DBSession, config_type=TGAdminConfig)
  30. error = ErrorController()
  31. api = pbca.PODApiController()
  32. def _before(self, *args, **kw):
  33. tmpl_context.project_name = "pboard"
  34. @expose('pboard.templates.index')
  35. def index(self):
  36. """Handle the front-page."""
  37. return dict(page='index')
  38. @expose('pboard.templates.about')
  39. def about(self):
  40. """Handle the 'about' page."""
  41. return dict(page='about')
  42. @expose('pboard.templates.environ')
  43. def environ(self):
  44. """This method showcases TG's access to the wsgi environment."""
  45. return dict(page='environ', environment=request.environ)
  46. @expose('pboard.templates.data')
  47. @expose('json')
  48. def data(self, **kw):
  49. """This method showcases how you can use the same controller for a data page and a display page"""
  50. return dict(page='data', params=kw)
  51. @expose('pboard.templates.iconset')
  52. def iconset(self, **kw):
  53. """This method showcases how you can use the same controller for a data page and a display page"""
  54. return dict(page='data', params=kw)
  55. @expose('pboard.templates.index')
  56. @require(predicates.has_permission('manage', msg=l_('Only for managers')))
  57. def manage_permission_only(self, **kw):
  58. """Illustrate how a page for managers only works."""
  59. return dict(page='managers stuff')
  60. @expose('pboard.templates.index')
  61. @require(predicates.is_user('editor', msg=l_('Only for the editor')))
  62. def editor_user_only(self, **kw):
  63. """Illustrate how a page exclusive for the editor works."""
  64. return dict(page='editor stuff')
  65. @expose('pboard.templates.login')
  66. def login(self, came_from=lurl('/')):
  67. """Start the user login."""
  68. login_counter = request.environ.get('repoze.who.logins', 0)
  69. if login_counter > 0:
  70. flash(_('Wrong credentials'), 'warning')
  71. return dict(page='login', login_counter=str(login_counter),
  72. came_from=came_from)
  73. @expose()
  74. def post_login(self, came_from=lurl('/')):
  75. """
  76. Redirect the user to the initially requested page on successful
  77. authentication or redirect her back to the login page if login failed.
  78. """
  79. if not request.identity:
  80. login_counter = request.environ.get('repoze.who.logins', 0) + 1
  81. redirect('/login',
  82. params=dict(came_from=came_from, __logins=login_counter))
  83. userid = request.identity['repoze.who.userid']
  84. flash(_('Welcome back, %s!') % userid)
  85. redirect(came_from)
  86. @expose()
  87. def post_logout(self, came_from=lurl('/')):
  88. """
  89. Redirect the user to the initially requested page on logout and say
  90. goodbye as well.
  91. """
  92. flash(_('We hope to see you soon!'))
  93. redirect(came_from)
  94. @expose('pboard.templates.document')
  95. def document(self, node=0, came_from=lurl('/')):
  96. """show the user dashboard"""
  97. import pboard.model.data as pbmd
  98. # loRootNodeList = pbm.DBSession.query(pbmd.PBNode).filter(pbmd.PBNode.parent_id==None).order_by(pbmd.PBNode.node_order).all()
  99. print "===> AAA"
  100. loRootNodeList = pld.buildTreeListForMenu()
  101. print "===> BBB"
  102. liNodeId = max(int(node), 1) # show node #1 if no selected node
  103. loCurrentNode = pbm.DBSession.query(pbmd.PBNode).filter(pbmd.PBNode.node_id==liNodeId).one()
  104. print "===> CCC"
  105. loNodeStatusList = pbmd.PBNodeStatus.getList()
  106. print "===> DDD"
  107. return dict(root_node_list=loRootNodeList, current_node=loCurrentNode, node_status_list = loNodeStatusList)
  108. @expose()
  109. def fill_treepath(self):
  110. """show the user dashboard"""
  111. import pboard.model.data as pbmd
  112. loRootNodeList = pbm.DBSession.query(pbmd.PBNode).order_by(pbmd.PBNode.parent_id).all()
  113. for loNode in loRootNodeList:
  114. if loNode.parent_id==None:
  115. loNode.node_depth = 0
  116. loNode.parent_tree_path = '/'
  117. else:
  118. loNode.node_depth = loNode._oParent.node_depth+1
  119. loNode.parent_tree_path = '%s%i/'%(loNode._oParent.parent_tree_path,loNode.parent_id)
  120. pbm.DBSession.flush()
  121. return