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, previous_url=None, *args, **kw):

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

        forward_url = None
        previous_url = request.path

        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.")
            forward_url = request.headers.get("Referer", "/")

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

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