def splat_handler(request, splat): slugs = filter(lambda x: x, splat.split("/")) current_node = topic_tools.get_topic_tree() while current_node: match = [ch for ch in (current_node.get('children') or []) if request.path.startswith(ch["path"])] if len(match) > 1: # can only happen for leaf nodes (only when one node is blank?) match = [m for m in match if request.path == m["path"]] if not match: raise Http404 current_node = match[0] if request.path == current_node["path"]: break if current_node["kind"] == "Topic": return topic_handler(request, cached_nodes={"topic": current_node}) elif current_node["kind"] == "Video": prev, next = topic_tools.get_neighbor_nodes(current_node, neighbor_kind=current_node["kind"]) return video_handler(request, cached_nodes={"video": current_node, "prev": prev, "next": next}) elif current_node["kind"] == "Exercise": cached_nodes = topic_tools.get_related_videos(current_node, limit_to_available=False) cached_nodes["exercise"] = current_node cached_nodes["prev"], cached_nodes["next"] = topic_tools.get_neighbor_nodes(current_node, neighbor_kind=current_node['kind']) return exercise_handler(request, cached_nodes=cached_nodes) else: raise Http404
def save_exercise_log(request): """ Receives an exercise_id and relevant data, saves it to the currently authorized user. """ # Form does all data validation, including of the exercise_id form = ExerciseLogForm(data=simplejson.loads(request.body)) if not form.is_valid(): raise Exception(form.errors) data = form.data # More robust extraction of previous object user = request.session["facility_user"] (exerciselog, was_created) = ExerciseLog.get_or_initialize( user=user, exercise_id=data["exercise_id"]) previously_complete = exerciselog.complete exerciselog.attempts = data[ "attempts"] # don't increment, because we fail to save some requests exerciselog.streak_progress = data["streak_progress"] exerciselog.points = data["points"] exerciselog.language = data.get("language") or request.language try: exerciselog.full_clean() exerciselog.save(update_userlog=True) except ValidationError as e: return JsonResponseMessageError( _("Could not save ExerciseLog") + u": %s" % e) if "points" in request.session: del request.session["points"] # will be recomputed when needed # Special message if you've just completed. # NOTE: it's important to check this AFTER calling save() above. if not previously_complete and exerciselog.complete: exercise = get_node_cache("Exercise").get(data["exercise_id"], [None])[0] junk, next_exercise = get_neighbor_nodes( exercise, neighbor_kind="Exercise") if exercise else None if not next_exercise: return JsonResponseMessageSuccess( _("You have mastered this exercise and this topic!")) else: return JsonResponseMessageSuccess( _("You have mastered this exercise! Please continue on to <a href='%(href)s'>%(title)s</a>" ) % { "href": next_exercise["path"], "title": _(next_exercise["title"]), }) # Return no message in release mode; "data saved" message in debug mode. return JsonResponse({})
def save_exercise_log(request): """ Receives an exercise_id and relevant data, saves it to the currently authorized user. """ # Form does all data validation, including of the exercise_id form = ExerciseLogForm(data=simplejson.loads(request.body)) if not form.is_valid(): raise Exception(form.errors) data = form.data # More robust extraction of previous object user = request.session["facility_user"] (exerciselog, was_created) = ExerciseLog.get_or_initialize(user=user, exercise_id=data["exercise_id"]) previously_complete = exerciselog.complete exerciselog.attempts = data["attempts"] # don't increment, because we fail to save some requests exerciselog.streak_progress = data["streak_progress"] exerciselog.points = data["points"] exerciselog.language = data.get("language") or request.language try: exerciselog.full_clean() exerciselog.save(update_userlog=True) except ValidationError as e: return JsonResponseMessageError(_("Could not save ExerciseLog") + u": %s" % e) if "points" in request.session: del request.session["points"] # will be recomputed when needed # Special message if you've just completed. # NOTE: it's important to check this AFTER calling save() above. if not previously_complete and exerciselog.complete: exercise = get_node_cache("Exercise").get(data["exercise_id"], [None])[0] junk, next_exercise = get_neighbor_nodes(exercise, neighbor_kind="Exercise") if exercise else None if not next_exercise: return JsonResponseMessageSuccess(_("You have mastered this exercise and this topic!")) else: return JsonResponseMessageSuccess(_("You have mastered this exercise! Please continue on to <a href='%(href)s'>%(title)s</a>") % { "href": next_exercise["path"], "title": _(next_exercise["title"]), }) # Return no message in release mode; "data saved" message in debug mode. return JsonResponse({})