import turbogears as tg
from turbogears import controllers, expose, flash
# from ${package} import model
#if $identity != "none"
from turbogears import identity, redirect
from cherrypy import request, response
# from ${package} import json
#end if
# import logging
# log = logging.getLogger("${package}.controllers")

class Root(controllers.RootController):
    ${b}expose(template="${package}.templates.welcome")${e}
#if $identity != "none"
    # ${b}identity.require(identity.in_group("admin"))${e}
#end if
    def index(self):
        import time
        # log.debug("Happy TurboGears Controller Responding For Duty")
        flash("Your application is now running")
        return dict(now=time.ctime())
#if $identity != "none"

    ${b}expose(template="${package}.templates.login")${e}
    def login(self, forward_url=None, *args, **kw):

        if forward_url:
            if isinstance(forward_url, list):
                forward_url = forward_url.pop(0)
            else:
                del request.params['forward_url']

        if not identity.current.anonymous and identity.was_login_attempted() \
                and not identity.get_identity_errors():
            redirect(tg.url(forward_url or '/', kw))

        if identity.was_login_attempted():
            msg = _("The credentials you supplied were not correct or "
                   "did not grant access to this resource.")
        elif identity.get_identity_errors():
            msg = _("You must provide your credentials before accessing "
                   "this resource.")
        else:
            msg = _("Please log in.")
            if not forward_url:
                forward_url = request.headers.get("Referer", "/")

        response.status = 401
        return dict(logging_in=True, message=msg,
            forward_url=forward_url, previous_url=request.path_info,
            original_parameters=request.params)

    ${b}expose()${e}
    def logout(self):
        identity.current.logout()
        redirect("/")
#end if
