<commit_before>from __future__ import unicode_literals

import json
from django.http import JsonResponse

from casepro.pods import registry


def read_pod_data(request, index):
    """Delegates to the `read_data` function of the correct pod."""
    if request.method != 'GET':
        return JsonResponse({'reason': 'Method not allowed'}, status=405)

    try:
        pod = registry.pods[int(index)]
    except IndexError:
        return JsonResponse({'reason': 'Pod does not exist'}, status=404)

    return JsonResponse(pod.read_data(request.GET))


def perform_pod_action(request, index):
    """Deletegates to the `perform_action` function of the correct pod."""
    if request.method != 'POST':
        return JsonResponse({'reason': 'Method not allowed'}, status=405)

    try:
        pod = registry.pods[int(index)]
    except IndexError:
        return JsonResponse({'reason': 'Pod does not exist'}, status=404)

    try:
        data = json.loads(request.body)
    except ValueError as e:
        return JsonResponse({'reason': 'JSON decode error', 'details': e.message}, status=400)

    return JsonResponse(pod.perform_action(data))
<commit_msg>Change case field to case_id in error message<commit_after>from __future__ import unicode_literals

import json
from django.http import JsonResponse

from casepro.cases.models import Case, CaseAction
from casepro.pods import registry


def read_pod_data(request, index):
    """Delegates to the `read_data` function of the correct pod."""
    if request.method != 'GET':
        return JsonResponse({'reason': 'Method not allowed'}, status=405)

    try:
        pod = registry.pods[int(index)]
    except IndexError:
        return JsonResponse({'reason': 'Pod does not exist'}, status=404)

    return JsonResponse(pod.read_data(request.GET))


def perform_pod_action(request, index):
    """Deletegates to the `perform_action` function of the correct pod."""
    if request.method != 'POST':
        return JsonResponse({'reason': 'Method not allowed'}, status=405)

    try:
        pod = registry.pods[int(index)]
    except IndexError:
        return JsonResponse({'reason': 'Pod does not exist'}, status=404)

    try:
        data = json.loads(request.body)
    except ValueError as e:
        return JsonResponse({'reason': 'JSON decode error', 'details': e.message}, status=400)

    case_id = data.get('case_id')
    if case_id is None:
        return JsonResponse(
            {'reason': 'Request object needs to have a "case_id" field'}, status=400)

    action_data = data.get('action', {})
    success, payload = pod.perform_action(action_data.get('type'), action_data.get('payload', {}))
    if success is True:
        case = Case.objects.get(id=case_id)
        CaseAction.create(case, request.user, CaseAction.ADD_NOTE, note=payload.get('message'))

    return JsonResponse(pod.perform_action(data))

{'fraction_non_alphanumeric': 0.1202641640597845, 'fraction_numerical': 0.011470281543274244, 'mean_word_length': 3.7491749174917492, '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': '6847777', 'n_tokens_mistral': 854, 'n_tokens_neox': 817, 'n_words': 244}