Revision 06014b1c snf-django-lib/snf_django/lib/api/urls.py

b/snf-django-lib/snf_django/lib/api/urls.py
35 35
from django.views.decorators import csrf
36 36
from django.conf.urls.defaults import patterns
37 37

  
38

  
38 39
def _patch_pattern(regex_pattern):
39 40
    """
40
    Patch pattern callback using csrf_exempt. Enforce 
41
    Patch pattern callback using csrf_exempt. Enforce
41 42
    RegexURLPattern callback to get resolved if required.
43

  
42 44
    """
43
    if not regex_pattern._callback:
44
        # enforce _callback resolving
45
        regex_pattern._get_callback()
45
    if hasattr(regex_pattern, "_get_callback"):  # Django==1.2
46
        if not regex_pattern._callback:
47
            # enforce _callback resolving
48
            regex_pattern._get_callback()
46 49

  
47
    regex_pattern._callback = \
50
        regex_pattern._callback = \
48 51
            csrf.csrf_exempt(regex_pattern._callback)
52
    else:
53
        regex_pattern._callback = \
54
            csrf.csrf_exempt(regex_pattern.callback)
55

  
49 56

  
50 57
def _patch_resolver(r):
51 58
    """
52 59
    Patch all patterns found in resolver with _patch_pattern
53 60
    """
54
    if hasattr(r, '_get_url_patterns'):
61
    if hasattr(r, '_get_url_patterns'):  # Django ==1.2
55 62
        entries = r._get_url_patterns()
63
    elif hasattr(r, 'url_patterns'):
64
        entries = r.url_patterns
56 65
    else:
57 66
        # first level view in patterns ?
58 67
        entries = [r]
......
65 74
        else:
66 75
            _patch_pattern(entry)
67 76

  
77

  
68 78
def api_patterns(*args, **kwargs):
69 79
    """
70
    Protect all url patterns from csrf attacks. 
80
    Protect all url patterns from csrf attacks.
71 81
    """
72 82
    _patterns = patterns(*args, **kwargs)
73 83
    for entry in _patterns:

Also available in: Unified diff