-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make asset, bed, assetbed get_queryset reusable based on user
- Loading branch information
1 parent
55b9eb8
commit 3aa52e8
Showing
4 changed files
with
65 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from care.facility.models import Asset, AssetBed, Bed | ||
from care.users.models import User | ||
from care.utils.cache.cache_allowed_facilities import get_accessible_facilities | ||
|
||
|
||
def get_asset_bed_queryset(user, queryset=None): | ||
queryset = queryset or AssetBed.objects.all() | ||
if user.is_superuser: | ||
pass | ||
elif user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]: | ||
queryset = queryset.filter(bed__facility__state=user.state) | ||
elif user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]: | ||
queryset = queryset.filter(bed__facility__district=user.district) | ||
else: | ||
allowed_facilities = get_accessible_facilities(user) | ||
queryset = queryset.filter(bed__facility__id__in=allowed_facilities) | ||
return queryset | ||
|
||
|
||
def get_bed_queryset(user, queryset=None): | ||
queryset = queryset or Bed.objects.all() | ||
if user.is_superuser: | ||
pass | ||
elif user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]: | ||
queryset = queryset.filter(facility__state=user.state) | ||
elif user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]: | ||
queryset = queryset.filter(facility__district=user.district) | ||
else: | ||
allowed_facilities = get_accessible_facilities(user) | ||
queryset = queryset.filter(facility__id__in=allowed_facilities) | ||
return queryset | ||
|
||
|
||
def get_asset_queryset(user, queryset=None): | ||
queryset = queryset or Asset.objects.all() | ||
if user.is_superuser: | ||
pass | ||
elif user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"]: | ||
queryset = queryset.filter(current_location__facility__state=user.state) | ||
elif user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]: | ||
queryset = queryset.filter(current_location__facility__district=user.district) | ||
else: | ||
allowed_facilities = get_accessible_facilities(user) | ||
queryset = queryset.filter( | ||
current_location__facility__id__in=allowed_facilities | ||
) | ||
return queryset |