Skip to content

Commit

Permalink
Merge branch 'release/0.3.70' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Feb 1, 2024
2 parents c27ddad + b48847c commit 7ea0605
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
from django import forms
from edc_constants.constants import NO, YES
from edc_model_form.utils import get_field_or_raise
from edc_sites.site import sites as site_sites
from edc_utils import AgeValueError, age, formatted_age

from ...exceptions import SiteConsentError
from ...site_consents import ConsentDefinitionDoesNotExist, site_consents
from ...exceptions import ConsentDefinitionValidityPeriodError
from ...utils import InvalidInitials, verify_initials_against_full_name

if TYPE_CHECKING:
Expand All @@ -29,23 +27,17 @@ class ConsentModelFormValidationMixin:

@property
def consent_definition(self) -> ConsentDefinition:
"""Returns a ConsentDefinition instance or raises on
missing data.
"""Returns a ConsentDefinition instance or raises
if consent date not within consent definition validity
period.
"""
current_site_id = getattr(
self.instance.site, "id", site_sites.get_current_site().site_id
)
try:
consent_definition = site_consents.get_consent_definition(
model=self._meta.model._meta.label_lower,
report_datetime=self.consent_datetime,
site=site_sites.get(current_site_id),
)
except (ConsentDefinitionDoesNotExist, SiteConsentError) as e:
raise forms.ValidationError(e)
if not consent_definition.version:
raise forms.ValidationError("Unable to determine consent version")
return consent_definition
cdef: ConsentDefinition = self.subject_screening.consent_definition
if self.consent_datetime:
try:
cdef.valid_for_datetime_or_raise(self.consent_datetime)
except ConsentDefinitionValidityPeriodError as e:
raise forms.ValidationError({"consent_datetime": str(e)})
return cdef

def get_field_or_raise(self, name: str, msg: str) -> Any:
return get_field_or_raise(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from edc_utils.date import to_local, to_utc
from edc_visit_schedule.site_visit_schedules import site_visit_schedules

from .. import NotConsentedError
from ..exceptions import ConsentDefinitionDoesNotExist

if TYPE_CHECKING:
Expand Down Expand Up @@ -38,6 +39,8 @@ def validate_against_consent(self) -> None:
model_obj = self.get_consent_or_raise()
except ConsentDefinitionDoesNotExist as e:
raise forms.ValidationError(e)
except NotConsentedError as e:
raise forms.ValidationError(e)
else:
if floor_secs(to_utc(self.report_datetime)) < floor_secs(
model_obj.consent_datetime
Expand Down
10 changes: 8 additions & 2 deletions edc_consent/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
ReviewFieldsMixin,
VulnerabilityFieldsMixin,
)
from edc_consent.model_mixins import ConsentModelMixin, RequiresConsentFieldsModelMixin
from edc_consent.model_mixins import (
ConsentDefinitionModelMixin,
ConsentModelMixin,
RequiresConsentFieldsModelMixin,
)


class SubjectScreening(SiteModelMixin, ConsentDefinitionModelMixin, BaseUuidModel):
consent_definition = None

class SubjectScreening(SiteModelMixin, BaseUuidModel):
screening_identifier = models.CharField(max_length=25, unique=True)

initials = models.CharField(max_length=5, default="TO")
Expand Down
8 changes: 5 additions & 3 deletions edc_consent/tests/tests/test_consent_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,19 @@ def setUp(self):
self.study_open_datetime = ResearchProtocolConfig().study_open_datetime
self.study_close_datetime = ResearchProtocolConfig().study_close_datetime

self.consent_factory(
self.convent_v1 = self.consent_factory(
start=self.study_open_datetime,
end=self.study_open_datetime + timedelta(days=50),
version="1.0",
)
self.consent_factory(
SubjectScreening.consent_definition = self.convent_v1

self.convent_v2 = self.consent_factory(
start=self.study_open_datetime + timedelta(days=51),
end=self.study_open_datetime + timedelta(days=100),
version="2.0",
)
self.consent_factory(
self.convent_v3 = self.consent_factory(
start=self.study_open_datetime + timedelta(days=101),
end=self.study_open_datetime + timedelta(days=150),
version="3.0",
Expand Down

0 comments on commit 7ea0605

Please sign in to comment.