Skip to content

Result views

result.views

consent(request)

Register consent: in contrast to create, available without sending a session_id

Source code in result/views.py
42
43
44
45
46
47
48
49
50
51
52
53
@require_POST
def consent(request):
    ''' Register consent: in contrast to `create`, available without sending a session_id '''
    participant = get_participant(request)
    data = json.loads(request.POST.get('json_data'))
    result = Result.objects.create(
        participant=participant,
        question_key=data.get('key'),
        given_response='agreed'
    )
    result.save()
    return JsonResponse({'status': 'ok'})

current_profile(request)

Get current participant profile

Source code in result/views.py
56
57
58
59
60
def current_profile(request):
    """Get current participant profile"""
    participant = get_participant(request)

    return JsonResponse(participant.profile_object(), json_dumps_params={'indent': 4})

get_result(request, question)

Get specific answer from question from participant profile

Source code in result/views.py
63
64
65
66
67
68
69
70
71
72
73
def get_result(request, question):
    """Get specific answer from question from participant profile"""
    participant = get_participant(request)
    try:
        result = Result.objects.get(
            question_key=question, participant=participant)
    except Result.DoesNotExist:
        return HttpResponse(status=204)

    return JsonResponse({"answer": result.given_response},
                        json_dumps_params={'indent': 4})

score(request)

Create a new result for the given session, and return followup action

Source code in result/views.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@require_POST
def score(request):
    """Create a new result for the given session, and return followup action"""
    session = verify_session(request)

    # Create result based on POST data
    json_data = request.POST.get("json_data")
    if not json_data:
        return HttpResponseBadRequest("json_data not defined")

    try:
        result_data = json.loads(json_data)
        # Create a result from the data
        result = handle_results(result_data, session)
        if not result:
            return HttpResponseServerError("Could not create result from data")
    except ValueError:
        return HttpResponseServerError("Invalid data")

    return JsonResponse({'success': True})