def activity_view(request, activity_id): activity = get_object_or_404(Activity, id=activity_id) session, _ = SessionActivity.objects.get_or_create(user=request.user, activity=activity) if request.method == 'GET': action = request.GET.get("action") if action == "pl": pl_id = request.GET.get("pl_id") if not pl_id or not pl_id.isdigit(): return HttpResponseBadRequest( "Missing/invalid parameter 'pl_id'") session.current_pl = get_object_or_404(PL, id=pl_id) session.save() elif action == "pltp": session.current_pl = None session.save() elif session.current_pl and action == "reset": Answer.objects.create(user=request.user, pl=session.current_pl) elif session.current_pl and action == "reroll": exercise = session.exercise() exercise.built = False exercise.seed = None exercise.save() elif session.current_pl and action == "next": pls = activity.pltp.pl.all() for previous, next in zip(pls, list(pls[1:]) + [None]): if previous == session.current_pl: session.current_pl = next session.save() break else: session.current_pl = None session.save() if action: # Remove get arguments from URL return redirect(reverse("playexo:activity", args=[activity_id])) if session.current_pl: last = Answer.last(session.current_pl, request.user) Answer.objects.create(user=request.user, pl=session.current_pl, answers=last.answers if last else {}) return render(request, 'playexo/exercise.html', session.exercise().get_context(request))
def template(self, request, activity, session): if not activity.open: return self.end(activity, session) if request.method == 'GET': action = request.GET.get("action") if action == "pl": pl_id = request.GET.get("pl_id") if not pl_id or not pl_id.isdigit(): return HttpResponseBadRequest( "Missing/invalid parameter 'pl_id'") session.current_pl = get_object_or_404(PL, id=pl_id) session.save() elif action == "home": session.current_pl = None session.save() elif session.current_pl and action == "reset": Answer.objects.create(user=request.user, pl=session.current_pl) elif session.current_pl and action == "reroll": exercise = session.session_exercise() exercise.built = False exercise.seed = None exercise.save() if action: # Remove get arguments from URL return redirect(reverse("activity:play", args=[activity.id])) if session.current_pl: last = Answer.last(session.current_pl, request.user) Answer.objects.create(user=request.user, pl=session.current_pl, answers=last.answers if last else {}) return render(request, 'activity/activity_type/pltp/exercise.html', self.get_context(activity, session, request))
def test_last(self): self.assertIs(Answer.last(self.pl, self.user), None) Answer.objects.create(pl=self.pl, user=self.user, grade=20) Answer.objects.create(pl=self.pl, user=self.user, grade=10) self.assertEqual(Answer.last(self.pl, self.user).grade, 10)