Enhance logging and error handling in collection access methods and WSGI configuration

This commit is contained in:
Ross
2026-04-13 13:44:25 +01:00
parent 463a741185
commit 90c8f0b60d
4 changed files with 38 additions and 8 deletions
+19 -5
View File
@@ -654,6 +654,8 @@ class ExamOrCollectionGenericBase(models.Model, AuthorMixin):
e.g. user management
"""
class InactiveException(Exception):
pass
# Is this actually used? It is but should be replaced on the inherited class
cid_users = GenericRelation("generic.CidUserExam")
@@ -768,7 +770,9 @@ class ExamOrCollectionGenericBase(models.Model, AuthorMixin):
Raises:
Http404: If user does not have access
"""
logger.debug(f"Checking user can take: {cid=}, passcode=****, user={user}, active_only={active_only}")
if user is not None and self.is_author(user):
logger.debug("User is author, allowing access")
return
# If we are limiting by dates check that the current date is within the range
@@ -778,24 +782,32 @@ class ExamOrCollectionGenericBase(models.Model, AuthorMixin):
):
# Raise with an explanatory message so the frontend can
# surface the reason to the end user.
logger.debug("Exam is outside of allowed dates, denying access")
raise Http404("Exam not currently available (outside allowed dates)")
# If it is not active no one can take
elif active_only and not self.active:
raise Http404("Exam currently inactive")
logger.debug("Exam is not active, denying access")
raise self.InactiveException("Exam currently inactive")
# If candidates only check they have access
if self.candidates_only:
logger.debug("Exam is candidates only, checking CID/user access")
if not self.check_cid_user(cid, passcode, user):
logger.debug("CID/user access check failed, denying access")
raise Http404("Error accessing exam")
return
# Otherwise check that they are a valid user.
if user is None:
logger.debug("No user provided, denying access")
raise Http404("Error accessing exam")
if not user.is_active:
logger.debug("User is not active, denying access")
raise Http404("Error accessing exam")
logger.debug("User passed all checks, allowing access")
return
def check_cid_user(
@@ -808,7 +820,7 @@ class ExamOrCollectionGenericBase(models.Model, AuthorMixin):
allow_authors: bool = True,
):
"""Checks if a user (cid or otherwise) is allowed to access (and take) the exam"""
print(f"Check cid user: {cid=}, {passcode=}, exam_id={self.pk}, {user=}, {user_id=}, {allow_authors=}")
logger.debug(f"Check cid user: {cid=}, {passcode=}, exam_id={self.pk}, {user=}, {user_id=}, {allow_authors=}")
if user is not None and user.is_superuser:
return True
@@ -829,14 +841,16 @@ class ExamOrCollectionGenericBase(models.Model, AuthorMixin):
return True
if not self.valid_cid_users.exists() and not self.valid_user_users.exists():
logger.debug("No valid CID or user users found")
return False
# Start by checking if the logged in user can access
logger.debug(f"Checking user access for user_id={user_id}")
if user_id is not None:
if self.valid_user_users.filter(pk=user_id).exists():
return True
else:
logger.debug("No valid user users found for user_id={user_id}")
# Then test CID data
if self.valid_cid_users.exists():
@@ -2284,4 +2298,4 @@ class FindingBase(models.Model):
return get_pretty_json(json.loads(self.annotation_json))
def get_pretty_viewport_json(self):
return get_pretty_json(json.loads(self.viewport_json))
return get_pretty_json(json.loads(self.viewport_json))