diff --git a/edc_consent/form_validators/consent_definition_form_validator_mixin.py b/edc_consent/form_validators/consent_definition_form_validator_mixin.py index c4efa11..25dad18 100644 --- a/edc_consent/form_validators/consent_definition_form_validator_mixin.py +++ b/edc_consent/form_validators/consent_definition_form_validator_mixin.py @@ -1,6 +1,7 @@ from __future__ import annotations from datetime import datetime +from typing import TYPE_CHECKING from django.utils.translation import gettext as _ from edc_form_validators import INVALID_ERROR @@ -15,6 +16,9 @@ ) from edc_consent.site_consents import site_consents +if TYPE_CHECKING: + from django.contrib.sites.models import Site + class ConsentDefinitionFormValidatorMixin: @@ -24,17 +28,22 @@ def subject_consent(self): return cdef.model_cls.objects.get(subject_identifier=self.subject_identifier) def get_consent_datetime_or_raise( - self, report_datetime: datetime = None, fldname: str = None, error_code: str = None + self, + report_datetime: datetime = None, + site: Site = None, + fldname: str = None, + error_code: str = None, ) -> datetime: """Returns the consent_datetime of this subject""" consent_obj = self.get_consent_or_raise( - report_datetime=report_datetime, fldname=fldname, error_code=error_code + report_datetime=report_datetime, site=site, fldname=fldname, error_code=error_code ) return consent_obj.consent_datetime def get_consent_or_raise( self, report_datetime: datetime = None, + site: Site = None, fldname: str | None = None, error_code: str | None = None, ) -> datetime: @@ -48,7 +57,9 @@ def get_consent_or_raise( error_code = error_code or INVALID_ERROR try: consent_obj = site_consents.get_consent_or_raise( - subject_identifier=self.subject_identifier, report_datetime=report_datetime + subject_identifier=self.subject_identifier, + report_datetime=report_datetime, + site_id=getattr(site, "id", None), ) except (NotConsentedError, ConsentDefinitionNotConfiguredForUpdate) as e: self.raise_validation_error({fldname: str(e)}, error_code) diff --git a/edc_consent/modelform_mixins/requires_consent_modelform_mixin.py b/edc_consent/modelform_mixins/requires_consent_modelform_mixin.py index 3c96722..e54f4b6 100644 --- a/edc_consent/modelform_mixins/requires_consent_modelform_mixin.py +++ b/edc_consent/modelform_mixins/requires_consent_modelform_mixin.py @@ -42,6 +42,7 @@ def validate_against_consent(self) -> None: consent_obj = site_consents.get_consent_or_raise( subject_identifier=self.get_subject_identifier(), report_datetime=self.report_datetime, + site_id=self.site.id, ) except (NotConsentedError, ConsentDefinitionNotConfiguredForUpdate) as e: raise forms.ValidationError({"__all__": str(e)}) diff --git a/edc_consent/site_consents.py b/edc_consent/site_consents.py index 166e553..ca58108 100644 --- a/edc_consent/site_consents.py +++ b/edc_consent/site_consents.py @@ -168,17 +168,19 @@ def get_consent_definition( Filters the registry by each param given. """ - cdefs = self.get_consent_definitions( + opts = dict( model=model, report_datetime=report_datetime, version=version, site=site, screening_model=screening_model, - **kwargs, ) + cdefs = self.get_consent_definitions(**opts, **kwargs) if len(cdefs) > 1: as_string = ", ".join(list(set([cdef.name for cdef in cdefs]))) - raise SiteConsentError(f"Multiple consent definitions returned. Got {as_string}. ") + raise SiteConsentError( + f"Multiple consent definitions returned. Using {opts}. Got {as_string}. " + ) return cdefs[0] def get_consent_definitions(