Skip to content

Commit

Permalink
fix: backend uses playlist for session.create again
Browse files Browse the repository at this point in the history
  • Loading branch information
BeritJanssen committed Feb 6, 2024
1 parent da6f06b commit 223d5f0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 24 deletions.
47 changes: 47 additions & 0 deletions backend/session/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.test import TestCase

from experiment.models import Experiment
from participant.models import Participant
from section.models import Playlist
from session.models import Session


class SessionViewsTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.participant = Participant.objects.create(unique_hash=42)
cls.playlist1 = Playlist.objects.create(name='First Playlist')
cls.playlist2 = Playlist.objects.create(name='Second Playlist')
cls.experiment = Experiment.objects.create(
name='TestViews',
slug='testviews'
)
cls.experiment.playlists.add(
cls.playlist1, cls.playlist2
)

def setUp(self):
session = self.client.session
session['participant_id'] = self.participant.id
session.save()

def test_create_with_playlist(self):
request = {
"experiment_id": self.experiment.id,
"playlist_id": self.playlist2.id
}
self.client.post('/session/create/', request)
new_session = Session.objects.get(
experiment=self.experiment, participant=self.participant)
assert new_session
assert new_session.playlist == self.playlist2

def test_create_without_playlist(self):
request = {
"experiment_id": self.experiment.id
}
self.client.post('/session/create/', request)
new_session = Session.objects.get(
experiment=self.experiment, participant=self.participant)
assert new_session
assert new_session.playlist == self.playlist1
7 changes: 3 additions & 4 deletions backend/session/urls.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from django.urls import path
from .views import create_session, continue_session, next_round, finalize_session, register_playlist
from .views import create_session, continue_session, next_round, finalize_session


app_name='session'

urlpatterns = [
path('create/',
create_session, name='session_create'),
path('<int:session_id>/next_round/',
next_round, name='session_next_round'),
path('<int:session_id>/register_playlist/',
register_playlist, name='register_playlist'),
next_round, name='session_next_round'),
path('continue/<int:session_id>',
continue_session, name='continue_session'),
path('<int:session_id>/finalize/', finalize_session)
Expand Down
28 changes: 8 additions & 20 deletions backend/session/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ def create_session(request):
# Create new session
session = Session(experiment=experiment, participant=participant)

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

Expand All @@ -41,25 +48,6 @@ def create_session(request):
return JsonResponse({'session': {'id': session.id}})


@require_POST
def register_playlist(request, session_id):
# load playlist from request
playlist_id = request.POST.get("playlist_id")
if not playlist_id:
return HttpResponseBadRequest("playlist_id not defined")
participant = get_participant(request)
session = get_object_or_404(Session,
pk=session_id, participant__id=participant.id)
try:
playlist = Playlist.objects.get(
pk=playlist_id, experiment__id=session.experiment.id)
session.playlist = playlist
session.save()
return JsonResponse({'success': True})
except Playlist.DoesNotExist:
raise Http404("Playlist does not exist")


def continue_session(request, session_id):
""" given a session_id, continue where we left off """

Expand Down

0 comments on commit 223d5f0

Please sign in to comment.