Skip to content

Commit

Permalink
update ContraceptiveUseReproductiveHistoryFormValidator & AlcoholToba…
Browse files Browse the repository at this point in the history
…ccoUseFormValidator
  • Loading branch information
frdrckaman committed May 27, 2022
1 parent 62304b5 commit dffd6f0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions edcs_constants/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
SMOKER = "smoker"
DEAD = "dead"
DECLINED = "Declined"
DECLINE_TO_ANSWER = "decline_to_answer"
DEFAULTER = "defaulter"
DELETE = "DELETE"
DONE = "done"
Expand Down
5 changes: 3 additions & 2 deletions edcs_subject/choices.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from edcs_constants.constants import (
ASTHMA,
COPD,
DECLINE_TO_ANSWER,
DONT_KNOW,
GREATER_THAN_6MONTHS,
INTERSTITIAL_LUNG_DISEASE,
Expand All @@ -25,9 +26,9 @@
YES_PAST_SMOKER,
YES_PAST_USER,
)
from edcs_subject.constants import MISSED_VISIT, SCHEDULED, UNSCHEDULED

from .constants import NON_SMALL_CELL
from edcs_subject.constants import MISSED_VISIT, SCHEDULED, UNSCHEDULED

MISS_ARV = (
("at_least_once_every_week", "At least once every week"),
Expand Down Expand Up @@ -199,7 +200,7 @@
(YES_CURRENT_USER, "Yes, Current user"),
(YES_PAST_USER, "Yes, Past user"),
(NO, "No"),
("decline_to_answer", "Decline to answer"),
(DECLINE_TO_ANSWER, "Decline to answer"),
(NOT_APPLICABLE, "Not applicable"),
)

Expand Down
35 changes: 33 additions & 2 deletions edcs_subject/forms/contraceptive_reproductive_form.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django import forms

from edcs_constants.constants import (
DECLINE_TO_ANSWER,
NO,
NOT_APPLICABLE,
OTHER,
YES,
Expand All @@ -14,16 +16,24 @@


class ContraceptiveUseReproductiveHistoryFormValidator(FormValidator):
def __init__(self):
super().__init__()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.use_contraceptives = self.cleaned_data.get("use_contraceptives")
self.how_long_use_oral_contraceptives = self.cleaned_data.get(
"how_long_use_contraceptives"
)

self.contraceptives = self.cleaned_data.get("contraceptives")

self.contraceptives_na = self.contraceptives.filter(
name=NOT_APPLICABLE
).exists()

def clean(self):
self.validate_how_long_use_oral_contraceptives()

self.validate_contraceptives()

self.applicable_if(
YES,
field="post_menopausal_hormone_therapy",
Expand All @@ -33,6 +43,13 @@ def clean(self):
OTHER, field="contraceptives", field_required="contraceptives_other"
)

def validate_contraceptives(self):
if (
self.use_contraceptives == YES_CURRENT_USER
or self.use_contraceptives == YES_PAST_USER
) and self.contraceptives_na:
raise forms.ValidationError({"contraceptives": "This field is Applicable "})

def validate_how_long_use_oral_contraceptives(self):
if (
self.use_contraceptives == YES_CURRENT_USER
Expand All @@ -41,6 +58,20 @@ def validate_how_long_use_oral_contraceptives(self):
raise forms.ValidationError(
{"how_long_use_contraceptives": "This field is Applicable "}
)
elif (
self.use_contraceptives == NO
or self.use_contraceptives == DECLINE_TO_ANSWER
or self.use_contraceptives == NOT_APPLICABLE
) and (self.contraceptives.count() > 1):
raise forms.ValidationError({"contraceptives": "Invalid choices "})
elif (
self.use_contraceptives == NO
or self.use_contraceptives == DECLINE_TO_ANSWER
or self.use_contraceptives == NOT_APPLICABLE
) and not self.contraceptives_na:
raise forms.ValidationError(
{"contraceptives": "This field is Not Applicable "}
)


class ContraceptiveUseReproductiveHistoryForm(FormValidatorMixin, forms.ModelForm):
Expand Down

0 comments on commit dffd6f0

Please sign in to comment.