Пример #1
0
def manage_exercises(request, course_prefix, course_suffix, pset_slug):
    # Get all necessary information about the problemset

    common_page_data = request.common_page_data

    data = {"common_page_data": common_page_data}
    form = ManageExercisesForm(initial={"course": common_page_data["course"].id})
    pset = ProblemSet.objects.getByCourse(common_page_data["course"]).get(slug=pset_slug)
    psetToExs = ProblemSetToExercise.objects.getByProblemset(pset).select_related("exercise", "problemSet")
    used_exercises = []
    problemset_taken = False
    if ProblemActivity.objects.filter(problemset_to_exercise__problemSet=pset.image).exists():
        problemset_taken = True
    # Get the list of exercises currently in this problem set
    for psetToEx in psetToExs:
        used_exercises.append(psetToEx.exercise.id)
    # Get all the exercises in the course but not in this problem set to list in add from existing
    # Q objects allow queryset filters to be ORed together
    exercises = (
        Exercise.objects.all()
        .filter(Q(problemSet__course=common_page_data["course"]) | Q(video__course=common_page_data["course"]))
        .exclude(id__in=used_exercises)
        .distinct()
    )

    # Form processing action if form was submitted
    if request.method == "POST":
        form = ManageExercisesForm(request.POST, request.FILES)
        if form.is_valid():
            pset = ProblemSet.objects.get(id=request.POST["pset_id"])
            file_content = request.FILES["file"]
            file_name = file_content.name

            exercise = Exercise()
            exercise.handle = request.POST["course_prefix"] + "--" + request.POST["course_suffix"]
            exercise.fileName = file_name
            exercise.file.save(file_name, file_content)
            exercise.save()

            index = ProblemSetToExercise.objects.getByProblemset(pset).count()
            psetToEx = ProblemSetToExercise(
                problemSet=pset, exercise=exercise, number=index, is_deleted=0, mode="draft"
            )
            psetToEx.save()
            return HttpResponseRedirect(
                reverse(
                    "problemsets.views.manage_exercises",
                    args=(request.POST["course_prefix"], request.POST["course_suffix"], pset.slug),
                )
            )

    # If form was not submitted then the form should be displayed or if there were errors the page needs to be rendered again
    data["form"] = form
    data["course_prefix"] = course_prefix
    data["course_suffix"] = course_suffix
    data["pset"] = pset
    data["psetToExs"] = psetToExs
    data["problemset_taken"] = problemset_taken
    data["exercises"] = exercises
    return render_to_response("problemsets/manage_exercises.html", data, context_instance=RequestContext(request))
Пример #2
0
def add_exercise(request):
    #    try:
    #        common_page_data = get_common_page_data(request, course_prefix, course_suffix)
    #    except:
    #        raise Http404

    # don't catch video DoesNotExist here because we want some tangible error to happen if
    # the video id changes in form submission, like emailing us
    video = Video.objects.get(id=request.POST["video_id"])

    video_time = request.POST["videotime"]

    file_content = request.FILES["exercise"]
    file_name = file_content.name

    exercise = Exercise()
    exercise.handle = request.POST["course_prefix"] + "--" + request.POST["course_suffix"]
    exercise.fileName = file_name
    exercise.file.save(file_name, file_content)
    exercise.save()

    videoToEx = VideoToExercise(video=video, exercise=exercise, is_deleted=False, video_time=video_time)
    videoToEx.save()
    return HttpResponseRedirect(
        reverse(
            "courses.videos.views.manage_exercises",
            args=(request.POST["course_prefix"], request.POST["course_suffix"], video.slug),
        )
    )
