"""
Helper functions

All names available in this module will be available under the Pylons h object.
"""
from webhelpers import *
from pylons.controllers.util import log, abort
from pylons.i18n import get_lang, set_lang
from {{package}}.lib.auth import permissions
from {{package}}.lib.auth import get_user

{{if template_engine == 'genshi'}}
from genshi.core import Markup
{{endif}}

{{if template_engine == 'genshi'}}
def wrap_helpers(localdict):
    """Wrap the helpers for use in Genshi templates"""
    def helper_wrapper(func):
        def wrapped_helper(*args, **kwargs):
            return Markup(func(*args, **kwargs))
        try:
            wrapped_helper.__name__ = func.__name__
        except TypeError:
            # Python < 2.4
            pass
        wrapped_helper.__doc__ = func.__doc__
        return wrapped_helper
    for name, func in localdict.iteritems():
        if (not callable(func) or
            not func.__module__.startswith('webhelpers.rails')):
            continue
        localdict[name] = helper_wrapper(func)

wrap_helpers(locals())
{{endif}}

def get_object_or_404(model, **kw):
    """
    Returns object, or raises a 404 Not Found is object is not in db 
    Example: user = get_object_or_404(model.user, id = 1)
    """
    # This will need some work in order to function with a couchdb back-end
    raise "Not yet implemented."


# Auth helpers

def signed_in():
    return permissions.SignedIn().check()

def in_group(group_name):
    return permissions.InGroup(group_name).check()

def has_permission(perm):
    return permissions.HasPermission(perm).check()

