This commit is contained in:
Ross
2022-05-21 12:21:52 +01:00
parent 160bb13b64
commit ee9b942893
14 changed files with 194 additions and 284 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ from django.utils.html import format_html
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
from django.utils.html import mark_safe
from sortedm2m.fields import SortedManyToManyField
+31 -1
View File
@@ -1,15 +1,21 @@
from django.urls import reverse
import pytest
from rapids.models import Abnormality, Exam
from rapids.views import GenericExamViews
#@pytest.mark.django_db
def test_create_abnormality(db):
abnormality = Abnormality.objects.create(name="Abnorm")
assert abnormality.name == "Abnorm"
@pytest.fixture
def create_basic_user(db, django_user_model):
return django_user_model.objects.create_superuser("basicuser", "ross@xkjq.uk", "password")
@pytest.fixture
def create_exam(db):
return Exam.objects.create(name="test exam")
return Exam.objects.create(name="test exam", exam_mode=True)
def test_exam_creation(create_exam):
exams = Exam.objects.filter(name="test exam")
@@ -18,3 +24,27 @@ def test_exam_creation(create_exam):
exam = exams.first()
assert exam.time_limit == 35 * 60
def test_exam_list(rf, admin_user, create_exam):
request = rf.get('/rapids/exam/')
# Remember that when using RequestFactory, the request does not pass
# through middleware. If your view expects fields such as request.user
# to be set, you need to set them explicitly.
# The following line sets request.user to an admin user.
request.user = admin_user
response = GenericExamViews.exam_list(request)
#print(response)
assert response.status_code == 200
def test_exam_active_admin(rf, admin_user, create_exam, client):
request = rf.get(reverse('rapids:active_exams'))
request.user = admin_user
response = GenericExamViews.active_exams(request)
print(response.content)
assert response.status_code == 200
def test_exam_active_basic_user(create_basic_user, create_exam, client):
client.login(username="basicuser", password="password")
response = client.get(reverse('rapids:active_exams'))
print(response.content)
assert response.status_code == 200
+1 -56
View File
@@ -509,61 +509,6 @@ class RapidView(LoginRequiredMixin, SingleTableMixin, FilterView):
filterset_class = RapidFilter
def loadJsonAnswer(answer, cid, eid, uid):
exam = get_object_or_404(Exam, pk=eid)
if not exam.check_cid_user(cid, answer["passcode"], user_id=uid):
return False, JsonResponse(
{"success": False, "error": "invalid cid / passcode"}
)
if uid:
exiting_answers = CidUserAnswer.objects.filter(
question__id=answer["qid"], exam__id=eid, user__id=uid
)
else:
exiting_answers = CidUserAnswer.objects.filter(
question__id=answer["qid"], exam__id=eid, cid=cid
)
posted_answer = answer["ans"]
## qidn 1 does not hold answer data (but should always arrive prior to 2)
# if answer["qidn"] == "1":
# posted_answer = ""
# Normal answers are just posted with the answer "Normal"
normal = False
if posted_answer == "Normal":
normal = True
if not exiting_answers:
if uid:
ans = CidUserAnswer(answer=posted_answer, normal=normal, user=User.objects.get(id=uid))
else:
ans = CidUserAnswer(answer=posted_answer, normal=normal, cid=cid)
ans.question_id = answer["qid"]
ans.exam_id = eid
ans.full_clean()
ans.save()
else:
# Update an existing answer
# should never be more than one (famous last words)
ans = exiting_answers[0]
if answer["qidn"] == "1":
ans.normal = normal
ans.answer = ""
elif answer["qidn"] == "2" and not ans.normal:
ans.answer = posted_answer
ans.full_clean()
ans.save()
return True, None
@login_required
def mark_all(request, exam_pk, sk):
@@ -818,7 +763,7 @@ class QuestionDelete(AuthorOrCheckerRequiredMixin, DeleteView):
GenericExamViews = ExamViews(
Exam, Rapid, Answer, CidUserAnswer, "rapids", "rapid", loadJsonAnswer
Exam, Rapid, Answer, CidUserAnswer, "rapids", "rapid"
)