Пример #3
0
def manage_exercises(request, course_prefix, course_suffix, video_slug):
    #Get all necessary information about the problemset
    try:
        common_page_data = get_common_page_data(request, course_prefix, course_suffix)
    except:
        raise Http404
    data = {'common_page_data': common_page_data}
    manage_form = ManageExercisesForm(initial={'course':common_page_data['course'].id})
    video = Video.objects.getByCourse(common_page_data['course']).get(slug=video_slug)
    videoToExs = VideoToExercise.objects.filter(video__course=common_page_data['course'], is_deleted=False, video__slug=video_slug).order_by('video_time')
    used_exercises = []
    exercise_attempted = False
    if len(ProblemActivity.objects.filter(video_to_exercise__video=video.image)) > 0:
        exercise_attempted = True
    #Get the list of exercises currently in this problem set
    for videoToEx in videoToExs:
        used_exercises.append(videoToEx.exercise.id)
    #Get all the exercises in the course but not in this problem set to list in add from existing
    #Q objects allow queryset objects to be ORed together
    exercises = Exercise.objects.all().filter(Q(problemSet__course=common_page_data['course'])|Q(video__course=common_page_data['course'])).exclude(id__in=used_exercises).distinct()
    additional_form = AdditionalExercisesForm(initial={'course':common_page_data['course'].id}, used_exercises=exercises)
    reorder_form = ReorderExercisesForm(current_exercises=videoToExs)

    #Form processing action if form was submitted
    if request.method == 'POST':
        manage_form = ManageExercisesForm(request.POST, request.FILES)
        additional_form = AdditionalExercisesForm(request.POST, used_exercises=exercises)

        if manage_form.is_valid():
            video = Video.objects.get(id=request.POST['video_id'])
            file_content = request.FILES['file']
            file_name = file_content.name

            exercise = Exercise()
            exercise.handle = request.POST['course_prefix'] + '--' + request.POST['course_suffix']
            exercise.fileName = file_name
            exercise.file.save(file_name, file_content)
            exercise.save()

            video_time = request.POST['video_time']
            videoToEx = VideoToExercise(video=video, exercise=exercise, video_time=video_time, is_deleted=0, mode='draft')
            videoToEx.save()
            return HttpResponseRedirect(reverse('courses.videos.views.manage_exercises', args=(request.POST['course_prefix'], request.POST['course_suffix'], video.slug,)))

    #If form was not submitted then the form should be displayed or if there were errors the page needs to be rendered again
    data['manage_form'] = manage_form
    data['additional_form'] = additional_form
    data['reorder_form'] = reorder_form
    data['course_prefix'] = course_prefix
    data['course_suffix'] = course_suffix
    data['video'] = video
    data['videoToExs'] = videoToExs
    data['exercise_attempted'] = exercise_attempted
    data['exercises'] = exercises
    data['exercise_attempted'] = exercise_attempted
    return render_to_response('videos/manage_exercises.html', data, context_instance=RequestContext(request))
Пример #4
0
def manage_exercises(request, course_prefix, course_suffix, pset_slug):
    #Get all necessary information about the problemset
    try:
        common_page_data = get_common_page_data(request, course_prefix, course_suffix)
    except:
        raise Http404
    data = {'common_page_data': common_page_data}
    form = ManageExercisesForm(initial={'course':common_page_data['course'].id})
    pset = ProblemSet.objects.getByCourse(common_page_data['course']).get(slug=pset_slug)
    psetToExs = ProblemSetToExercise.objects.getByProblemset(pset).select_related('exercise', 'problemSet')
    used_exercises = []
    problemset_taken = False
    if len(ProblemActivity.objects.filter(problemset_to_exercise__problemSet=pset.image)) > 0:
        problemset_taken = True
    #Get the list of exercises currently in this problem set
    for psetToEx in psetToExs:
        used_exercises.append(psetToEx.exercise.id)
    #Get all the exercises in the course but not in this problem set to list in add from existing
    #Q objects allow queryset filters to be ORed together
    exercises = Exercise.objects.all().filter(Q(problemSet__course=common_page_data['course'])|Q(video__course=common_page_data['course'])).exclude(id__in=used_exercises).distinct()

    #Form processing action if form was submitted
    if request.method == 'POST':
        form = ManageExercisesForm(request.POST, request.FILES)
        if form.is_valid():
            pset = ProblemSet.objects.get(id=request.POST['pset_id'])
            file_content = request.FILES['file']
            file_name = file_content.name

            exercise = Exercise()
            exercise.handle = request.POST['course_prefix'] + '--' + request.POST['course_suffix']
            exercise.fileName = file_name
            exercise.file.save(file_name, file_content)
            exercise.save()

            index = len(ProblemSetToExercise.objects.getByProblemset(pset))
            psetToEx = ProblemSetToExercise(problemSet=pset, exercise=exercise, number=index, is_deleted=0, mode='draft')
            psetToEx.save()
            return HttpResponseRedirect(reverse('problemsets.views.manage_exercises', args=(request.POST['course_prefix'], request.POST['course_suffix'], pset.slug,)))

    #If form was not submitted then the form should be displayed or if there were errors the page needs to be rendered again
    data['form'] = form
    data['course_prefix'] = course_prefix
    data['course_suffix'] = course_suffix
    data['pset'] = pset
    data['psetToExs'] = psetToExs
    data['problemset_taken'] = problemset_taken
    data['exercises'] = exercises
    return render_to_response('problemsets/manage_exercises.html', data, context_instance=RequestContext(request))
