<commit_before># -*- coding: utf-8 -*-
import json
from django.views.decorators.csrf import csrf_exempt
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse, HttpResponseBadRequest


def allowed_action(func):
    """
    All methods which shall be callable through a given Ajax 'action' must be
    decorated with @allowed_action. This is required for safety reasons. It
    inhibits the caller to invoke all available methods of a class.
    """
    setattr(func, 'is_allowed_action', None)
    return func


class JSONResponseMixin(object):
    """
    A mixin that dispatches POST requests containing the keyword 'action' onto
    the method with that name. It renders the returned context as JSON response.
    """

    @csrf_exempt
    def dispatch(self, *args, **kwargs):
        return super(JSONResponseMixin, self).dispatch(*args, **kwargs)

    def get(self, request, *args, **kwargs):
        action = kwargs.get('action')
        action = action and getattr(self, action, None)
        if not callable(action):
            return super(JSONResponseMixin, self).get(request, *args, **kwargs)
        if not hasattr(action, 'is_allowed_action'):
            raise ValueError('method "%s" is not decorated with @allowed_action' % action)
        out_data = json.dumps(action(), cls=DjangoJSONEncoder)
        return HttpResponse(out_data, content_type='application/json;charset=UTF-8')

    def post(self, request, *args, **kwargs):
        if not request.is_ajax():
            return HttpResponseBadRequest('Expected an XMLHttpRequest')
        try:
            in_data = json.loads(request.raw_post_data)
            action = in_data.pop('action', kwargs.get('action'))
            action = action and getattr(self, action, None)
            if not (callable(action) and hasattr(action, 'is_allowed_action')):
                raise ValueError('action="%s" is undefined or not callable' % action)
            out_data = json.dumps(action(in_data), cls=DjangoJSONEncoder)
            return HttpResponse(out_data, content_type='application/json;charset=UTF-8')
        except ValueError as e:
            return HttpResponseBadRequest('POST data is not valid JSON: %s' % e.message)
<commit_msg>Requests of method POST without an action are passed through to the default handler<commit_after># -*- coding: utf-8 -*-
import json
from django.views.decorators.csrf import csrf_exempt
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse, HttpResponseBadRequest


def allowed_action(func):
    """
    All methods which shall be callable through a given Ajax 'action' must be
    decorated with @allowed_action. This is required for safety reasons. It
    inhibits the caller to invoke all available methods of a class.
    """
    setattr(func, 'is_allowed_action', None)
    return func


class JSONResponseMixin(object):
    """
    A mixin that dispatches POST requests containing the keyword 'action' onto
    the method with that name. It renders the returned context as JSON response.
    """

    @csrf_exempt
    def dispatch(self, *args, **kwargs):
        return super(JSONResponseMixin, self).dispatch(*args, **kwargs)

    def get(self, request, *args, **kwargs):
        action = kwargs.get('action')
        action = action and getattr(self, action, None)
        if not callable(action):
            return super(JSONResponseMixin, self).get(request, *args, **kwargs)
        if not hasattr(action, 'is_allowed_action'):
            raise ValueError('method "%s" is not decorated with @allowed_action' % action)
        out_data = json.dumps(action(), cls=DjangoJSONEncoder)
        return HttpResponse(out_data, content_type='application/json;charset=UTF-8')

    def post(self, request, *args, **kwargs):
        if not request.is_ajax():
            return super(JSONResponseMixin, self).post(request, *args, **kwargs)
        try:
            in_data = json.loads(request.raw_post_data)
            action = in_data.pop('action', kwargs.get('action'))
            action = action and getattr(self, action, None)
            if not callable(action):
                return super(JSONResponseMixin, self).post(request, *args, **kwargs)
            if not hasattr(action, 'is_allowed_action'):
                raise ValueError('method "%s" is not decorated with @allowed_action' % action)
            out_data = json.dumps(action(in_data), cls=DjangoJSONEncoder)
            return HttpResponse(out_data, content_type='application/json;charset=UTF-8')
        except ValueError as e:
            return HttpResponseBadRequest('POST data is not valid JSON: %s' % e.message)

{'fraction_non_alphanumeric': 0.10019226660969878, 'fraction_numerical': 0.001281777397991882, 'mean_word_length': 3.271897810218978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1971461', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1280, 'n_words': 403}