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

from mneia_admin_backend.models import Person
from mneia_admin_backend.serializers import PersonSerializer
from mneia_admin_backend.utils import musicbrainz


class PersonViewSet(viewsets.ModelViewSet):
    serializer_class = PersonSerializer
    queryset = Person.objects.all()

    def list(self, request):
        # Overrides:
        # https://github.com/encode/django-rest-framework/blob/e95e91ccf2065cbf474892b73ebd5790e5a4ae14/rest_framework/mixins.py#L37
        queryset = Person.objects.all()
        name = request.query_params.get("name")
        if name is not None:
            queryset = queryset.filter(name__icontains=name)
        limit = request.query_params.get("limit")
        if limit is not None:
            queryset = queryset[: int(limit)]
        serializer = PersonSerializer(queryset, many=True)
        return Response(serializer.data)

    def create(self, request, *args, **kwargs):
        # Overrides:
        # https://github.com/encode/django-rest-framework/blob/e95e91ccf2065cbf474892b73ebd5790e5a4ae14/rest_framework/mixins.py#L12
        try:
            mbid = musicbrainz.get_mbid_from_request(request)
        except KeyError:
            pass
        else:
            mb_obj = musicbrainz.get_person_by_mbid(mbid)
            request.data.update(mb_obj)

        serializer = self.get_serializer(data=request.data)
        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)

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

from mneia_admin_backend.models import Person
from mneia_admin_backend.serializers import PersonSerializer
from mneia_admin_backend.utils import musicbrainz


class PersonViewSet(viewsets.ModelViewSet):
    serializer_class = PersonSerializer
    queryset = Person.objects.all()

    def list(self, request):
        # Overrides:
        # https://github.com/encode/django-rest-framework/blob/e95e91ccf2065cbf474892b73ebd5790e5a4ae14/rest_framework/mixins.py#L37
        queryset = Person.objects.all()
        name = request.query_params.get("name")
        if name is not None:
            queryset = queryset.filter(name__icontains=name)
        limit = request.query_params.get("limit")
        if limit is not None:
            queryset = queryset[: int(limit)]
        serializer = PersonSerializer(queryset, many=True)
        return Response(serializer.data)

    def create(self, request, *args, **kwargs):
        # Overrides:
        # https://github.com/encode/django-rest-framework/blob/e95e91ccf2065cbf474892b73ebd5790e5a4ae14/rest_framework/mixins.py#L12
        try:
            mbid = musicbrainz.get_mbid_from_request(request)
        except KeyError:
            pass
        else:
            mb_obj = musicbrainz.get_person_by_mbid(mbid)
            request.data.update(mb_obj)

        serializer = self.get_serializer(data=request.data)
        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)

# Overrides: # https://github.com/encode/django-rest-framework/blob/e95e91ccf2065cbf474892b73ebd5790e5a4ae14/rest_framework/mixins.py#L37 # Overrides: # https://github.com/encode/django-rest-framework/blob/e95e91ccf2065cbf474892b73ebd5790e5a4ae14/rest_framework/mixins.py#L12