<commit_before># -*- coding: utf-8 -*-

from django.db import transaction

from rest_framework import viewsets
from rest_framework import status
from rest_framework import mixins
from rest_framework.response import Response

from .pagination import HeadersPaginationMixin, ConditionalPaginationMixin


class AtomicMixin(object):
    @transaction.atomic
    def post(self, *args, **kwargs):
        return super().post(*args, **kwargs)

    @transaction.atomic
    def delete(self, *args, **kwargs):
        return super().delete(*args, **kwargs)

    @transaction.atomic
    def put(self, *args, **kwargs):
        return super().put(*args, **kwargs)

    @transaction.atomic
    def patch(self, *args, **kwargs):
        return super().patch(*args, **kwargs)


class DestroyModelMixin(object):
    """
    Self version of DestroyModelMixin with
    pre_delete hook method.
    """

    def pre_delete(self, obj):
        pass

    def destroy(self, request, *args, **kwargs):
        obj = self.get_object()
        self.pre_delete(obj)
        obj.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


class PreconditionMixin(object):
    def pre_conditions_on_save(self, obj):
        pass

    def pre_conditions_on_delete(self, obj):
        pass

    def pre_save(self, obj):
        super().pre_save(obj)
        self.pre_conditions_on_save(obj)

    def pre_delete(self, obj):
        super().pre_delete(obj)
        self.pre_conditions_on_delete(obj)


class ModelCrudViewSet(AtomicMixin,
                       PreconditionMixin,
                       HeadersPaginationMixin,
                       ConditionalPaginationMixin,
                       mixins.CreateModelMixin,
                       mixins.RetrieveModelMixin,
                       mixins.UpdateModelMixin,
                       DestroyModelMixin,
                       mixins.ListModelMixin,
                       viewsets.GenericViewSet):
    pass


class ModelListViewSet(AtomicMixin,
                       PreconditionMixin,
                       HeadersPaginationMixin,
                       ConditionalPaginationMixin,<commit_msg>Added an API mixin to use a diferent serializers in the list action method<commit_after># -*- coding: utf-8 -*-

from django.db import transaction

from rest_framework import viewsets
from rest_framework import status
from rest_framework import mixins
from rest_framework.response import Response

from .pagination import HeadersPaginationMixin, ConditionalPaginationMixin


class AtomicMixin(object):
    @transaction.atomic
    def post(self, *args, **kwargs):
        return super().post(*args, **kwargs)

    @transaction.atomic
    def delete(self, *args, **kwargs):
        return super().delete(*args, **kwargs)

    @transaction.atomic
    def put(self, *args, **kwargs):
        return super().put(*args, **kwargs)

    @transaction.atomic
    def patch(self, *args, **kwargs):
        return super().patch(*args, **kwargs)


class DestroyModelMixin(object):
    """
    Self version of DestroyModelMixin with
    pre_delete hook method.
    """

    def pre_delete(self, obj):
        pass

    def destroy(self, request, *args, **kwargs):
        obj = self.get_object()
        self.pre_delete(obj)
        obj.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


class PreconditionMixin(object):
    def pre_conditions_on_save(self, obj):
        pass

    def pre_conditions_on_delete(self, obj):
        pass

    def pre_save(self, obj):
        super().pre_save(obj)
        self.pre_conditions_on_save(obj)

    def pre_delete(self, obj):
        super().pre_delete(obj)
        self.pre_conditions_on_delete(obj)


class DetailAndListSerializersMixin(object):
    """
    Use a diferent serializer class to the list action.
    """
    list_serializer_class = None

    def get_serializer_class(self):
        if self.action == "list" and self.list_serializer_class:
            return self.list_serializer_class
        return super().get_serializer_class()


class ModelCrudViewSet(AtomicMixin,
                       DetailAndListSerializersMixin,
                       PreconditionMixin,
                       HeadersPaginationMixin,
                       ConditionalPaginationMixin,
                       mixins.CreateModelMixin,
                       mixins.RetrieveModelMixin,
                       mixins.UpdateModelMixin,
                       DestroyModelMixin,
                       mixins.ListModelMixin,
                       viewsets.GenericViewSet):
    pass


class ModelListViewSet(AtomicMixin,
                       DetailAndListSerializersMixin,
                       PreconditionMixin,
                       HeadersPaginationMixin,
                       ConditionalPaginationMixin,
{'fraction_non_alphanumeric': 0.08505747126436781, 'fraction_numerical': 0.001671891327063741, 'mean_word_length': 2.3052486187845305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7019077', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1285, 'n_words': 286}