Пример #5
0
def add_exercise(request):
#    try:
#        common_page_data = get_common_page_data(request, course_prefix, course_suffix)
#    except:
#        raise Http404

    video = Video.objects.get(id=request.POST['video_id'])
    video_time = request.POST['videotime']

    file_content = request.FILES['exercise']
    file_name = file_content.name

    exercise = Exercise()
    exercise.handle = request.POST['course_prefix'] + '--' + request.POST['course_suffix']
    exercise.fileName = file_name
    exercise.file.save(file_name, file_content)
    exercise.save()

    videoToEx = VideoToExercise(video=video, exercise=exercise, is_deleted=False, video_time=video_time)
    videoToEx.save()
    return HttpResponseRedirect(reverse('courses.videos.views.manage_exercises', args=(request.POST['course_prefix'], request.POST['course_suffix'], video.slug,)))
Пример #6
0
def add_exercise(request):
    #    try:
    #        common_page_data = get_common_page_data(request, course_prefix, course_suffix)
    #    except:
    #        raise Http404

    #don't catch video DoesNotExist here because we want some tangible error to happen if
    #the video id changes in form submission, like emailing us
    video = Video.objects.get(id=request.POST['video_id'])

    video_time = request.POST['videotime']

    file_content = request.FILES['exercise']
    file_name = file_content.name

    exercise = Exercise()
    exercise.handle = request.POST['course_prefix'] + '--' + request.POST[
        'course_suffix']
    exercise.fileName = file_name
    exercise.file.save(file_name, file_content)
    exercise.save()

    videoToEx = VideoToExercise(video=video,
                                exercise=exercise,
                                is_deleted=False,
                                video_time=video_time)
    videoToEx.save()
    return HttpResponseRedirect(
        reverse('courses.videos.views.manage_exercises',
                args=(
                    request.POST['course_prefix'],
                    request.POST['course_suffix'],
                    video.slug,
                )))
