First, the decorator:
def require_institution():
"""
Decorator to check for a valid institution id in the session
variables
and return institution object in kwargs
"""
def decorator(func):
def inner(request, *args, **kwargs):
## If there is no inst_id set in the session, transfer to
institution select
## page for a chance to select one
try:
institution =
Institution.objects.get(pk=request.session.get('inst_id',None))
kwargs['institution'] = institution
except Institution.DoesNotExist:
path = urlquote(request.get_full_path())
tup = reverse('select_inst'), REDIRECT_FIELD_NAME,
path
return HttpResponseRedirect('%s?%s=%s' % tup)
return func(request, *args, **kwargs)
return wraps(func, assigned=available_attrs(func))(inner)
return decorator
And then the class:
class ListViewInstCheck(ListView):
model = Event
paginate_by = DEFAULT_PAGINATION
@method_decorator(require_institution())
def dispatch(self, request, *args, **kwargs):
return super(ListViewInstCheck, self).dispatch(request, *args,
**kwargs)
def get_queryset(self):
queryset = super(ListViewInstCheck,self).get_queryset()
return queryset.filter(institution=self.kwargs['institution'])
Hope this helps.
Dan
On Dec 10, 1:51 pm, Eli Criffield <elicriffi...@gmail.com> wrote: