some refactoring and atlas improvements
This commit is contained in:
+62
-26
@@ -4,7 +4,7 @@ import os
|
||||
from typing import Self, Tuple
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.db import models
|
||||
from django.http import HttpRequest
|
||||
from django.http import Http404, HttpRequest
|
||||
from django.utils import timezone
|
||||
from smtplib import SMTPException
|
||||
|
||||
@@ -393,6 +393,30 @@ class ExamCollectionGenericBase(models.Model):
|
||||
# Is this actually used?
|
||||
cid_users = GenericRelation("generic.CidUserExam")
|
||||
|
||||
name = models.CharField(max_length=200, help_text="Name of the exam/collection")
|
||||
|
||||
active = models.BooleanField(
|
||||
help_text="If an exam/collection should be available to take", default=False
|
||||
)
|
||||
publish_results = models.BooleanField(
|
||||
help_text="If an exam/collections results should be available", default=False
|
||||
)
|
||||
|
||||
archive = models.BooleanField(
|
||||
help_text="Archived exams/collections will remain on the test system but will not be displayed by default",
|
||||
default=False,
|
||||
)
|
||||
|
||||
open_access = models.BooleanField(
|
||||
help_text="If the exam/collection is freely accessible (to view and edit on the test system)",
|
||||
default=False,
|
||||
)
|
||||
|
||||
candidates_only = models.BooleanField(
|
||||
help_text="If the exam/collection is only available to candidates who have been added",
|
||||
default=True,
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@@ -412,6 +436,42 @@ class ExamCollectionGenericBase(models.Model):
|
||||
"""Helper to check if the logged in user can access the exam"""
|
||||
return self.check_cid_user(request=request, user_id=request.user.id)
|
||||
|
||||
def check_user_can_review(
|
||||
self, cid, passcode, request: HttpRequest, active_only=False
|
||||
):
|
||||
return self.check_user_can_take(cid, passcode, request, active_only)
|
||||
|
||||
def check_user_can_take(
|
||||
self, cid, passcode, request: HttpRequest, active_only=True
|
||||
):
|
||||
"""
|
||||
Helper to check if a user is allowed to access an exam/collection
|
||||
|
||||
Args:
|
||||
cid (_type_): _description_
|
||||
passcode (_type_): _description_
|
||||
request (_type_): _description_
|
||||
|
||||
Raises:
|
||||
Http404: If user does not have access
|
||||
"""
|
||||
# If it is not active no one can take
|
||||
print(0)
|
||||
if active_only and not self.active:
|
||||
raise Http404("Exam not found")
|
||||
|
||||
print(1)
|
||||
# If candidates only check they have access
|
||||
if self.candidates_only:
|
||||
if not self.check_cid_user(cid, passcode, request):
|
||||
raise Http404("Error accessing exam")
|
||||
return
|
||||
|
||||
print(2)
|
||||
# Otherwise check that they are a valid user.
|
||||
if not request.user.is_active:
|
||||
raise Http404("Error accessing exam")
|
||||
|
||||
def check_cid_user(
|
||||
self,
|
||||
cid: int | None = None,
|
||||
@@ -524,13 +584,6 @@ class ExamCollectionGenericBase(models.Model):
|
||||
|
||||
|
||||
class ExamBase(ExamCollectionGenericBase):
|
||||
name = models.CharField(max_length=200, help_text="Name of the exam")
|
||||
# exam_questions = SortedManyToManyField(Long, related_name="exams", blank="true")
|
||||
|
||||
active = models.BooleanField(
|
||||
help_text="If an exam should be available to take", default=False
|
||||
)
|
||||
|
||||
exam_mode = models.BooleanField(
|
||||
help_text="If an exam should be taken in exam mode (users results will be submited to the server for marking)",
|
||||
default=False,
|
||||
@@ -541,10 +594,6 @@ class ExamBase(ExamCollectionGenericBase):
|
||||
default=False,
|
||||
)
|
||||
|
||||
publish_results = models.BooleanField(
|
||||
help_text="If an exams results should be available", default=False
|
||||
)
|
||||
|
||||
recreate_json = models.BooleanField(
|
||||
help_text="If the json cache needs updating", default=False
|
||||
)
|
||||
@@ -554,16 +603,6 @@ class ExamBase(ExamCollectionGenericBase):
|
||||
default=1, help_text="auto incrementing field when json recreated"
|
||||
)
|
||||
|
||||
archive = models.BooleanField(
|
||||
help_text="Archived exams will remain on the test system but will not be displayed by default",
|
||||
default=False,
|
||||
)
|
||||
|
||||
open_access = models.BooleanField(
|
||||
help_text="If the exam is freely accessible (to view and edit on the test system)",
|
||||
default=False,
|
||||
)
|
||||
|
||||
authors_only = models.BooleanField(
|
||||
help_text="If true only exam authors will be able to view.",
|
||||
default=False,
|
||||
@@ -1090,10 +1129,7 @@ class CidUserExam(models.Model):
|
||||
else:
|
||||
end_time = f"{ self.end_time:%Y-%m-%d %H:%M }"
|
||||
|
||||
|
||||
return (
|
||||
f"{user}: {start_time} {end_time}"
|
||||
)
|
||||
return f"{user}: {start_time} {end_time}"
|
||||
|
||||
|
||||
CID_GROUP_EXAMS = (
|
||||
|
||||
Reference in New Issue
Block a user