Пример #7
0
def manage_exercises(request, course_prefix, course_suffix, pset_slug):
    #Get all necessary information about the problemset
    try:
        common_page_data = get_common_page_data(request, course_prefix, course_suffix)
    except:
        raise Http404
    data = {'common_page_data': common_page_data}
    form = ManageExercisesForm(initial={'course':common_page_data['course'].id})
    pset = ProblemSet.objects.getByCourse(common_page_data['course']).get(slug=pset_slug)
    psetToExs = ProblemSetToExercise.objects.getByProblemset(pset).select_related('exercise', 'problemSet')
    used_exercises = []
    problemset_taken = False
    if len(ProblemActivity.objects.filter(problemset_to_exercise__problemSet=pset.image)) > 0:
        problemset_taken = True
    #Get the list of exercises currently in this problem set
    for psetToEx in psetToExs:
        used_exercises.append(psetToEx.exercise.id)
    #Get all the exercises in the course but not in this problem set to list in add from existing
    #Q objects allow queryset filters to be ORed together
    exercises = Exercise.objects.all().filter(Q(problemSet__course=common_page_data['course'])|Q(video__course=common_page_data['course'])).exclude(id__in=used_exercises).distinct()

    #Form processing action if form was submitted
    if request.method == 'POST':
        form = ManageExercisesForm(request.POST, request.FILES)
        if form.is_valid():
            pset = ProblemSet.objects.get(id=request.POST['pset_id'])
            file_content = request.FILES['file']
            file_name = file_content.name

            exercise = Exercise()
            exercise.handle = request.POST['course_prefix'] + '--' + request.POST['course_suffix']
            exercise.fileName = file_name
            exercise.file.save(file_name, file_content)
            exercise.save()

            index = len(ProblemSetToExercise.objects.getByProblemset(pset))
            psetToEx = ProblemSetToExercise(problemSet=pset, exercise=exercise, number=index, is_deleted=0, mode='draft')
            psetToEx.save()
            return HttpResponseRedirect(reverse('problemsets.views.manage_exercises', args=(request.POST['course_prefix'], request.POST['course_suffix'], pset.slug,)))

    #If form was not submitted then the form should be displayed or if there were errors the page needs to be rendered again
    data['form'] = form
    data['course_prefix'] = course_prefix
    data['course_suffix'] = course_suffix
    data['pset'] = pset
    data['psetToExs'] = psetToExs
    data['problemset_taken'] = problemset_taken
    data['exercises'] = exercises
    return render_to_response('problemsets/manage_exercises.html', data, context_instance=RequestContext(request))
Пример #8
0
def manage_exercises(request, course_prefix, course_suffix, pset_slug):
    #Get all necessary information about the problemset

    common_page_data = request.common_page_data

    data = {'common_page_data': common_page_data}
    form = ManageExercisesForm(
        initial={'course': common_page_data['course'].id})
    pset = ProblemSet.objects.getByCourse(
        common_page_data['course']).get(slug=pset_slug)
    psetToExs = ProblemSetToExercise.objects.getByProblemset(
        pset).select_related('exercise', 'problemSet')
    used_exercises = []
    problemset_taken = False
    if ProblemActivity.objects.filter(
            problemset_to_exercise__problemSet=pset.image).exists():
        problemset_taken = True
    #Get the list of exercises currently in this problem set
    for psetToEx in psetToExs:
        used_exercises.append(psetToEx.exercise.id)
    #Get all the exercises in the course but not in this problem set to list in add from existing
    #Q objects allow queryset filters to be ORed together
    exercises = Exercise.objects.all().filter(
        Q(problemSet__course=common_page_data['course'])
        | Q(video__course=common_page_data['course'])).exclude(
            id__in=used_exercises).distinct()

    #Form processing action if form was submitted
    if request.method == 'POST':
        form = ManageExercisesForm(request.POST, request.FILES)
        if form.is_valid():
            pset = ProblemSet.objects.get(id=request.POST['pset_id'])
            file_content = request.FILES['file']
            file_name = file_content.name

            exercises = Exercise.objects.filter(handle=course_prefix + "--" +
                                                course_suffix,
                                                is_deleted=0)
            exercise_exists = False
            for exercise in exercises:
                if exercise.fileName == file_name:
                    #We don't wipe out all problem activites associated with this
                    #existing exercise, but if it's a nontrivial overwrite, should we?
                    exercise.file = file_content
                    exercise.save()
                    exercise_exists = True

                    #If exercise already in pset, don't need to create new psetToEx
                    #If exercise already in pset but deleted, undelete
                    #Otherwise create new psetToEx
                    queryPsetToEx = ProblemSetToExercise.objects.filter(
                        problemSet=pset, exercise=exercise,
                        mode='draft').order_by('-id')
                    if queryPsetToEx.exists():
                        existingPsetToEx = queryPsetToEx[0]
                        if existingPsetToEx.is_deleted == 1:
                            existingPsetToEx.is_deleted = 0
                            existingPsetToEx.number = ProblemSetToExercise.objects.getByProblemset(
                                pset).count()
                            existingPsetToEx.save()
                    else:
                        index = ProblemSetToExercise.objects.getByProblemset(
                            pset).count()
                        psetToEx = ProblemSetToExercise(problemSet=pset,
                                                        exercise=exercise,
                                                        number=index,
                                                        is_deleted=0,
                                                        mode='draft')
                        psetToEx.save()
                    break

            if not exercise_exists:
                exercise = Exercise()
                exercise.handle = request.POST[
                    'course_prefix'] + '--' + request.POST['course_suffix']
                exercise.fileName = file_name
                exercise.file.save(file_name, file_content)
                exercise.save()

                index = ProblemSetToExercise.objects.getByProblemset(
                    pset).count()
                psetToEx = ProblemSetToExercise(problemSet=pset,
                                                exercise=exercise,
                                                number=index,
                                                is_deleted=0,
                                                mode='draft')
                psetToEx.save()
            return HttpResponseRedirect(
                reverse('problemsets.views.manage_exercises',
                        args=(
                            request.POST['course_prefix'],
                            request.POST['course_suffix'],
                            pset.slug,
                        )))

    #If form was not submitted then the form should be displayed or if there were errors the page needs to be rendered again
    data['form'] = form
    data['course_prefix'] = course_prefix
    data['course_suffix'] = course_suffix
    data['pset'] = pset
    data['psetToExs'] = psetToExs
    data['problemset_taken'] = problemset_taken
    data['exercises'] = exercises
    return render_to_response('problemsets/manage_exercises.html',
                              data,
                              context_instance=RequestContext(request))
