Source

hbwebui / hbwebui / decorators.py

Full commit
from functools import wraps
from django.utils.decorators import available_attrs

from django.http import HttpResponse, HttpResponseForbidden, HttpResponseBadRequest
from django.template import loader, Context, RequestContext

def login_required_simple(func):
    @wraps(func, assigned=available_attrs(func))
    def inner(request, *args, **kwargs):
        if not request.user.is_authenticated(): return HttpResponseForbidden()
        return func(request, *args, **kwargs)
    return inner

def ident_required(func, simple=False):
    @wraps(func, assigned=available_attrs(func))
    def inner(request, *args, **kwargs):
        try:
            idents = request.session['idents']
        except KeyError:
            idents = None
        if not idents:
            if simple:
                return HttpResponseBadRequest('no idents for %s' % request.user)
            else:
                t = loader.get_template('error_noidents.html')
                c = RequestContext(request, {
                
                })
                return HttpResponse(t.render(c));
        else:
            return func(request, *args, **kwargs)
    return inner

def ident_required_simple(func):
    return ident_required(func, simple=True)