def get_facilities_and_location(request): def _filter_facilities_by_location(facilities, location): if _is_region(location): return facilities.filter(Q(supplied_by__location__parent_id=location.id) | Q(supplied_by__location=location)) elif _is_district(location): return facilities.filter(supplied_by__location=location) return facilities base_facilities = SupplyPoint.objects.filter(active=True, type__code="facility") user_loc = get_user_location(request.user) # filter initial list by location filtered_facilities = _filter_facilities_by_location(base_facilities, user_loc) if request.location: if _are_not_related(request.location, user_loc): messages.error(request, "You don't have permission to view that. Location reset.") location = user_loc else: location = request.location filtered_facilities = _filter_facilities_by_location(filtered_facilities, request.location) elif user_loc: location = user_loc else: location = Location.objects.get(name="MOHSW") request.location = location return (filtered_facilities, location)
def _user_regions(user): regions = _regions() location = get_user_location(user) if _is_district(location): return regions.filter(pk=location.parent.pk) elif _is_region(location): return regions.filter(pk=location.pk) return regions
def _user_districts(user): districts = _districts() location = get_user_location(user) if _is_district(location): return districts.filter(pk=location.pk) elif _is_region(location): return districts.filter(parent_id=location.pk) return districts
def get_facilities_and_location(request): def _filter_facilities_by_location(facilities, location): if _is_region(location): return facilities.filter( Q(supplied_by__location__parent_id=location.id) | Q(supplied_by__location=location)) elif _is_district(location): return facilities.filter(supplied_by__location=location) return facilities base_facilities = SupplyPoint.objects.filter(active=True, type__code="facility") user_loc = get_user_location(request.user) # filter initial list by location filtered_facilities = _filter_facilities_by_location( base_facilities, user_loc) if request.location: if _are_not_related(request.location, user_loc): messages.error( request, "You don't have permission to view that. Location reset.") location = user_loc else: location = request.location filtered_facilities = _filter_facilities_by_location( filtered_facilities, request.location) elif user_loc: location = user_loc else: location = Location.objects.get(name="MOHSW") request.location = location return (filtered_facilities, location)