Skip to content

Commit

Permalink
Merge branch 'release/0.3.33' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Nov 28, 2023
2 parents c3141a9 + a0718eb commit a22049f
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions edc_sites/valid_site_for_subject_or_raise.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from warnings import warn

from django.core.exceptions import ObjectDoesNotExist
from edc_registration import get_registered_subject_model_cls
Expand All @@ -19,30 +20,35 @@ class InvalidSubjectError(Exception):
pass


def valid_site_for_subject_or_raise(subject_identifier: str) -> Site:
def valid_site_for_subject_or_raise(
subject_identifier: str, skip_get_current_site: bool | None = None
) -> Site:
"""Raises an InvalidSiteError exception if the subject_identifier is not
from the current site.
* Confirms by querying RegisteredSubject.
* If subject_identifier is invalid will raise ObjectDoesNotExist
"""
current_site = get_site_model_cls().objects.get_current()
try:
get_registered_subject_model_cls().objects.get(
site=current_site, subject_identifier=subject_identifier
obj = get_registered_subject_model_cls().objects.get(
subject_identifier=subject_identifier
)
except ObjectDoesNotExist:
raise InvalidSubjectError(
"Unknown subject. "
f"Searched `{get_registered_subject_model_cls()._meta.label_lower}`. "
f"Got subject_identifier=`{subject_identifier}`."
)
if skip_get_current_site:
warn("Skipping validation of current site against registered subject site.")
current_site = obj.site
else:
current_site = get_site_model_cls().objects.get_current()
try:
obj = get_registered_subject_model_cls().objects.get(
subject_identifier=subject_identifier
get_registered_subject_model_cls().objects.get(
subject_identifier=subject_identifier, site=current_site
)
except ObjectDoesNotExist:
raise InvalidSubjectError(
"Unknown subject. "
f"Searched `{get_registered_subject_model_cls()._meta.label_lower}`. "
f"Got subject_identifier=`{subject_identifier}`."
)
else:
raise InvalidSiteForSubjectError(
f"Invalid site for subject. {subject_identifier}. Expected `{obj.site.name}`. "
f"Got `{current_site.name}`"
Expand Down

0 comments on commit a22049f

Please sign in to comment.