HiPhone_BE/core/mixins.py

32 lines
904 B
Python
Raw Permalink Normal View History

2023-05-03 13:07:36 +09:00
from django.db.models.query import QuerySet
class ActionBasedMixin:
serializer_class_map = {}
permission_classes_map = {}
queryset_map = {}
def get_serializer_class(self):
return self.serializer_class_map.get(
self.action, self.serializer_class
)
def get_permissions(self):
if self.action in self.permission_classes_map:
return [
permission()
for permission in self.permission_classes_map[self.action]
]
return super().get_permissions()
def get_queryset(self):
if self.action in self.queryset_map:
queryset = self.queryset_map.get(self.action, self.queryset)
if isinstance(queryset, QuerySet):
queryset = queryset.all()
return queryset
return super().get_queryset()
class Meta:
abstract = True