def viewproblemgroup(request, pk): userprofile = get_or_create_up(request.user) prob_group = get_object_or_404(ProblemGroup, pk=pk) if prob_group not in userprofile.problem_groups.all( ) and prob_group not in userprofile.owned_problem_groups.all( ) and prob_group not in userprofile.editable_problem_groups.all( ) and prob_group not in userprofile.readonly_problem_groups.all( ) and prob_group not in userprofile.archived_problem_groups.all( ) and prob_group not in userprofile.archived_owned_problem_groups.all( ) and prob_group not in userprofile.archived_editable_problem_groups.all( ) and prob_group not in userprofile.archived_readonly_problem_groups.all(): return HttpResponse('Unauthorized', status=401) context = {} context['nbar'] = 'groups' context['prob_group'] = prob_group context['request'] = request owned_groups = userprofile.problem_groups.exclude(pk=pk) editable_groups = userprofile.editable_problem_groups.exclude(pk=pk) probgroups = list(chain(owned_groups, editable_groups)) context['prob_groups'] = probgroups context['form'] = AddProblemsForm(userprofile=userprofile) if prob_group in userprofile.problem_groups.all( ) or prob_group in userprofile.owned_problem_groups.all( ) or prob_group in userprofile.editable_problem_groups.all( ) or prob_group in userprofile.archived_problem_groups.all( ) or prob_group in userprofile.archived_owned_problem_groups.all( ) or prob_group in userprofile.archived_editable_problem_groups.all(): context['can_delete'] = 1 if prob_group in userprofile.problem_groups.all( ) or prob_group in userprofile.archived_problem_groups.all(): context['can_edit'] = 1 template = loader.get_template('groups/probgroupview.html') return HttpResponse(template.render(context, request))
def searchform(request): form = request.GET advanced = False if 'advanced' in form: advanced = True userprofile = get_or_create_up(request.user) types = userprofile.user_type_new.allowed_types.all() tags = sorted(list(NewTag.objects.exclude(label='root')), key=lambda x: x.tag) template = loader.get_template('search/searchform.html') context = { 'nbar': 'search', 'types': types, 'tags': tags, 'advanced': advanced, 'userprofile': userprofile, 'presets': AdvancedSearchPreset.objects.all(), } if 'advanced' in form: owned_pgs = userprofile.problem_groups.all() co_owned_pgs = userprofile.owned_problem_groups.all() editor_pgs = userprofile.editable_problem_groups.all() readonly_pgs = userprofile.readonly_problem_groups.all() pgs = list(owned_pgs) + list(co_owned_pgs) + list(editor_pgs) + list( readonly_pgs) context['probgroups'] = pgs return HttpResponse(template.render(context, request))
def tagtableview(request): userprofile = get_or_create_up(request.user) tags = NewTag.objects.all().exclude(tag='root').order_by('tag') template = loader.get_template('groups/tagtableview.html') context = {} context['nbar'] = 'groups' context['tags'] = tags return HttpResponse(template.render(context, request))
def viewtaggroup(request, pk): userprofile = get_or_create_up(request.user) tag = get_object_or_404(NewTag, pk=pk) if request.method == 'POST': if request.POST.get("newtest"): form = request.POST if "chk" in form: checked = form.getlist("chk") if len(checked) > 0: # P = prob_group.problems.all() testname = form.get('testname', '') t = Test(name=testname) t.save() for i in checked: p = Problem.objects.get(label=i) t.problems.add(p) ### t.save() userprofile.tests.add(t) ut = UserTest(test=t, num_probs=t.problems.count(), num_correct=0, userprof=userprofile) ut.save() for i in t.problems.all(): r = NewResponse(response='', problem_label=i.label, problem=i, usertest=ut) r.save() t.save() # userprofile.usertests.add(ut) userprofile.save() return redirect('/test/' + str(ut.pk) + '/') P = tag.problems.filter( type_new__in=userprofile.user_type_new.allowed_types.all()) context = {} if request.method == 'GET': if "updatetypes" in request.GET: form = request.GET include_types_names = form.getlist('includetypes') include_types = Type.objects.filter(type__in=include_types_names) P = P.filter(type_new__in=include_types) context['include_types'] = include_types name = tag.tag context['rows'] = P context['name'] = name context['nbar'] = 'groups' context['pk'] = pk context['userprofile'] = userprofile template = loader.get_template('groups/taggroupview.html') return HttpResponse(template.render(context, request))
def handoutlistview(request): userprofile = get_or_create_up(request.user) if request.method == "POST": form = request.POST if "addhandout" in form: h = Handout(name=form.get("handout-name", "")) h.save() userprofile.handouts.add(h) userprofile.save() handouts = userprofile.handouts.all() return render(request, 'handouts/handoutlistview.html', { 'object_list': handouts, 'nbar': 'viewmytests' })
def tableview(request, **kwargs): context = {} userprof = get_or_create_up(request.user) studentusers = userprof.students.all() studentusernames = [] for i in studentusers: studentusernames.append(i.username) context['studentusernames'] = studentusernames template = loader.get_template('testmanager/tableview.html') folders = userprof.folders.all() context['nbar'] = 'testmanager' context['folders'] = folders return HttpResponse(template.render(context, request))
def archivedtableview(request): userprofile = get_or_create_up(request.user) owned_pgs = userprofile.archived_problem_groups.all() co_owned_pgs = userprofile.archived_owned_problem_groups.all() editor_pgs = userprofile.archived_editable_problem_groups.all() readonly_pgs = userprofile.archived_readonly_problem_groups.all() template = loader.get_template('groups/tableview.html') context = {} context['nbar'] = 'groups' context['owned_pgs'] = owned_pgs context['co_owned_pgs'] = co_owned_pgs context['editor_pgs'] = editor_pgs context['readonly_pgs'] = readonly_pgs context['archived'] = True return HttpResponse(template.render(context, request))
def tableview(request): userprofile = get_or_create_up(request.user) owned_pgs = userprofile.problem_groups.all() co_owned_pgs = userprofile.owned_problem_groups.all() editor_pgs = userprofile.editable_problem_groups.all() readonly_pgs = userprofile.readonly_problem_groups.all() template = loader.get_template('groups/tableview.html') group_inst = ProblemGroup(name='') ## form = GroupModelForm(instance=group_inst) ## context = {} context['form'] = form context['nbar'] = 'groups' context['owned_pgs'] = owned_pgs context['co_owned_pgs'] = co_owned_pgs context['editor_pgs'] = editor_pgs context['readonly_pgs'] = readonly_pgs context['archived'] = False return HttpResponse(template.render(context, request))
def edit_pg_view(request, pk): userprofile = get_or_create_up(request.user) prob_group = get_object_or_404(ProblemGroup, pk=pk) if prob_group not in userprofile.problem_groups.all( ) and prob_group not in userprofile.owned_problem_groups.all( ) and prob_group not in userprofile.editable_problem_groups.all( ) and prob_group not in userprofile.readonly_problem_groups.all( ) and prob_group not in userprofile.archived_problem_groups.all( ) and prob_group not in userprofile.archived_owned_problem_groups.all( ) and prob_group not in userprofile.archived_editable_problem_groups.all( ) and prob_group not in userprofile.archived_readonly_problem_groups.all(): return HttpResponse('Unauthorized', status=401) problems = prob_group.problem_objects.all() paginator = Paginator(problems, 50) page = request.GET.get('page') try: prows = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. prows = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. prows = paginator.page(paginator.num_pages) userprofile = request.user.userprofile owned_groups = userprofile.problem_groups.all() editable_groups = userprofile.editable_problem_groups.all() probgroups = list(chain(owned_groups, editable_groups)) template = loader.get_template('groups/typetagview.html') context = { 'rows': prows, 'nbar': 'groups', 'typelabel': 'Problem Groups', 'tag': 'Problem Group: ' + str(prob_group.name), 'tags': NewTag.objects.exclude(tag='root'), 'probgroups': probgroups, } return HttpResponse(template.render(context, request))
def searchresults(request): userprofile = get_or_create_up(request.user) # if request.method=='POST': # form=request.POST # next = form.get('next', '') # next = next[next.index('?'):next.index('\'>')] # for i in form: # if 'problemgroup' in i: # pk = i.split('_')[1] # prob = Problem.objects.get(pk=pk) # gpk = form[i] # probgroup = ProblemGroup.objects.get(pk=gpk) # probgroup.problems.add(prob) # probgroup.save() # return HttpResponseRedirect(next) if request.method == 'GET': page = request.GET.get('page') z = request.GET.copy() if 'page' in z: del (z['page']) current_url = z.urlencode() form = request.GET if form.get('searchform', '') == "start": testtype = form.get('tp', '') type_args = testtype.split('_') round_or_type = type_args[0] rt_pk = type_args[1] searchterm = form.get('keywords', '') if searchterm is None or searchterm == u'': keywords = [] else: keywords = searchterm.split(' ') tag = form.get('tag', '') if tag == "Unspecified": tag = '' probbegin = form.get('probbegin', '') if probbegin is None or probbegin == u'': probbegin = 0 else: probbegin = int(probbegin) probend = form.get('probend', '') if probend is None or probend == u'': probend = 10000 else: probend = int(probend) yearbegin = form.get('yearbegin', '') if yearbegin is None or yearbegin == u'': yearbegin = 0 else: yearbegin = int(yearbegin) yearend = form.get('yearend', '') if yearend is None or yearend == u'': yearend = 10000 else: yearend = int(yearend) sol_opts = form.get('sol_opts', '') if sol_opts == "sols": P = Problem.objects.exclude(solutions=None) elif sol_opts == "nosols": P = Problem.objects.filter(solutions=None) else: P = Problem.objects.all() if len(tag) > 0: if round_or_type == "T": P = P.filter(problem_number__gte=probbegin, problem_number__lte=probend).filter( year__gte=yearbegin, year__lte=yearend).filter( type_new__pk=rt_pk) # else: P = P.filter(problem_number__gte=probbegin, problem_number__lte=probend).filter( year__gte=yearbegin, year__lte=yearend).filter( round__pk=rt_pk) # P = P.filter(newtags__in=NewTag.objects.filter( tag__startswith=tag)).distinct() else: if round_or_type == "T": P = P.filter(problem_number__gte=probbegin, problem_number__lte=probend).filter( year__gte=yearbegin, year__lte=yearend).filter( type_new__pk=rt_pk).distinct() # else: P = P.filter(problem_number__gte=probbegin, problem_number__lte=probend).filter( year__gte=yearbegin, year__lte=yearend).filter( round__pk=rt_pk).distinct() # # if form.get('solution_search','') is not None: # S = Solution.objects.filter(parent_problem__problem_number__gte = probbegin,parent_problem__problem_number__lte = probend).filter(parent_problem__year__gte = yearbegin,parent_problem__year__lte = yearend).filter(parent_problem__types__type = testtype).distinct() # for i in keywords: # S = S.filter(solution_text__contains = i) # P2 = Problem.objects.filter(id__in = S.values('parent_problem_id')) if 'solutionsearch' in form: if round_or_type == "T": S = Solution.objects.filter( parent_problem__problem_number__gte=probbegin, parent_problem__problem_number__lte=probend).filter( parent_problem__year__gte=yearbegin, parent_problem__year__lte=yearend).filter( parent_problem__type_new__pk=rt_pk).filter( parent_problem__id__in=P).distinct() else: S = Solution.objects.filter( parent_problem__problem_number__gte=probbegin, parent_problem__problem_number__lte=probend).filter( parent_problem__year__gte=yearbegin, parent_problem__year__lte=yearend).filter( parent_problem__round__pk=rt_pk).filter( parent_problem__id__in=P).distinct() for i in keywords: S = S.filter(solution_text__contains=i) for i in keywords: P = P.filter( Q(problem_text__contains=i) | Q(mc_problem_text__contains=i) | Q(label=i) | Q(test_label=i)) P = Problem.objects.filter( Q(id__in=S.values('parent_problem_id')) | Q(id__in=P)) else: for i in keywords: P = P.filter( Q(problem_text__contains=i) | Q(mc_problem_text__contains=i) | Q(label=i) | Q(test_label=i)) P = list(P) P = sorted(P, key=lambda x: (x.problem_number, x.year)) owned_groups = userprofile.problem_groups.all() editable_groups = userprofile.editable_problem_groups.all() probgroups = list(chain(owned_groups, editable_groups)) paginator = Paginator(P, 25) page = request.GET.get('page') try: prows = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. prows = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. prows = paginator.page(paginator.num_pages) template = loader.get_template('search/searchresults.html') context = { 'nbar': 'search', 'rows': prows, 'searchterm': searchterm, 'current_url': current_url, 'matchnums': len(P), 'probgroups': probgroups, 'request': request, 'tags': NewTag.objects.exclude(tag='root') } if tag != "": context['tag_list'] = [tag] if round_or_type == 'T': context['testtypes'] = [Type.objects.get(pk=rt_pk)] else: context['testtypes'] = [Round.objects.get(pk=rt_pk)] return HttpResponse(template.render(context, request))
def advanced_searchresults(request): userprofile = get_or_create_up(request.user) if request.method == 'GET': page = request.GET.get('page') z = request.GET.copy() if 'page' in z: del (z['page']) current_url = z.urlencode() form = request.GET if form.get('searchform', '') == "start": testtypes = form.getlist('tp') type_pks = [] round_pks = [] for i in testtypes: type_args = i.split('_') if type_args[0] == 'T': type_pks.append(type_args[1]) else: round_pks.append(type_args[1]) searchterm = form.get('keywords', '') if searchterm is None or searchterm == u'': keywords = [] else: keywords = searchterm.split(' ') #########TAGS tag_list = form.getlist('tag') probbegin = form.get('probbegin', '') if probbegin is None or probbegin == u'': probbegin = 0 else: probbegin = int(probbegin) probend = form.get('probend', '') if probend is None or probend == u'': probend = 10000 else: probend = int(probend) yearbegin = form.get('yearbegin', '') if yearbegin is None or yearbegin == u'': yearbegin = 0 else: yearbegin = int(yearbegin) yearend = form.get('yearend', '') if yearend is None or yearend == u'': yearend = 10000 else: yearend = int(yearend) P = Problem.objects.filter( type_new__pk__in=userprofile.user_type_new.allowed_types.all()) sol_opts = form.get('sol_opts', '') if sol_opts == "sols": P = P.exclude(solutions=None) elif sol_opts == "nosols": P = P.filter(solutions=None) P = P.filter(problem_number__gte=probbegin, problem_number__lte=probend).filter( year__gte=yearbegin, year__lte=yearend) if len(type_pks) + len(round_pks) > 0: P = P.filter( Q(type_new__pk__in=type_pks) | Q(round__pk__in=round_pks)) union = request.GET.get('unionintersection') if union == None or union == 'union': if len(tag_list) > 0: every_tag = [] for t in tag_list: every_tag += list( NewTag.objects.filter(tag__startswith=t)) tag_pks = [t.pk for t in every_tag] P = P.filter(newtags__in=NewTag.objects.filter( pk__in=tag_pks)).distinct() else: if len(tag_list) > 0: every_tag = [] for t in tag_list: every_tag += list( NewTag.objects.filter(tag__startswith=t)) tag_pks = [t.pk for t in every_tag] for i in tag_pks: P = P.filter(newtags__in=NewTag.objects.filter( pk__in=[i])).distinct() for i in keywords: P = P.filter( Q(problem_text__contains=i) | Q(mc_problem_text__contains=i) | Q(label=i) | Q(test_label=i)) if 'solutionsearch' in form: S = Solution.objects.filter( parent_problem__problem_number__gte=probbegin, parent_problem__problem_number__lte=probend).filter( parent_problem__year__gte=yearbegin, parent_problem__year__lte=yearend) if len(type_pks) > 0 or len(round_pks) > 0: S = S.filter( Q(parent_problem__type_new__pk__in=type_pks) | Q(parent_problem__round__pk__in=round_pks)).distinct( ) for i in keywords: S = S.filter(solution_text__contains=i) P = Problem.objects.filter( Q(id__in=S.values('parent_problem_id')) | Q(id__in=P)) if 'prob_group' in form: pgs = form.getlist('prob_group', '') exclude_pg_list = ProblemGroup.objects.filter(pk__in=pgs) exclude_prob_pks = [] for pg in exclude_pg_list: for po in pg.problem_objects.all(): exclude_prob_pks.append(po.problem.pk) P = P.exclude(pk__in=exclude_prob_pks) P = list(P) P = sorted(P, key=lambda x: (x.problem_number, x.year)) owned_groups = userprofile.problem_groups.all() editable_groups = userprofile.editable_problem_groups.all() probgroups = list(chain(owned_groups, editable_groups)) paginator = Paginator(P, 25) page = request.GET.get('page') try: prows = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. prows = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. prows = paginator.page(paginator.num_pages) template = loader.get_template('search/searchresults.html') context = { 'nbar': 'search', 'rows': prows, 'searchterm': searchterm, 'current_url': current_url, 'matchnums': len(P), 'probgroups': probgroups, 'request': request, 'tags': NewTag.objects.exclude(tag='root') } if len(tag_list) > 0: context['tag_list'] = tag_list types = Type.objects.filter(pk__in=type_pks) rounds = Round.objects.filter(pk__in=round_pks) type_names = [] for i in types: type_names.append(i.label) for i in rounds: type_names.append(i.name) type_names.sort() context['testtypes'] = type_names return HttpResponse(template.render(context, request))
def editnewtestview(request, pk, hpk): h = get_object_or_404(Handout, pk=pk) T = get_object_or_404(NewTest, pk=hpk) Tprobs = T.problems.all() if request.method == "POST": if 'save' in request.POST: form = request.POST if 'probleminput' in form: P = list(T.problems.all()) P = sorted(P, key=lambda x: x.order) probinputs = form.getlist( 'probleminput') #could be an issue if no problems for prob in P: if 'problem_' + str(prob.pk) not in probinputs: prob.delete() for i in range(0, len(probinputs)): prob = T.problems.get(pk=probinputs[i].split('_')[1]) prob.order = i + 1 prob.save() return redirect('../../') if 'addproblems' in request.POST: form = request.POST testtype = form.get('testtype', '') searchterm = form.get('keywords', '') if searchterm is None or searchterm == u'': keywords = [] else: keywords = searchterm.split(' ') num = form.get('numproblems', '') if num is None or num == u'': num = 10 else: num = int(num) tag = form.get('tag', '') if tag == "Unspecified": tag = '' probbegin = form.get('probbegin', '') if probbegin is None or probbegin == u'': probbegin = 0 else: probbegin = int(probbegin) probend = form.get('probend', '') if probend is None or probend == u'': probend = 10000 else: probend = int(probend) yearbegin = form.get('yearbegin', '') if yearbegin is None or yearbegin == u'': yearbegin = 0 else: yearbegin = int(yearbegin) yearend = form.get('yearend', '') if yearend is None or yearend == u'': yearend = 10000 else: yearend = int(yearend) if len(tag) > 0: P = Problem.objects.filter( problem_number__gte=probbegin, problem_number__lte=probend).filter( year__gte=yearbegin, year__lte=yearend).filter(types__type=testtype) P = P.filter(newtags__in=NewTag.objects.filter( tag__startswith=tag)).distinct() else: P = Problem.objects.filter( problem_number__gte=probbegin, problem_number__lte=probend).filter( year__gte=yearbegin, year__lte=yearend).filter( types__type=testtype).distinct() for i in keywords: P = P.filter( Q(problem_text__contains=i) | Q(mc_problem_text__contains=i) | Q(label=i) | Q(test_label=i)) blocked_probs = Tprobs.values('problem_id') P = P.exclude(id__in=blocked_probs) P = list(P) P = sorted(P, key=lambda x: x.problem_number) # shuffle(P) P = P[0:min(50, num)] t = Tprobs.count() for i in range(t, t + len(P)): sp = SortableProblem(problem=P[i - t], order=i + 1, newtest_pk=T.pk) sp.save() T.problems.add(sp) T.num_problems = T.problems.count() T.save() userprofile = get_or_create_up(request.user) types = userprofile.user_type_new.allowed_types.all() tags = sorted(list(NewTag.objects.exclude(tag="root")), key=lambda x: x.tag) P = list(Tprobs) P = sorted(P, key=lambda x: x.order) return render( request, 'handouts/newtesteditview.html', { 'sortableproblems': P, 'nbar': 'viewmytests', 'test': T, 'types': types, 'tags': tags, 'handout': h })
def testeditview(request, pk): test = get_object_or_404(Test, pk=pk) userprofile = get_or_create_up(request.user) testresponses = Responses.objects.filter(test=test).filter( user_profile=userprofile) if testresponses.count() == 0: allresponses = Responses(test=test, num_problems_correct=0) allresponses.save() P = list(test.problems.all()) for i in range(0, len(P)): r = Response(response='', problem_label=P[i].label) r.save() allresponses.reponses.add(r) allresponses.save() userprofile.allresponses.add(allresponses) userprofile.save() else: allresponses = Responses.objects.get(test=test, user_profile=userprofile) msg = "" #Prepare for the add problems form types = list(Type.objects.all()) taglist = sorted(list(NewTag.objects.exclude(label='root')), key=lambda x: x.tag) testrows = [] for i in range(0, len(types)): testrows.append((types[i].type, types[i].label)) testrows = sorted(testrows, key=lambda x: x[1]) if request.method == "POST": rows = [] if request.POST.get("remove"): form = request.POST P = list(test.problems.all()) for i in range(0, len(P)): if "chk" + P[i].label not in form: test.problems.remove(P[i]) A = list(Responses.objects.filter(test=test)) for j in range(0, len(A)): r = A[j].responses.get(problem_label=P[i].label) r.delete() P = list(test.problems.all()) P = sorted(P, key=lambda x: (x.problem_number, x.year)) num_correct = 0 for i in range(0, len(P)): r = allresponses.responses.get(problem_label=P[i].label) if r.response == P[i].answer: num_correct += 1 allresponses.num_problems_correct = num_correct allresponses.save() R = allresponses.responses for i in range(0, len(P)): rows.append( (P[i].label, str(P[i].answer), "checked=\"checked\"")) msg = "Problems Removed." test.refresh_types() elif request.POST.get("addproblems"): form = request.POST if form.get('startform', '') == "start": testname = form.get('testname', '') testtype = form.get('testtype', '') tag = form.get('tag', '') if tag == "Unspecified": tag = '' num = form.get('numproblems', '') if num is None or num == u'': num = 10 else: num = int(num) probbegin = form.get('probbegin', '') if probbegin is None or probbegin == u'': probbegin = 0 else: probbegin = int(probbegin) probend = form.get('probend', '') if probend is None or probend == u'': probend = 10000 else: probend = int(probend) yearbegin = form.get('yearbegin', '') if yearbegin is None or yearbegin == u'': yearbegin = 0 else: yearbegin = int(yearbegin) yearend = form.get('yearend', '') if yearend is None or yearend == u'': yearend = 10000 else: yearend = int(yearend) if len(tag) > 0: matches = Problem.objects.filter( problem_number__gte=probbegin, problem_number__lte=probend).filter( year__gte=yearbegin, year__lte=yearend).filter(types__type=testtype) matches = matches.filter(newtags__in=NewTag.objects.filter( tag__startswith=tag)) else: matches = Problem.objects.filter( problem_number__gte=probbegin, problem_number__lte=probend).filter( year__gte=yearbegin, year__lte=yearend).filter(types__type=testtype) matches.exclude(id__in=test.problems.all()) matches = list(matches) shuffle(matches) matches = matches[0:num] for i in range(0, len(matches)): test.problems.add(matches[i]) A = list(Responses.objects.filter(test=test)) for j in range(0, len(A)): r = Response(response='', problem_label=matches[i].label) r.save() A[j].responses.add(r) A[j].save() test.save() P = test.problems.all() P = sorted(P, key=lambda x: (x.problem_number, x.year)) for i in range(0, len(P)): rows.append( (P[i].label, str(P[i].answer), "checked=\"checked\"")) test.refresh_types() else: P = list(test.problems.all()) P = sorted(P, key=lambda x: (x.problem_number, x.year)) for i in range(0, len(P)): rows.append( (P[i].label, str(P[i].answer), "checked=\"checked\"")) else: P = list(test.problems.all()) P = sorted(P, key=lambda x: (x.problem_number, x.year)) rows = [] for i in range(0, len(P)): rows.append((P[i].label, str(P[i].answer), "checked=\"checked\"")) return render( request, 'randomtest/testeditview.html', { 'rows': rows, 'pk': pk, 'nbar': 'viewmytests', 'msg': msg, 'testrows': testrows, 'taglist': taglist })
def testview(request, **kwargs): #switching to UserTest context = {} pk = kwargs['pk'] curruserprof = get_or_create_up(request.user) if 'username' in kwargs: username = kwargs['username'] context['username'] = username user = get_object_or_404(User, username=username) if user not in curruserprof.students.all(): return HttpResponse('Unauthorized', status=401) userprofile = get_or_create_up(user) else: userprofile = get_or_create_up(request.user) usertest = get_object_or_404(UserTest, pk=pk) if userprofile.usertests.filter(pk=pk).exists() == False: return HttpResponse('Unauthorized', status=401) test = get_object_or_404(Test, pk=usertest.test.pk) allresponses = usertest.responses if request.method == "POST" and 'username' not in kwargs: form = request.POST P = list(test.problems.all()) P = sorted(P, key=lambda x: (x.problem_number, x.year)) num_correct = 0 rows = [] for i in range(0, len(P)): r = allresponses.responses.get(problem_label=P[i].label) tempanswer = form.get('answer' + P[i].label) if tempanswer != None and tempanswer != '': t = timezone.now() r.attempted = 1 if r.response != tempanswer: pv = 0 if P[i].type_new.type == 'AIME': if P[i].problem_number <= 1: pv = -3 elif P[i].problem_number <= 5: pv = 1 elif P[i].problem_number <= 10: pv = 3 else: pv = 5 ur = UserResponse(test_label=test.name, test_pk=usertest.pk, response=tempanswer, problem_label=P[i].label, modified_date=t, point_value=pv) ur.save() r.modified_date = t r.response = tempanswer if r.response == P[i].answer and P[ i].question_type_new.question_type != 'proof': ur.correct = 1 ur.save() userprofile.responselog.add(ur) tempsticky = form.get('sticky' + P[i].label) if tempsticky == 'on': if r.stickied == False: s = Sticky(problem_label=P[i].label, sticky_date=timezone.now(), test_pk=usertest.pk, test_label=test.name) s.save() userprofile.stickies.add(s) r.stickied = True else: if r.stickied == True: try: s = Sticky.objects.get(problem_label=P[i].label, test_pk=usertest.pk) s.delete() except Sticky.DoesNotExist: s = None r.stickied = False r.save() if r.response == P[i].answer and P[ i].question_type_new.question_type != 'proof': num_correct += 1 if P[i].question_type_new.question_type == 'multiple choice' or P[ i].question_type_new.question_type == 'multiple choice short answer': texcode = newtexcode(P[i].mc_problem_text, P[i].label, P[i].answers()) else: texcode = newtexcode(P[i].problem_text, P[i].label, '') readablelabel = P[i].readable_label.replace('\\#', '#') rows.append( (texcode, P[i].label, str(P[i].answer), r.response, P[i].question_type_new, P[i].pk, P[i].solutions.count(), r.attempted, r.modified_date, r.stickied, readablelabel)) allresponses.num_problems_correct = num_correct allresponses.show_answer_marks = 1 allresponses.save() usertest.num_correct = num_correct usertest.save() R = allresponses.responses else: P = list(test.problems.all()) P = sorted(P, key=lambda x: (x.problem_number, x.year)) R = allresponses.responses rows = [] for i in range(0, len(P)): r = R.get(problem_label=P[i].label) if P[i].question_type_new.question_type == 'multiple choice' or P[ i].question_type_new.question_type == 'multiple choice short answer': texcode = newtexcode(P[i].mc_problem_text, P[i].label, P[i].answers()) else: texcode = newtexcode(P[i].problem_text, P[i].label, '') readablelabel = P[i].readable_label.replace('\\#', '#') rows.append( (texcode, P[i].label, str(P[i].answer), r.response, P[i].question_type_new, P[i].pk, P[i].solutions.count(), r.attempted, r.modified_date, r.stickied, readablelabel)) context['rows'] = rows context['pk'] = pk context['nbar'] = 'viewmytests' context['name'] = test.name context['show_marks'] = allresponses.show_answer_marks return render(request, 'randomtest/testview.html', context)