def sort(ctx_name, defaultfield=None, defaultdirection=DEFAULT):
    """Sort queryset found in TemplateResponse context under ``ctx_name``."""
    def decorator(view_func):
        @wraps(view_func)
        def _wrapped_view(request, *args, **kwargs):
            response = view_func(request, *args, **kwargs)
            try:
                ctx = response.context_data
            except __HOLE__:
                return response
            ctx["sort"] = Sort(request, defaultfield, defaultdirection)
            try:
                sortqs = ctx[ctx_name].order_by(*ctx["sort"].order_by)
                str(sortqs.query)  # hack to force evaluation of sort arguments
            except FieldError:
                pass
            else:
                ctx[ctx_name] = sortqs
            return response

        return _wrapped_view

    return decorator
dataset/ETHPy150Open mozilla/moztrap/moztrap/view/lists/sort.py/sort