Skip to content

Commit

Permalink
Merge branch 'release/0.3.46' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Jul 2, 2023
2 parents 3c3cdce + 083c5b5 commit 107cf9c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

from django import forms
from edc_constants.constants import NO, YES
from edc_screening.utils import get_subject_screening_model_cls
from edc_screening.utils import (
get_subject_screening_model_cls,
get_subject_screening_or_raise,
)

if TYPE_CHECKING:
from edc_screening.model_mixins import ScreeningModelMixin
Expand All @@ -29,13 +32,20 @@ def subject_screening(self):
"screening_identifier"
) or self.initial.get("screening_identifier")
if not screening_identifier:
raise ConsentModelFormMixinError(
raise forms.ValidationError(
"Unable to determine the screening identifier. "
f"This should be part of the initial form data. Got {self.cleaned_data}"
)
return self.subject_screening_model_cls.objects.get(
screening_identifier=screening_identifier
)
# try:
# subject_screening = self.subject_screening_model_cls.objects.get(
# screening_identifier=screening_identifier
# )
# except ObjectDoesNotExist:
# raise forms.ValidationError(
# "Unable to find screening form. Invalid screening identifier. "
# f"Got `{screening_identifier}`"
# )
return get_subject_screening_or_raise(screening_identifier, is_modelform=True)

def clean_consent_reviewed(self) -> str:
consent_reviewed = self.cleaned_data.get("consent_reviewed")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from edc_screening.utils import is_eligible_or_raise

from ...consent_helper import ConsentHelper
from ...site_consents import site_consents
from .clean_fields_modelform_mixin import CleanFieldsModelformMixin
Expand All @@ -7,6 +9,7 @@
class ConsentModelFormMixin(CleanFieldsModelformMixin, CustomValidationMixin):
def clean(self) -> dict:
cleaned_data = super().clean()
self.validate_is_eligible_or_raise()
self.validate_initials_with_full_name()
self.validate_gender_of_consent()
self.validate_is_literate_and_witness()
Expand All @@ -29,3 +32,9 @@ def clean(self) -> dict:
self.validate_identity_with_unique_fields()
self.validate_identity_plus_version_is_unique()
return cleaned_data

def validate_is_eligible_or_raise(self) -> None:
screening_identifier = self.get_field_or_raise(
"screening_identifier", "Screening identifier is required."
)
is_eligible_or_raise(screening_identifier=screening_identifier)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from dateutil.relativedelta import relativedelta
from django import forms
from edc_constants.constants import NO, YES
from edc_model_form.utils import get_field_or_raise
from edc_utils import AgeValueError, age, formatted_age

from ...site_consents import ConsentObjectDoesNotExist, SiteConsentError, site_consents
Expand Down Expand Up @@ -36,15 +37,9 @@ def consent_config(self) -> Consent:
return consent_config

def get_field_or_raise(self, name: str, msg: str) -> Any:
"""Returns a field value from cleaned_data if the key
exists, or from the model instance.
"""
if name in self.cleaned_data and not self.cleaned_data.get(name):
raise forms.ValidationError({"__all__": msg})
value = self.cleaned_data.get(name, getattr(self.instance, name))
if not value:
raise forms.ValidationError({"__all__": msg})
return value
return get_field_or_raise(
name, msg, cleaned_data=self.cleaned_data, instance=self.instance
)

@property
def consent_datetime(self) -> datetime:
Expand Down
2 changes: 2 additions & 0 deletions edc_consent/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class SubjectScreening(models.Model):

report_datetime = models.DateTimeField()

eligible = models.BooleanField(default=False)

eligibility_datetime = models.DateTimeField()

class Meta:
Expand Down
1 change: 1 addition & 0 deletions edc_consent/tests/tests/test_consent_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def prepare_subject_consent(
gender=gender,
screening_identifier=screening_identifier,
report_datetime=consent_datetime,
eligible=True,
eligibility_datetime=consent_datetime,
)
subject_consent = baker.prepare_recipe(
Expand Down
4 changes: 2 additions & 2 deletions edc_consent/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from django import forms
from django.apps import apps as django_apps
Expand All @@ -26,7 +26,7 @@ def get_consent_model_name() -> str:
return settings.SUBJECT_CONSENT_MODEL


def get_consent_model_cls() -> models.Model:
def get_consent_model_cls() -> Any:
return django_apps.get_model(get_consent_model_name())


Expand Down

0 comments on commit 107cf9c

Please sign in to comment.