fix bug of filter_by_region for models that had no relation to region but has user relation
This commit is contained in:
@@ -17,22 +17,32 @@ class RegionFilterMixin:
|
||||
organization = get_organization_by_user(self.request.user) # noqa
|
||||
|
||||
if city_id:
|
||||
queryset = queryset.filter(city_id=id(city_id))
|
||||
if hasattr(queryset.model, 'city_id'):
|
||||
queryset = queryset.filter(city_id=id(city_id))
|
||||
elif hasattr(queryset.model, 'user'):
|
||||
queryset = queryset.filter(user__city=organization.city)
|
||||
|
||||
elif province_id:
|
||||
if hasattr(queryset.model, 'province_id'):
|
||||
queryset = queryset.filter(province_id=id(province_id))
|
||||
elif hasattr(queryset.model, 'user'):
|
||||
queryset = queryset.filter(user__province=organization.city)
|
||||
|
||||
# filter by organization type region
|
||||
if org:
|
||||
scope = organization.activity_fields
|
||||
|
||||
scope = organization.field_of_activity
|
||||
print(scope)
|
||||
if scope == 'CI':
|
||||
queryset = queryset.filter(city=organization.city)
|
||||
if hasattr(queryset.model, 'city_id'):
|
||||
queryset = queryset.filter(city=organization.city)
|
||||
elif hasattr(queryset.model, 'user'):
|
||||
queryset = queryset.filter(user__city=organization.city)
|
||||
|
||||
elif scope == 'PR':
|
||||
if hasattr(queryset.model, 'province_id'):
|
||||
queryset = queryset.filter(province=organization.province)
|
||||
elif hasattr(queryset.model, 'user'):
|
||||
queryset = queryset.filter(user__province=organization.city)
|
||||
|
||||
# if organization is admin of system
|
||||
elif scope == 'CO':
|
||||
|
||||
@@ -19,6 +19,7 @@ class BaseViewSet(RegionFilterMixin, viewsets.ModelViewSet):
|
||||
|
||||
if self.request.method.lower() == 'get' and not self.kwargs.get('pk'):
|
||||
queryset = self.filter_by_region(queryset, org=True)
|
||||
print(queryset)
|
||||
|
||||
if not user_relation.first().role.type.key == 'ADM':
|
||||
model_name = queryset.model.__name__.lower()
|
||||
|
||||
Reference in New Issue
Block a user