Пример #9
0
def manage_exercises(request, course_prefix, course_suffix, video_slug):
    #Get all necessary information about the problemset
    try:
        common_page_data = get_common_page_data(request, course_prefix, course_suffix)
    except:
        raise Http404
    data = {'common_page_data': common_page_data}
    manage_form = ManageExercisesForm(initial={'course':common_page_data['course'].id})
    try:
        video = Video.objects.getByCourse(common_page_data['course']).get(slug=video_slug)
    except Video.DoesNotExist:
        raise Http404

    videoToExs = VideoToExercise.objects.filter(video__course=common_page_data['course'], is_deleted=False, video__slug=video_slug).order_by('video_time')
    used_exercises = []
    exercise_attempted = False
    if ProblemActivity.objects.filter(video_to_exercise__video=video.image).exists():
        exercise_attempted = True
    #Get the list of exercises currently in this problem set
    for videoToEx in videoToExs:
        used_exercises.append(videoToEx.exercise.id)
    #Get all the exercises in the course but not in this problem set to list in add from existing
    #Q objects allow queryset objects to be ORed together
    exercises = Exercise.objects.all().filter(Q(problemSet__course=common_page_data['course'])|Q(video__course=common_page_data['course'])).exclude(id__in=used_exercises).distinct()
    additional_form = AdditionalExercisesForm(initial={'course':common_page_data['course'].id}, used_exercises=exercises)
    reorder_form = ReorderExercisesForm(current_exercises=videoToExs)

    #Form processing action if form was submitted
    if request.method == 'POST':
        manage_form = ManageExercisesForm(request.POST, request.FILES)
        additional_form = AdditionalExercisesForm(request.POST, used_exercises=exercises)

        if manage_form.is_valid():
            
            #don't catch video DoesNotExist here because we want some tangible error to happen if
            #the video id changes in form submission, like emailing us
            video = Video.objects.get(id=request.POST['video_id'])
            video_time = request.POST['video_time']
            file_content = request.FILES['file']
            file_name = file_content.name

            exercises = Exercise.objects.filter(handle=course_prefix+"--"+course_suffix,is_deleted=0)
            exercise_exists = False
            for exercise in exercises:
                if exercise.fileName == file_name:
                    #We don't wipe out all problem activites associated with this
                    #existing exercise, but if it's a nontrivial overwrite, should we?
                    exercise.file = file_content
                    exercise.save()
                    exercise_exists = True

                    #If exercise already in video, don't need to create new videoToEx, just update video_time
                    #If exercise already in video but deleted, undelete
                    #Otherwise create new videoToEx
                    queryVideoToEx = VideoToExercise.objects.filter(video=video, exercise=exercise, mode='draft').order_by('-id')
                    if queryVideoToEx.exists():
                        existingVideoToEx = queryVideoToEx[0]
                        if existingVideoToEx.is_deleted == 1:
                            existingVideoToEx.is_deleted = 0
                            existingVideoToEx.video_time = video_time
                            existingVideoToEx.save()
                        else:
                            existingVideoToEx.video_time = video_time
                            existingVideoToEx.save()
                    else:
                        videoToEx = VideoToExercise(video=video, exercise=exercise, video_time=video_time, is_deleted=0, mode='draft')
                        videoToEx.save()
                    break

            if not exercise_exists:
                exercise = Exercise()
                exercise.handle = request.POST['course_prefix'] + '--' + request.POST['course_suffix']
                exercise.fileName = file_name
                exercise.file.save(file_name, file_content)
                exercise.save()

                videoToEx = VideoToExercise(video=video, exercise=exercise, video_time=video_time, is_deleted=0, mode='draft')
                videoToEx.save()
            return HttpResponseRedirect(reverse('courses.videos.views.manage_exercises', args=(request.POST['course_prefix'], request.POST['course_suffix'], video.slug,)))

    #If form was not submitted then the form should be displayed or if there were errors the page needs to be rendered again
    data['manage_form'] = manage_form
    data['additional_form'] = additional_form
    data['reorder_form'] = reorder_form
    data['course_prefix'] = course_prefix
    data['course_suffix'] = course_suffix
    data['video'] = video
    data['videoToExs'] = videoToExs
    data['exercise_attempted'] = exercise_attempted
    data['exercises'] = exercises
    data['exercise_attempted'] = exercise_attempted
    return render_to_response('videos/manage_exercises.html', data, context_instance=RequestContext(request))
