add - new vivibility filter by org type & remove city province from device
This commit is contained in:
@@ -33,13 +33,14 @@ class RegionFilterMixin:
|
||||
scope = organization.field_of_activity
|
||||
print(scope)
|
||||
if scope == 'CI':
|
||||
if hasattr(queryset.model, 'city_id'):
|
||||
if hasattr(queryset.model, 'city'):
|
||||
queryset = queryset.filter(city=organization.city)
|
||||
print(queryset)
|
||||
elif hasattr(queryset.model, 'user'):
|
||||
queryset = queryset.filter(user__city=organization.city)
|
||||
|
||||
elif scope == 'PR':
|
||||
if hasattr(queryset.model, 'province_id'):
|
||||
if hasattr(queryset.model, 'province'):
|
||||
queryset = queryset.filter(province=organization.province)
|
||||
elif hasattr(queryset.model, 'user'):
|
||||
queryset = queryset.filter(user__province=organization.province)
|
||||
|
||||
24
apps/core/services/visibility_service.py
Normal file
24
apps/core/services/visibility_service.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from apps.core.visibility_registry import VISIBILITY_MAP_BY_ORG_KEY
|
||||
|
||||
|
||||
def apply_visibility_filter_by_org_type(queryset, org):
|
||||
"""
|
||||
Dynamically filter queryset based on user's organization type and model.
|
||||
"""
|
||||
|
||||
if not org:
|
||||
return queryset.none()
|
||||
|
||||
model_name = queryset.model.__name__.lower()
|
||||
|
||||
if model_name not in VISIBILITY_MAP_BY_ORG_KEY:
|
||||
return queryset
|
||||
|
||||
org_type_key = getattr(org.type, 'key', None)
|
||||
visibility_field = VISIBILITY_MAP_BY_ORG_KEY[model_name].get(org_type_key)
|
||||
|
||||
if visibility_field is None:
|
||||
return queryset
|
||||
|
||||
filter_kwargs = {f"{visibility_field}": org}
|
||||
return queryset.filter(**filter_kwargs)
|
||||
@@ -17,3 +17,10 @@ VISIBILITY_MAP = {
|
||||
# 'stakeholdershareamount': 'registering_organization',
|
||||
# 'posfreeproducts': 'organization',
|
||||
}
|
||||
|
||||
VISIBILITY_MAP_BY_ORG_KEY = {
|
||||
'device': {
|
||||
'PSP': 'organization',
|
||||
'CO': 'assignment__client__organization'
|
||||
},
|
||||
}
|
||||
|
||||
@@ -42,18 +42,6 @@ class Device(BaseModel):
|
||||
server_in = models.BooleanField(default=False)
|
||||
latitude = models.FloatField(default=0)
|
||||
longitude = models.FloatField(default=0)
|
||||
city = models.ForeignKey(
|
||||
City,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='devices',
|
||||
null=True
|
||||
)
|
||||
province = models.ForeignKey(
|
||||
Province,
|
||||
on_delete=models.CASCADE,
|
||||
related_name='devices',
|
||||
null=True
|
||||
)
|
||||
is_activated = models.BooleanField(default=False)
|
||||
pre_registered = models.BooleanField(default=False)
|
||||
organization = models.ForeignKey(
|
||||
|
||||
@@ -22,6 +22,7 @@ from apps.core.api import BaseViewSet
|
||||
from apps.core.mixins.admin_mixin import AdminFilterMixin
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
||||
from apps.core.services.visibility_service import apply_visibility_filter_by_org_type
|
||||
from apps.pos_device import models as pos_models
|
||||
from apps.pos_device.web.api.v1.serilaizers import device as device_serializer
|
||||
from apps.pos_device.web.api.v1.viewsets.client import POSClientViewSet
|
||||
@@ -97,11 +98,12 @@ class DeviceViewSet(BaseViewSet, SoftDeleteMixin, viewsets.ModelViewSet, AdminFi
|
||||
""" list of company devices """
|
||||
organization = get_organization_by_user(request.user)
|
||||
# using admin filter mixin to get query
|
||||
devices = self.get_query(self.get_queryset(
|
||||
devices = self.get_queryset(
|
||||
visibility_by_org_scope=True
|
||||
) if organization.free_visibility_by_scope else self.get_queryset().filter(
|
||||
organization=organization
|
||||
))
|
||||
) if organization.free_visibility_by_scope else self.get_queryset()
|
||||
|
||||
queryset = apply_visibility_filter_by_org_type(devices, organization)
|
||||
print(queryset)
|
||||
|
||||
# paginate devices
|
||||
page = self.paginate_queryset(devices)
|
||||
|
||||
Reference in New Issue
Block a user