OpenSlides/openslides/utils/views.py

50 lines
1.3 KiB
Python
Raw Normal View History

2017-08-23 20:51:06 +02:00
from typing import List # noqa
2013-09-25 12:53:44 +02:00
from django.views import generic as django_views
from django.views.decorators.csrf import ensure_csrf_cookie
from rest_framework.response import Response
from rest_framework.views import APIView as _APIView
2012-02-20 17:46:45 +01:00
2013-09-25 12:53:44 +02:00
View = django_views.View
2012-04-13 11:35:53 +02:00
class CSRFMixin:
"""
Adds the csrf cookie to the response.
"""
@classmethod
def as_view(cls, *args, **kwargs):
view = super().as_view(*args, **kwargs)
return ensure_csrf_cookie(view)
class APIView(_APIView):
"""
The Django Rest framework APIView with improvements for OpenSlides.
"""
2017-08-23 20:51:06 +02:00
http_method_names = [] # type: List[str]
"""
The allowed actions have to be explicitly defined.
Django allowes the following:
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
"""
def get_context_data(self, **context):
"""
Returns the context for the response.
"""
return context
def method_call(self, request, *args, **kwargs):
"""
Http method that returns the response object with the context data.
"""
return Response(self.get_context_data())
# Add the http-methods and delete the method "method_call"
get = post = put = patch = delete = head = options = trace = method_call
del method_call