JSONPfiedCBV   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 7
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 10
wmc 1
1
from __future__ import absolute_import
2
3
from functools import wraps
4
import types
5
6
from django.views.decorators.http import require_GET
7
from django.views.generic import View
8
from .response import get_jsonp_response
9
10
from .utils import get_callback
11
12
13
def jsonp(view):
14
    if isinstance(view, types.FunctionType):
15
        @require_GET
16
        @wraps(view)
17
        def jsonpfied_view(request, *args, **kwargs):
18
19
            return get_jsonp_response(view(request, *args, **kwargs), callback=get_callback(request))
20
        return jsonpfied_view
21
22
    elif issubclass(view, View):
23
        class JSONPfiedCBV(view):
24
            http_method_names = ['get']  # only GET method is allowed for JSONP
25
26
            def get(self, request, *args, **kwargs):
27
                return get_jsonp_response(
28
                    super(JSONPfiedCBV, self).get(request, *args, **kwargs),
29
                    callback=get_callback(request))
30
31
        return JSONPfiedCBV
32
    else:
33
        raise NotImplementedError('Only django CBVs and FBVs are supported')
34