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

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

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

log = logging.getLogger(__name__)

import {{package}}.model as model

# from {{package}}.lib.auth import require
from {{package}}.lib.decorators import require

from {{package}}.lib.auth.permissions import SignedIn


class DemoController(BaseController):

    def __before__(self):
        pass

    def index(self):
        c.users = model.Session.query(model.User).all()
        c.groups = model.Session.query(model.Group).all()
        c.permissions = model.Session.query(model.Permission).all()
        c.title = 'Test'
        c.identity = request.environ.get('repoze.who.identity', False)
        return render('test.mak')

    # Use the (stolen from TG2) @require decorator thus:
    # @require(Any(has_permission('edit-posts'), is_user('admin'))) 
    def privindex(self):
        # Or use in-action Permission checks ...
        # This SignedIn check will do for now.
        if not SignedIn().check():
            abort(401, detail='You are not authenticated')
        c.identity = request.environ.get('repoze.who.identity')
        c.users = model.Session.query(model.User).all()
        c.groups = model.Session.query(model.Group).all()
        c.permissions = model.Session.query(model.Permission).all()
        c.title = 'Test'
        return render('test.mak')