Пример #10
0
def manage_exercises(request, course_prefix, course_suffix, video_slug):
    # Get all necessary information about the problemset
    try:
        common_page_data = get_common_page_data(request, course_prefix, course_suffix)
    except:
        raise Http404
    data = {"common_page_data": common_page_data}
    manage_form = ManageExercisesForm(initial={"course": common_page_data["course"].id})
    try:
        video = Video.objects.getByCourse(common_page_data["course"]).get(slug=video_slug)
    except Video.DoesNotExist:
        raise Http404

    videoToExs = VideoToExercise.objects.filter(
        video__course=common_page_data["course"], is_deleted=False, video__slug=video_slug
    ).order_by("video_time")
    used_exercises = []
    exercise_attempted = False
    if len(ProblemActivity.objects.filter(video_to_exercise__video=video.image)) > 0:
        exercise_attempted = True
    # Get the list of exercises currently in this problem set
    for videoToEx in videoToExs:
        used_exercises.append(videoToEx.exercise.id)
    # Get all the exercises in the course but not in this problem set to list in add from existing
    # Q objects allow queryset objects to be ORed together
    exercises = (
        Exercise.objects.all()
        .filter(Q(problemSet__course=common_page_data["course"]) | Q(video__course=common_page_data["course"]))
        .exclude(id__in=used_exercises)
        .distinct()
    )
    additional_form = AdditionalExercisesForm(
        initial={"course": common_page_data["course"].id}, used_exercises=exercises
    )
    reorder_form = ReorderExercisesForm(current_exercises=videoToExs)

    # Form processing action if form was submitted
    if request.method == "POST":
        manage_form = ManageExercisesForm(request.POST, request.FILES)
        additional_form = AdditionalExercisesForm(request.POST, used_exercises=exercises)

        if manage_form.is_valid():

            # don't catch video DoesNotExist here because we want some tangible error to happen if
            # the video id changes in form submission, like emailing us
            video = Video.objects.get(id=request.POST["video_id"])
            file_content = request.FILES["file"]
            file_name = file_content.name

            exercise = Exercise()
            exercise.handle = request.POST["course_prefix"] + "--" + request.POST["course_suffix"]
            exercise.fileName = file_name
            exercise.file.save(file_name, file_content)
            exercise.save()

            video_time = request.POST["video_time"]
            videoToEx = VideoToExercise(
                video=video, exercise=exercise, video_time=video_time, is_deleted=0, mode="draft"
            )
            videoToEx.save()
            return HttpResponseRedirect(
                reverse(
                    "courses.videos.views.manage_exercises",
                    args=(request.POST["course_prefix"], request.POST["course_suffix"], video.slug),
                )
            )

    # If form was not submitted then the form should be displayed or if there were errors the page needs to be rendered again
    data["manage_form"] = manage_form
    data["additional_form"] = additional_form
    data["reorder_form"] = reorder_form
    data["course_prefix"] = course_prefix
    data["course_suffix"] = course_suffix
    data["video"] = video
    data["videoToExs"] = videoToExs
    data["exercise_attempted"] = exercise_attempted
    data["exercises"] = exercises
    data["exercise_attempted"] = exercise_attempted
    return render_to_response("videos/manage_exercises.html", data, context_instance=RequestContext(request))
