fix - quota exception in distribution limit & sale licence and visibility free by org
This commit is contained in:
44
apps/authentication/services/visibility_services.py
Normal file
44
apps/authentication/services/visibility_services.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import typing
|
||||
|
||||
from django.db.models import Q
|
||||
|
||||
from apps.authentication.models import Organization
|
||||
from apps.core.visibility_registry import VISIBILITY_MAP
|
||||
|
||||
|
||||
def get_visible_organizations(org: Organization) -> typing.Any:
|
||||
"""
|
||||
get visible organizations
|
||||
"""
|
||||
if org.free_visibility_by_scope:
|
||||
|
||||
if org.field_of_activity == 'CO':
|
||||
return Organization.objects.all()
|
||||
|
||||
elif org.field_of_activity == 'CI':
|
||||
return Organization.objects.filter(city=org.city)
|
||||
|
||||
elif org.field_of_activity == 'PR':
|
||||
return Organization.objects.filter(province=org.province)
|
||||
|
||||
return Organization.objects.filter(id=org.id)
|
||||
|
||||
|
||||
def apply_visibility_filter(queryset, org):
|
||||
model_name = queryset.model.__name__.lower()
|
||||
org_fields = VISIBILITY_MAP.get(model_name)
|
||||
|
||||
if not org_fields:
|
||||
return queryset
|
||||
|
||||
visible_orgs = get_visible_organizations(org)
|
||||
org_ids = visible_orgs.values_list('id', flat=True)
|
||||
|
||||
if isinstance(org_fields, str):
|
||||
return queryset.filter(**{f"{org_fields}__in": org_ids})
|
||||
|
||||
q_obj = Q()
|
||||
for field in org_fields:
|
||||
q_obj |= Q(**{f"{field}__in": org_ids})
|
||||
|
||||
return queryset.filter(q_obj)
|
||||
Reference in New Issue
Block a user