Ejemplo n.º 1
0
def perform_pod_action(request, index):
    """
    Delegates to the `perform_action` function of the correct pod. If the action completes successfully, a new case
    action note is created with the success message.
    """
    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_decode(request.body)
    except ValueError as e:
        return JsonResponse({'reason': 'JSON decode error', 'details': six.text_type(e)}, 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)

    case = get_case(case_id)
    if case is None:
        return case_not_found_response(case_id)

    if not has_case_access(request.user, case, AccessLevel.update):
        return authorization_failure_response()
Ejemplo n.º 2
0
    def test_invalid_json(self):
        """
        If the request has an invalid json body, a correct error response
        should be returned.
        """
        response = self.url_post("unicef", reverse("perform_pod_action", args=("0",)), body="{")
        self.assertEqual(response.status_code, 400)

        content = json_decode(response.content)
        self.assertEqual(content["reason"], "JSON decode error")
        self.assertTrue(content["details"])
Ejemplo n.º 3
0
    def test_invalid_json(self):
        """
        If the request has an invalid json body, a correct error response
        should be returned.
        """
        response = self.url_post(
            'unicef', reverse('perform_pod_action', args=('0',)), body="{")
        self.assertEqual(response.status_code, 400)

        content = json_decode(response.content)
        self.assertEqual(content['reason'], "JSON decode error")
        self.assertTrue(content['details'])
Ejemplo n.º 4
0
def perform_pod_action(request, index):
    """
    Delegates to the `perform_action` function of the correct pod. If the action completes successfully, a new case
    action note is created with the success message.
    """
    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_decode(request.body)
    except ValueError as e:
        return JsonResponse({'reason': 'JSON decode error', 'details': six.text_type(e)}, 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)

    case = get_case(case_id)
    if case is None:
        return case_not_found_response(case_id)

    if not has_case_access(request.user, case, AccessLevel.update):
        return authorization_failure_response()

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

    return JsonResponse({'success': success, 'payload': payload})
Ejemplo n.º 5
0
 def process_request(self, request):
     if 'application/json' in request.META.get('CONTENT_TYPE', ""):
         request.json = json_decode(request.body)
Ejemplo n.º 6
0
    def __call__(self, request):
        if "application/json" in request.META.get("CONTENT_TYPE", ""):
            request.json = json_decode(request.body)

        return self.get_response(request)