Пример #11
0
def manage_exercises(request, course_prefix, course_suffix, pset_slug):
    #Get all necessary information about the problemset
    
    common_page_data = request.common_page_data
    
    data = {'common_page_data': common_page_data}
    form = ManageExercisesForm(initial={'course':common_page_data['course'].id})
    pset = ProblemSet.objects.getByCourse(common_page_data['course']).get(slug=pset_slug)
    psetToExs = ProblemSetToExercise.objects.getByProblemset(pset).select_related('exercise', 'problemSet')
    used_exercises = []
    problemset_taken = False
    if ProblemActivity.objects.filter(problemset_to_exercise__problemSet=pset.image).exists():
        problemset_taken = True
    #Get the list of exercises currently in this problem set
    for psetToEx in psetToExs:
        used_exercises.append(psetToEx.exercise.id)
    #Get all the exercises in the course but not in this problem set to list in add from existing
    #Q objects allow queryset filters to be ORed together
    exercises = Exercise.objects.all().filter(Q(problemSet__course=common_page_data['course'])|Q(video__course=common_page_data['course'])).exclude(id__in=used_exercises).distinct()

    #Form processing action if form was submitted
    if request.method == 'POST':
        form = ManageExercisesForm(request.POST, request.FILES)
        if form.is_valid():
            pset = ProblemSet.objects.get(id=request.POST['pset_id'])
            file_content = request.FILES['file']
            file_name = file_content.name

            exercises = Exercise.objects.filter(handle=course_prefix+"--"+course_suffix,is_deleted=0)
            exercise_exists = False
            for exercise in exercises:
                if exercise.fileName == file_name:
                    #We don't wipe out all problem activites associated with this
                    #existing exercise, but if it's a nontrivial overwrite, should we?
                    exercise.file = file_content
                    exercise.save()
                    exercise_exists = True

                    #If exercise already in pset, don't need to create new psetToEx
                    #If exercise already in pset but deleted, undelete
                    #Otherwise create new psetToEx
                    queryPsetToEx = ProblemSetToExercise.objects.filter(problemSet=pset, exercise=exercise, mode='draft').order_by('-id')
                    if queryPsetToEx.exists():
                        existingPsetToEx = queryPsetToEx[0]
                        if existingPsetToEx.is_deleted == 1:
                            existingPsetToEx.is_deleted = 0
                            existingPsetToEx.number = ProblemSetToExercise.objects.getByProblemset(pset).count()
                            existingPsetToEx.save()
                    else:
                        index = ProblemSetToExercise.objects.getByProblemset(pset).count()
                        psetToEx = ProblemSetToExercise(problemSet=pset, exercise=exercise, number=index, is_deleted=0, mode='draft')
                        psetToEx.save()                        
                    break

            if not exercise_exists:
                exercise = Exercise()
                exercise.handle = request.POST['course_prefix'] + '--' + request.POST['course_suffix']
                exercise.fileName = file_name
                exercise.file.save(file_name, file_content)
                exercise.save()

                index = ProblemSetToExercise.objects.getByProblemset(pset).count()
                psetToEx = ProblemSetToExercise(problemSet=pset, exercise=exercise, number=index, is_deleted=0, mode='draft')
                psetToEx.save()
            return HttpResponseRedirect(reverse('problemsets.views.manage_exercises', args=(request.POST['course_prefix'], request.POST['course_suffix'], pset.slug,)))

    #If form was not submitted then the form should be displayed or if there were errors the page needs to be rendered again
    data['form'] = form
    data['course_prefix'] = course_prefix
    data['course_suffix'] = course_suffix
    data['pset'] = pset
    data['psetToExs'] = psetToExs
    data['problemset_taken'] = problemset_taken
    data['exercises'] = exercises
    return render_to_response('problemsets/manage_exercises.html', data, context_instance=RequestContext(request))
