class CommunityHealthUnitFilter(CommonFieldsFilterset): def chu_pending_approval(self, value): if value in TRUTH_NESS: return self.filter( Q(is_approved=False, is_rejected=False, has_edits=False) | Q(is_approved=True, is_rejected=False, has_edits=True) | Q(is_approved=False, is_rejected=True, has_edits=True)) else: return self.filter( Q(is_approved=True, is_rejected=False, has_edits=False) | Q(is_approved=False, is_rejected=True, has_edits=False)) name = django_filters.CharFilter(lookup_type='icontains') ward = ListCharFilter(name='facility__ward') constituency = ListCharFilter(name='facility__ward__constituency') county = ListCharFilter(name='facility__ward__constituency__county') is_approved = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) is_rejected = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) has_edits = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) pending_approval = django_filters.MethodFilter(action=chu_pending_approval) class Meta(object): model = CommunityHealthUnit
class WorldBorderFilter(CommonFieldsFilterset): name = ListCharFilter(lookup_expr='icontains') code = ListCharFilter(lookup_expr='icontains') class Meta(CommonFieldsFilterset.Meta): model = WorldBorder exclude = ('mpoly', )
class WardBoundaryFilter(CommonFieldsFilterset): id = ListCharFilter(lookup_expr='icontains') name = ListCharFilter(lookup_expr='icontains') code = ListCharFilter(lookup_expr='exact') area = ListCharFilter(lookup_expr='exact') class Meta(CommonFieldsFilterset.Meta): model = WardBoundary exclude = ('mpoly', )
class FacilityExportExcelMaterialViewFilter(django_filters.FilterSet): search = SearchFilter(name='search') county = ListCharFilter(lookup_type='exact') code = ListCharFilter(lookup_type='exact') constituency = ListCharFilter(lookup_type='exact') ward = ListCharFilter(lookup_type='exact') owner = ListCharFilter(lookup_type='exact') owner_type = ListCharFilter(lookup_type='exact') number_of_beds = ListIntegerFilter(lookup_type='exact') number_of_cots = ListIntegerFilter(lookup_type='exact') open_whole_day = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) open_late_night = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) open_weekends = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) open_public_holidays = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) keph_level = ListCharFilter(lookup_type='exact') facility_type = ListCharFilter(lookup_type='exact') operation_status = ListCharFilter(lookup_type='exact') service = ListUUIDFilter(lookup_type='exact', name='services') service_category = ListUUIDFilter(lookup_type='exact', name='categories') class Meta(object): model = FacilityExportExcelMaterialView
class FacilityCoordinatesFilter(CommonFieldsFilterset): ward = ListCharFilter(lookup_expr='exact', name='facility__ward') constituency = ListCharFilter( lookup_expr='exact', name='facility__ward__constituency' ) county = ListCharFilter( lookup_expr='exact', name='facility__ward__constituency__county' ) class Meta(CommonFieldsFilterset.Meta): model = FacilityCoordinates exclude = ('coordinates', )
class FacilityExportExcelMaterialViewFilter(django_filters.FilterSet): def filter_number_beds(self, qs, name, value): return qs.filter(beds__gte=1) def filter_number_cots(self, qs, name, value): return qs.filter(cots__gte=1) search = ClassicSearchFilter(name='search') county = ListCharFilter(lookup_expr='exact') code = ListCharFilter(lookup_expr='exact') constituency = ListCharFilter(lookup_expr='exact') ward = ListCharFilter(lookup_expr='exact') owner = ListCharFilter(lookup_expr='exact') owner_type = ListCharFilter(lookup_expr='exact') number_of_beds = django_filters.CharFilter(method='filter_number_beds') number_of_cots = django_filters.CharFilter(method='filter_number_cots') open_whole_day = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) open_late_night = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) open_weekends = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) open_public_holidays = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) keph_level = ListCharFilter(lookup_expr='exact') facility_type = ListCharFilter(lookup_expr='exact') operation_status = ListCharFilter(lookup_expr='exact') service = ListUUIDFilter(lookup_expr='exact', name='services') service_category = ListUUIDFilter(lookup_expr='exact', name='categories') service_name = ClassicSearchFilter(name='service_names') approved_national_level = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) class Meta(CommonFieldsFilterset.Meta): model = FacilityExportExcelMaterialView exclude = ('services', 'categories', 'service_names', )
class RegulatorSyncFilter(CommonFieldsFilterset): mfl_code_null = NullFilter(name='mfl_code') county = ListCharFilter(lookup_type='exact') class Meta(object): model = RegulatorSync
class FacilityFilter(CommonFieldsFilterset): def service_filter(self, value): categories = value.split(',') facility_ids = [] for facility in self.filter(): for cat in categories: service_count = FacilityService.objects.filter( service__category=cat, facility=facility).count() if service_count > 0: facility_ids.append(facility.id) return self.filter(id__in=list(set(facility_ids))) def filter_approved_facilities(self, value): if value in TRUTH_NESS: return self.filter(Q(approved=True) | Q(rejected=True)) else: return self.filter(rejected=False, approved=False) def facilities_pending_approval(self, value): if value in TRUTH_NESS: return self.filter(Q(rejected=False), Q(has_edits=True) | Q(approved=False)) else: return self.filter( Q(rejected=True) | Q(has_edits=False) & Q(approved=True)) id = ListCharFilter(lookup_type='icontains') name = django_filters.CharFilter(lookup_type='icontains') code = ListIntegerFilter(lookup_type='exact') description = ListCharFilter(lookup_type='icontains') facility_type = ListCharFilter(lookup_type='icontains') keph_level = ListCharFilter(lookup_type='exact') operation_status = ListCharFilter(lookup_type='icontains') ward = ListCharFilter(lookup_type='icontains') sub_county = ListCharFilter(lookup_type='exact') sub_county_code = ListCharFilter(name="sub_county__code", lookup_type='exact') ward_code = ListCharFilter(name="ward__code", lookup_type='icontains') county_code = ListCharFilter(name='ward__constituency__county__code', lookup_type='icontains') constituency_code = ListCharFilter(name='ward__constituency__code', lookup_type='icontains') county = ListCharFilter(name='ward__constituency__county', lookup_type='exact') constituency = ListCharFilter(name='ward__constituency', lookup_type='icontains') owner = ListCharFilter(lookup_type='icontains') owner_type = ListCharFilter(name='owner__owner_type', lookup_type='exact') officer_in_charge = ListCharFilter(lookup_type='icontains') number_of_beds = ListIntegerFilter(lookup_type='exact') number_of_cots = ListIntegerFilter(lookup_type='exact') open_whole_day = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) open_late_night = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) open_weekends = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) open_public_holidays = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) is_classified = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) is_published = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) is_approved = django_filters.MethodFilter( action=filter_approved_facilities) service_category = django_filters.MethodFilter(action=service_filter) has_edits = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) rejected = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) regulated = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) approved = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) closed = django_filters.TypedChoiceFilter(choices=BOOLEAN_CHOICES, coerce=strtobool) pending_approval = django_filters.MethodFilter( action=facilities_pending_approval) class Meta(object): model = Facility
class AdminOfficeFilter(CommonFieldsFilterset): code = ListCharFilter(name='code') class Meta(CommonFieldsFilterset.Meta): model = AdminOffice exclude = ('coordinates', )
class FacilityFilter(CommonFieldsFilterset): def service_filter(self, qs, name, value): categories = value.split(',') facility_ids = [] for facility in self.filter(): for cat in categories: service_count = FacilityService.objects.filter( service__category=cat, facility=facility).count() if service_count > 0: facility_ids.append(facility.id) return qs.filter(id__in=list(set(facility_ids))) def filter_approved_facilities(self, qs, name, value): if value in TRUTH_NESS: return qs.filter(Q(approved=True) | Q(rejected=True)) else: return qs.filter(rejected=False, approved=False) def filter_unpublished_facilities_national_level(self, qs, name, value): """ This is in order to allow the facilities to be seen so that they can be approved at the national level and assigned an MFL code. """ return qs.filter( approved_national_level=None, code=None, approved=True, has_edits=False,closed=False ) def filter_incomplete_facilities(self, qs, name, value): """ Filter the incomplete/complete facilities """ incomplete = qs.filter(code=None) if value in TRUTH_NESS: return incomplete else: return qs.exclude(id__in=[facility.id for facility in incomplete]) def facilities_pending_approval(self, qs, name, value): incomplete = qs.filter(code=not None) incomplete_facility_ids = [facility.id for facility in incomplete] if value in TRUTH_NESS: return qs.filter( Q( Q(rejected=False), Q(has_edits=True) | Q(approved=None,rejected=False) ) | Q( Q(rejected=False), Q(has_edits=True) | Q(approved=None,rejected=False)) ).exclude(id__in=incomplete_facility_ids) else: return qs.filter( Q(rejected=True) | Q(has_edits=False) & Q(approved=None) ).exclude(id__in=incomplete_facility_ids) def filter_national_rejected(self, qs, name, value): rejected_national = qs.filter(rejected=False,code=None, approved=True,approved_national_level=False) if value in TRUTH_NESS: return rejected_national else: return qs.exclude(id__in=[facility.id for facility in rejected_national]) def filter_number_beds(self, qs, name, value): return qs.filter(number_of_beds__gte=1) def filter_number_cots(self, qs, name, value): return self.filter(number_of_cots__gte=1) id = ListCharFilter(lookup_expr='icontains') name = django_filters.CharFilter(lookup_expr='icontains') code = ListIntegerFilter(lookup_expr='exact') description = ListCharFilter(lookup_expr='icontains') facility_type = ListCharFilter(lookup_expr='icontains') keph_level = ListCharFilter(lookup_expr='exact') operation_status = ListCharFilter(lookup_expr='icontains') ward = ListCharFilter(lookup_expr='icontains') sub_county = ListCharFilter(lookup_expr='exact', name='ward__sub_county') sub_county_code = ListCharFilter( name="ward__sub_county__code", lookup_expr='exact') ward_code = ListCharFilter(name="ward__code", lookup_expr='icontains') county_code = ListCharFilter( name='ward__constituency__county__code', lookup_expr='icontains') constituency_code = ListCharFilter( name='ward__constituency__code', lookup_expr='icontains') county = ListCharFilter( name='ward__constituency__county', lookup_expr='exact') constituency = ListCharFilter( name='ward__constituency', lookup_expr='icontains') owner = ListCharFilter(lookup_expr='icontains') owner_type = ListCharFilter(name='owner__owner_type', lookup_expr='exact') officer_in_charge = ListCharFilter(lookup_expr='icontains') number_of_beds = django_filters.CharFilter(method='filter_number_beds') number_of_cots = django_filters.CharFilter(method='filter_number_cots') open_whole_day = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) open_late_night = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) open_weekends = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) open_public_holidays = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) is_classified = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) is_published = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) is_approved = django_filters.CharFilter( method='filter_approved_facilities') service_category = django_filters.CharFilter( method=service_filter) has_edits = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) rejected = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) regulated = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) approved = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) closed = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) pending_approval = django_filters.CharFilter( method='facilities_pending_approval') rejected_national = django_filters.CharFilter( method='filter_national_rejected') search = ClassicSearchFilter(name='name') incomplete = django_filters.CharFilter( method='filter_incomplete_facilities') to_publish = django_filters.CharFilter( method='filter_unpublished_facilities_national_level') approved_national_level = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) reporting_in_dhis = django_filters.TypedChoiceFilter( choices=BOOLEAN_CHOICES, coerce=strtobool) class Meta(CommonFieldsFilterset.Meta): model = Facility