# -*- 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}}.model import *
from {{package}}.lib import auth
from {{package}}.lib.decorators import authorize
from {{package}}.lib.auth.permissions import SignedIn

class DemoController(BaseController):

    # Need to protect an entire controller?
    # Decorating __before__ will protect all actions

    # @authorize(SignedIn())
    def __before__(self):
        pass

    def index(self):
        c.users = User.by_displayname(c.db, descending=True, count=5)
        c.groups = Group.by_name(c.db, descending=True, count=5)
        c.permissions = Permission.by_name(c.db, descending=True, count=5)
        c.title = 'Public'
        authusr = req.environ.get('AUTH_USER', False)
        c.identity = c.db[authusr] if authusr else False
        return render('test.mak')

    # Need to protect just a single action?
    # Do it like this ....
    @authorize(SignedIn())
    def privindex(self):
        c.users = User.by_displayname(c.db, descending=True, count=5)
        c.groups = Group.by_name(c.db, descending=True, count=5)
        c.permissions = Permission.by_name(c.db, descending=True, count=5)
        c.title = 'Private'
        # Either works ..
        # c.user = c.db.get(auth.get_user())
        # or
        authusr = req.environ.get('AUTH_USER', False)
        c.user = c.db[authusr] if authusr else False
        return render('test.mak')
