# -*- coding: utf-8 -*-
import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to

from {{package}}.lib.base import BaseController, render

log = logging.getLogger(__name__)

from {{package}}.lib import auth
from {{package}}.model import *
from {{package}} import model

class LoginController(BaseController):

    "A stub example login controller, with login and logout methods"

    def index(self):
        # add your login form here
        return render('login.mak')

    def signin(self):
        username = request.params.get('username')
        password = request.params.get('password')
        try:
            user = model.User.authenticate(username, password)
            auth.login(user)
            if session.get('redirect'):
                redir_url = session.pop('redirect')
                session.save()
                redirect_to(redir_url)
            else:
                redirect_to(controller='demo', action='privindex')
        except model.NotAuthenticated:
            # if the user visiting this controller is not 
            # authenticated the user is routed to the index
            # (above) which presents a login form.
            redirect_to(action='index')
    

    def signout(self):
        auth.logout()
        redirect_to(action='index')
