def add_existing_exercises(request): pset = ProblemSet.objects.get(id=request.POST["pset_id"]) exercise_ids = request.POST.getlist("exercise") exercises = Exercise.objects.filter(id__in=exercise_ids) for exercise in exercises: # if this exercise has been deleted previously then just un-delete it psetToExs = ProblemSetToExercise.objects.filter( problemSet=pset, exercise_id=exercise.id, mode="draft", is_deleted=1 ).order_by("-id") if psetToExs.exists(): psetToEx = psetToExs[0] psetToEx.is_deleted = 0 psetToEx.number = ProblemSetToExercise.objects.getByProblemset(pset).count() psetToEx.save() # else create a new one else: psetToEx = ProblemSetToExercise( problemSet=pset, exercise=exercise, number=ProblemSetToExercise.objects.getByProblemset(pset).count(), 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), ) )
def add_existing_exercises(request): pset = ProblemSet.objects.get(id=request.POST['pset_id']) exercise_ids = request.POST.getlist('exercise') exercises = Exercise.objects.filter(id__in=exercise_ids) for exercise in exercises: #if this exercise has been deleted previously then just un-delete it psetToExs = ProblemSetToExercise.objects.filter( problemSet=pset, exercise_id=exercise.id, mode='draft', is_deleted=1).order_by('-id') if psetToExs.exists(): psetToEx = psetToExs[0] psetToEx.is_deleted = 0 psetToEx.number = ProblemSetToExercise.objects.getByProblemset( pset).count() psetToEx.save() #else create a new one else: psetToEx = ProblemSetToExercise( problemSet=pset, exercise=exercise, number=ProblemSetToExercise.objects.getByProblemset( pset).count(), 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, )))
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))
def add_existing_exercises(request): pset = ProblemSet.objects.get(id=request.POST['pset_id']) exercise_ids = request.POST.getlist('exercise') exercises = Exercise.objects.filter(id__in=exercise_ids) for exercise in exercises: psetToEx = ProblemSetToExercise(problemSet=pset, exercise=exercise, number=len(ProblemSetToExercise.objects.getByProblemset(pset)), 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,)))
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))
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))
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))