diff --git a/core/mixins.py b/core/mixins.py new file mode 100644 index 0000000..2357e0f --- /dev/null +++ b/core/mixins.py @@ -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