import turbogears   as tg
import cherrypy

from turbogears                     import identity, controllers, flash
from subcontrollers.dashboard       import DashboardController
from subcontrollers.project         import ProjectController
from subcontrollers.people          import PeopleController
from subcontrollers.recentchanges   import RecentChangesController
from subcontrollers.trackfeed       import WhatWhatFeed


class Root(controllers.RootController):
    
    @tg.expose()
    def index(self, *args, **kw):
        raise tg.redirect('/dashboard')
    
    
    @tg.expose(template="whatwhat.templates.login")
    def login(self, *args, **kw):
        if cherrypy.request.path == "/login" and cherrypy.request.headers.get("Referer") == "/":
            raise tg.redirect('/dashboard')
        else:
            if hasattr(cherrypy.request, "identity_errors"):
                flash(str(cherrypy.request.identity_errors))
                previous_url = cherrypy.request.path
            else:
                previous_url = cherrypy.request.headers.get("Referer", "/")
            cherrypy.response.status=403
            
            return dict(previous_url=previous_url, 
                        logging_in=True,
                        original_parameters={})
    
    
    @tg.expose()
    def logout(self):
        identity.current.logout()
        
        if cherrypy.response.simpleCookie.has_key('active_section'):
            cherrypy.response.simpleCookie['active_section']['expires'] = 0 
        
        flash("You have been logged out.")
        
        raise tg.redirect('/login')
    
        
    dashboard       = DashboardController()
    project         = ProjectController()
    people          = PeopleController()
    recentchanges   = RecentChangesController()
    feed            = WhatWhatFeed()
