File: decorators.py

package info (click to toggle)
python-django-debreach 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 204 kB
  • sloc: python: 327; makefile: 140
file content (27 lines) | stat: -rw-r--r-- 895 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# -*- coding: utf-8 -*-
from functools import wraps

from django.utils.decorators import decorator_from_middleware

from debreach.middleware import RandomCommentMiddleware


append_random_comment = decorator_from_middleware(RandomCommentMiddleware)
append_random_comment.__name__ = str('append_random_comment')
append_random_comment.__doc__ = '''
Applies a random comment to the response of the decorated view in the same
way as the RandomCommentMiddleware. Using both, or using the decorator
multiple times is harmless and efficient.
'''


def random_comment_exempt(view_func):
    """
    Marks a view as being exempt from having its response modified by the
    RandomCommentMiddleware
    """
    def wrapped_view(*args, **kwargs):
        response = view_func(*args, **kwargs)
        response._random_comment_exempt = True
        return response
    return wraps(view_func)(wrapped_view)