2019-07-03 14:28:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-01-16 11:02:09 +00:00
|
|
|
from django.db.models import QuerySet
|
2020-05-08 04:46:18 +00:00
|
|
|
from rest_framework.viewsets import ModelViewSet, GenericViewSet
|
2019-07-03 14:28:20 +00:00
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
2021-03-17 05:01:59 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.api import CommonApiMixin, RelationMixin
|
2020-05-28 09:00:42 +00:00
|
|
|
from orgs.utils import current_org
|
2020-06-04 12:00:39 +00:00
|
|
|
from ..utils import set_to_root_org
|
2019-07-03 14:28:20 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2021-03-08 09:19:59 +00:00
|
|
|
'RootOrgViewMixin', 'OrgModelViewSet', 'OrgBulkModelViewSet', 'OrgQuerySetMixin',
|
2023-02-20 06:13:22 +00:00
|
|
|
'OrgGenericViewSet', 'OrgRelationMixin', 'OrgReadonlyModelViewSet'
|
2019-07-03 14:28:20 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class RootOrgViewMixin:
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
set_to_root_org()
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
2019-09-19 06:48:25 +00:00
|
|
|
class OrgQuerySetMixin:
|
2023-01-16 11:02:09 +00:00
|
|
|
queryset: QuerySet
|
|
|
|
get_serializer_class: callable
|
|
|
|
action: str
|
|
|
|
|
2019-07-03 14:28:20 +00:00
|
|
|
def get_queryset(self):
|
2019-10-18 07:05:45 +00:00
|
|
|
if hasattr(self, 'model'):
|
|
|
|
queryset = self.model.objects.all()
|
|
|
|
else:
|
|
|
|
assert self.queryset is None, (
|
|
|
|
"'%s' should not include a `queryset` attribute"
|
|
|
|
% self.__class__.__name__
|
|
|
|
)
|
|
|
|
queryset = super().get_queryset()
|
2019-07-04 07:36:57 +00:00
|
|
|
return queryset
|
2019-07-03 14:28:20 +00:00
|
|
|
|
2019-09-19 06:48:25 +00:00
|
|
|
|
2021-03-17 05:01:59 +00:00
|
|
|
class OrgViewSetMixin(OrgQuerySetMixin):
|
2021-03-18 02:10:05 +00:00
|
|
|
pass
|
2021-03-17 05:01:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrgModelViewSet(CommonApiMixin, OrgViewSetMixin, ModelViewSet):
|
2019-09-19 06:48:25 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-03-17 05:01:59 +00:00
|
|
|
class OrgGenericViewSet(CommonApiMixin, OrgViewSetMixin, GenericViewSet):
|
2020-05-08 04:46:18 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-03-17 05:01:59 +00:00
|
|
|
class OrgBulkModelViewSet(CommonApiMixin, OrgViewSetMixin, BulkModelViewSet):
|
2019-08-21 12:27:21 +00:00
|
|
|
def allow_bulk_destroy(self, qs, filtered):
|
2019-12-05 07:09:25 +00:00
|
|
|
qs_count = qs.count()
|
|
|
|
filtered_count = filtered.count()
|
|
|
|
if filtered_count == 1:
|
|
|
|
return True
|
2020-08-10 06:45:03 +00:00
|
|
|
if qs_count > filtered_count:
|
|
|
|
return True
|
2019-09-12 03:17:15 +00:00
|
|
|
if self.request.query_params.get('spm', ''):
|
|
|
|
return True
|
2019-08-21 12:27:21 +00:00
|
|
|
return False
|
|
|
|
|
2019-07-03 14:28:20 +00:00
|
|
|
|
2023-02-20 06:13:22 +00:00
|
|
|
class OrgReadonlyModelViewSet(OrgModelViewSet):
|
|
|
|
http_method_names = ['get', 'head', 'options']
|
|
|
|
|
|
|
|
|
2020-05-28 09:00:42 +00:00
|
|
|
class OrgRelationMixin(RelationMixin):
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2021-03-18 08:44:32 +00:00
|
|
|
if not current_org.is_root():
|
|
|
|
org_id = current_org.org_id()
|
2020-05-28 09:00:42 +00:00
|
|
|
queryset = queryset.filter(**{f'{self.from_field}__org_id': org_id})
|
|
|
|
return queryset
|