Skip to content

Session views

session.views

create_session(request)

Create new session for given experiment for current participant

Source code in session/views.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
@require_POST
def create_session(request):
    """Create new session for given experiment for current participant"""

    # Current participant
    participant = get_participant(request)

    # Get block
    block_id = request.POST.get("block_id")
    if not block_id:
        return HttpResponseBadRequest("block_id not defined")
    try:
        block = Block.objects.get(pk=block_id)
    except Block.DoesNotExist:
        raise Http404("Block does not exist")

    # Create new session
    session = Session(block=block, participant=participant)

    if request.POST.get("playlist_id"):
        try:
            playlist = Playlist.objects.get(pk=request.POST.get("playlist_id"), block__id=session.block.id)
            session.playlist = playlist
        except:
            raise Http404("Playlist does not exist")
    elif block.playlists.count() >= 1:
        # register first playlist
        session.playlist = block.playlists.first()

    # Save session
    session.save()

    return JsonResponse({"session": {"id": session.id}})

next_round(request, session_id)

Fall back to continue a block is case next_round data is missing This data is normally provided in: result()

Source code in session/views.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def next_round(request, session_id):
    """
    Fall back to continue a block is case next_round data is missing
    This data is normally provided in: result()
    """
    # Current participant
    participant = get_participant(request)

    session = get_object_or_404(Session, pk=session_id, participant__id=participant.id)

    # check if this block is part of an Experiment
    experiment_slug = request.session.get(EXPERIMENT_KEY)
    if experiment_slug:
        # check that current session does not have the experiment information saved yet
        if not session.json_data.get(EXPERIMENT_KEY):
            # set information of the Experiment to the session
            experiment = Experiment.objects.get(slug=experiment_slug)
            if experiment and session.block in experiment.associated_blocks():
                session.save_json_data({EXPERIMENT_KEY: experiment_slug})

    # Get next round for given session
    actions = serialize_actions(session.block_rules().next_round(session))

    if not isinstance(actions, list):
        if actions.get("redirect"):
            return redirect(actions.get("redirect"))
        actions = [actions]

    return JsonResponse({"next_round": actions}, json_dumps_params={"indent": 4})