Пример #12
0
def manage_exercises(request, course_prefix, course_suffix, video_slug):
    #Get all necessary information about the problemset
    try:
        common_page_data = get_common_page_data(request, course_prefix, course_suffix)
    except:
        raise Http404
    data = {'common_page_data': common_page_data}
    manage_form = ManageExercisesForm(initial={'course':common_page_data['course'].id})
    try:
        video = Video.objects.getByCourse(common_page_data['course']).get(slug=video_slug)
    except Video.DoesNotExist:
        raise Http404

    videoToExs = VideoToExercise.objects.filter(video__course=common_page_data['course'], is_deleted=False, video__slug=video_slug).order_by('video_time')
    used_exercises = []
    exercise_attempted = False
    if len(ProblemActivity.objects.filter(video_to_exercise__video=video.image)) > 0:
        exercise_attempted = True
    #Get the list of exercises currently in this problem set
    for videoToEx in videoToExs:
        used_exercises.append(videoToEx.exercise.id)
    #Get all the exercises in the course but not in this problem set to list in add from existing
    #Q objects allow queryset objects to be ORed together
    exercises = Exercise.objects.all().filter(Q(problemSet__course=common_page_data['course'])|Q(video__course=common_page_data['course'])).exclude(id__in=used_exercises).distinct()
    additional_form = AdditionalExercisesForm(initial={'course':common_page_data['course'].id}, used_exercises=exercises)
    reorder_form = ReorderExercisesForm(current_exercises=videoToExs)

    #Form processing action if form was submitted
    if request.method == 'POST':
        manage_form = ManageExercisesForm(request.POST, request.FILES)
        additional_form = AdditionalExercisesForm(request.POST, used_exercises=exercises)

        if manage_form.is_valid():
            
            #don't catch video DoesNotExist here because we want some tangible error to happen if
            #the video id changes in form submission, like emailing us
            video = Video.objects.get(id=request.POST['video_id'])
            file_content = request.FILES['file']
            file_name = file_content.name

            exercise = Exercise()
            exercise.handle = request.POST['course_prefix'] + '--' + request.POST['course_suffix']
            exercise.fileName = file_name
            exercise.file.save(file_name, file_content)
            exercise.save()

            video_time = request.POST['video_time']
            videoToEx = VideoToExercise(video=video, exercise=exercise, video_time=video_time, is_deleted=0, mode='draft')
            videoToEx.save()
            return HttpResponseRedirect(reverse('courses.videos.views.manage_exercises', args=(request.POST['course_prefix'], request.POST['course_suffix'], video.slug,)))

    #If form was not submitted then the form should be displayed or if there were errors the page needs to be rendered again
    data['manage_form'] = manage_form
    data['additional_form'] = additional_form
    data['reorder_form'] = reorder_form
    data['course_prefix'] = course_prefix
    data['course_suffix'] = course_suffix
    data['video'] = video
    data['videoToExs'] = videoToExs
    data['exercise_attempted'] = exercise_attempted
    data['exercises'] = exercises
    data['exercise_attempted'] = exercise_attempted
    return render_to_response('videos/manage_exercises.html', data, context_instance=RequestContext(request))