def filter(self, *args, **kw): if 'ekey' in kw: ekey = kw.pop('ekey') try: assert ekey is not None kw['id'] = decode(ekey, get_model_sub_key(self.model)) except (AssertionError, EncryptedIDDecodeError): return self.none() return super(EncryptedIDQuerySet, self).filter(*args, **kw)
def __init__(self, lhs, rhs): try: rhs = decode(rhs) except EncryptedIDDecodeError: raise lhs.target.model.DoesNotExist( '%s matching query does not exist.' % lhs.target.model._meta.object_name ) super(EkeyLookup, self).__init__(lhs, rhs)
def test_decode(): with pytest.raises(EncryptedIDDecodeError): decode("") # strucr.error with pytest.raises(EncryptedIDDecodeError): decode("1") # binascii.Error with pytest.raises(EncryptedIDDecodeError): decode(encode(0)[:-1] + 'Z') # crc error
def test_decode(): with pytest.raises(EncryptedIDDecodeError): decode("", "") # strucr.error with pytest.raises(EncryptedIDDecodeError): decode("1", "") # binascii.Error with pytest.raises(EncryptedIDDecodeError): decode(encode(0, "")[:-1] + 'Z', "") # crc error
def get_by_ekey(self, ekey, **kw): return self.get(id=decode(ekey, get_model_sub_key(self.model)), **kw)
def test_encode(): assert decode(encode(10)) == 10
def examAttempt(request, pk): exams = Exam.objects.all() checked = False for exam in exams: if exam.ekey == pk: checked = True break if not checked: return TemplateResponse(request, status=404, template='errors/error_400.html') exam_id = decode(pk, "teacher_exam") exam_details = Exam.objects.get(id=exam_id) exam_period = exam_details.exam_period questions = Question.objects.filter(exam_id=exam_id) questions_count = Question.objects.filter(exam_id=exam_id).count() questions = Question.objects.filter(exam_id=exam_id) student = User.objects.get(id=request.user.id) exam = Exam.objects.get(id=exam_id) paginator = Paginator(questions, 6) page = request.GET.get('page') questions_list = list() answer_nums = list() if request.method == 'POST': for question in questions: answer_nums.append(question.question_number) questions_list.append(question) studentResponseForm = StudentResponseForm(request.POST) t = 0 if studentResponseForm.is_valid(): # well functioning code # StudentExam.objects.create(exam=exam, student=student, exam_status="Active") # studentResponseObjects = list() # for t in range(questions_count): # answer = request.POST.get('answer_' + str(answer_nums[t]), None) # studentResponseObjects.append(StudentResponse(student=student, question=questions_list[t], student_response_text=answer, # student_response_degree=23)) # StudentResponse.objects.bulk_create(studentResponseObjects) # examAttemptConfirm(request) request.session['exam_request'] = request.POST request.session['attempted_exam_id'] = exam_id return redirect('exam-confirm-report') translations = { 'remove_question': _('Remove Question'), 'question': _('Question') } context = { 'questions': paginator.get_page(page), 'questions2': questions, 'page_obj': paginator.get_page(page), 'is_paginated': True, 'num_pages': paginator.num_pages, 'per_page': 6, 'translations': translations, 'exam_question_count': questions_count, 'exam_period': exam_period, 'exam_id': exam_id, 'ekey': pk, 'title': _("Exam Attempt"), } return render(request, 'student/handle_exam.html', context)
def get_by_ekey(self, ekey, **kw): return self.get(id=decode(ekey), **kw)
def test_encode(): sub_key = uuid.uuid4().hex assert decode(encode(10, sub_key), sub_key) == 10