def common_clean(self): try: household_structure = self.household_member.household_structure except AttributeError: household_structure = self.household_structure if self.prohibit_log_entry_by_report_datetime: report_datetime = get_utcnow() else: report_datetime = self.report_datetime todays_log_entry_or_raise( household_structure=household_structure, report_datetime=report_datetime) super().common_clean()
def current_household_log_entry(self): """Returns a household log entry model instance or None. """ if not self._current_household_log_entry: try: obj = todays_log_entry_or_raise( self.household_structure, report_datetime=get_utcnow()) except HouseholdLogRequired: obj = None self._current_household_log_entry = obj return self._current_household_log_entry
def validate_refused_enumeration(self): cleaned_data = self.cleaned_data household_structure = cleaned_data.get('household_structure') household_log_entry = todays_log_entry_or_raise( household_structure=household_structure, report_datetime=get_utcnow()) if household_log_entry.household_status == REFUSED_ENUMERATION: raise forms.ValidationError('Household log entry for today shows ' 'household status as refused ' 'therefore you cannot add a member') return cleaned_data
def get_current_log_entry(self, report_datetime=None): for log_entry in self.log_entries.all(): household_structure = log_entry.household_log.household_structure try: log_entry = todays_log_entry_or_raise( household_structure, report_datetime=report_datetime or get_utcnow()) except HouseholdLogRequired: pass else: return log_entry return None
def validate_eligible_if_no_household_participant(self): cleaned_data = self.cleaned_data household_structure = cleaned_data.get('household_structure') household_log_entry = todays_log_entry_or_raise( household_structure=household_structure, report_datetime=get_utcnow()) if household_log_entry.household_status == NO_HOUSEHOLD_INFORMANT: raise forms.ValidationError('Household log entry for today shows ' 'household status as no household informant ' 'therefore you cannot add ' 'representative eligibility') return cleaned_data
def validate_eligible_participant_absent(self): cleaned_data = self.cleaned_data household_structure = cleaned_data.get('household_structure') household_log_entry = todays_log_entry_or_raise( household_structure=household_structure, report_datetime=get_utcnow()) if household_log_entry.household_status == ELIGIBLE_REPRESENTATIVE_ABSENT: raise forms.ValidationError('Household log entry for today shows ' 'household status as absent ' 'therefore you cannot add ' 'representative eligibility') return cleaned_data
def clean(self): cleaned_data = super().clean() try: todays_log_entry_or_raise( household_structure=cleaned_data.get( 'household_member').household_structure, report_datetime=cleaned_data.get('report_datetime')) except HouseholdLogRequired as e: raise forms.ValidationError(str(e)) rdate = arrow.Arrow.fromdatetime( cleaned_data.get('report_datetime'), tzinfo=cleaned_data.get('report_datetime').tzinfo) try: obj = self._meta.model.objects.get( household_member=cleaned_data.get('household_member'), report_datetime__date=rdate.to('UTC').date()) except ObjectDoesNotExist: pass else: if obj.id != self.instance.id: raise forms.ValidationError( {'report_datetime': 'A report already exists for {}.'.format( rdate.to(str(get_default_timezone())).date().strftime( '%Y-%m-%d'))}) household_member = cleaned_data.get('household_member') if household_member.consent: if (cleaned_data.get('report_datetime') > household_member.consent.consent_datetime): raise forms.ValidationError( 'Report may not be submitted for survey \'{}\'. ' 'Subject is already consented for this survey.'.format( household_member.consent.survey_schedule_object.name)) return cleaned_data
def clean(self): self.household_member_model_cls = django_apps.get_model( self.household_member_model) self.deceased_member_model_cls = django_apps.get_model( self.deceased_member_model) # validate cannot change if enrollment_checklist_completed if self.instance.id and self.instance.enrollment_checklist_completed: raise forms.ValidationError( 'Enrollment checklist exists. This member may not be changed.', code='enrollment_checklist_completed') # require household log entry try: self.household_log_entry = todays_log_entry_or_raise( household_structure=self.household_structure, report_datetime=self.report_datetime) except HouseholdLogRequired as e: raise forms.ValidationError(e, code='household_log_entry') # validate_refused_enumeration if self.household_log_entry.household_status == REFUSED_ENUMERATION: raise forms.ValidationError( 'Household log entry for today shows household status as refused ' 'therefore you cannot add a member', code='refused_enumeration') self.validate_member_integrity_with_previous() # validate age of HoH if self.relation == HEAD_OF_HOUSEHOLD and not self.age_in_years >= 18: raise forms.ValidationError({ 'age_in_years': 'Head of Household must be 18 years or older.' }) elif self.eligible_hoh and self.age_in_years < 18: raise forms.ValidationError({ 'age_in_years': (f'This household member completed the HoH questionnaire. ' f'You cannot change their age to less than 18. ' f'Got {self.age_in_years}.') }) self.validate_relation_and_gender() # validate_initials_on_first_name try: assert self.first_name[0] == self.initials[0] except AssertionError: raise forms.ValidationError({ 'initials': 'Invalid initials. First name does not match first initial.' }) except TypeError: pass if self.survival_status in [ALIVE, UNKNOWN]: try: obj = self.deceased_member_model_cls.objects.get( household_member=self.instance) except ObjectDoesNotExist: pass else: aware_date = obj.site_aware_date.strftime('%Y-%m-%d') raise forms.ValidationError({ 'survival_status': f'Member was reported as deceased on {aware_date}' }) self.not_applicable_if(NO, field='present_today', field_applicable='inability_to_participate') self.not_applicable_if(NO, field='present_today', field_applicable='study_resident') self.not_applicable_if(NO, field='present_today', field_applicable='personal_details_changed') self.required_if(YES, field='personal_details_changed', field_required='details_change_reason')