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

from mycarehub.utils.excel_utils import AuditSerializerExcelIO

from ..mixins import AuditSerializerExcelIOMixin


class BaseView(ModelViewSet, AuditSerializerExcelIOMixin):
    """Base class for most application views.

    This view's `create` method has been extended to support the creation of
    a single or multiple records.
    """

    excel_io_class = AuditSerializerExcelIO

    def create(self, request, *args, **kwargs):
        """Create and persist single or multiple records."""

        # Check if the data given by the user is composed of a single or
        # multiple records.
        has_many = isinstance(request.data, list)

        # Initialize this viewset's serializer to handle multiple or a single
        # records depending on the value of `has_many` and proceed to create
        # the data.
        serializer = self.get_serializer(data=request.data, many=has_many)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
