Add ActionBasedMixin
This commit is contained in:
parent
f37c0a9cff
commit
2f3184d923
31
core/mixins.py
Normal file
31
core/mixins.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
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
|
Loading…
Reference in New Issue
Block a user