Skip to content

Commit

Permalink
Merge branch 'release/0.3.80' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Apr 10, 2024
2 parents 9ed17f7 + db6ff7c commit 3f7020d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,6 +16,9 @@
)
from edc_consent.site_consents import site_consents

if TYPE_CHECKING:
from django.contrib.sites.models import Site


class ConsentDefinitionFormValidatorMixin:

Expand All @@ -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:
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand Down
8 changes: 5 additions & 3 deletions edc_consent/site_consents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3f7020d

Please sign in to comment.