File: views.py

package info (click to toggle)
python-django-crum 0.7.9-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: python: 449; makefile: 221
file content (57 lines) | stat: -rw-r--r-- 1,663 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Python
from __future__ import with_statement
from __future__ import unicode_literals

# Django
from django.http import HttpResponse
from django.views.generic.base import View
try:
    from django.utils.six import text_type
except ImportError:
    text_type = str

# Django-REST-Framework
from rest_framework.response import Response
from rest_framework.views import APIView

# Django-CRUM
from crum import get_current_user, impersonate


class IndexView(View):

    def get(self, request):
        if request.GET.get('raise', ''):
            raise RuntimeError()
        if request.GET.get('impersonate', ''):
            with impersonate(None):
                current_user = text_type(get_current_user())
        else:
            current_user = text_type(get_current_user())
        return HttpResponse(current_user, content_type='text/plain')


index = IndexView.as_view()


class ApiIndexView(APIView):

    def initialize_request(self, request, *args, **kwargs):
        """Store the REST Framework request on the Django request."""
        req = super(ApiIndexView, self).initialize_request(request, *args,
                                                           **kwargs)
        request.drf_request = req
        return req

    def get(self, request, format=None):
        if request.query_params.get('raise', ''):
            raise RuntimeError()
        if request.query_params.get('impersonate', ''):
            with impersonate(None):
                current_user = text_type(get_current_user())
        else:
            current_user = text_type(get_current_user())
        return Response(current_user)


api_index = ApiIndexView.as_view()