<commit_before>from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.filters import BaseFilterBackend
from rest_framework.decorators import list_route, detail_route
from rest_framework.response import Response

from songs.models import Song

from .models import Scrobble
from .serializers import ScrobbleSerializer

def song_exists(title):
    does_exist = Song.objects.filter(title=title)
    if len(does_exist) != 0:
        return True
    else:
        return False


class ScrobbleView(viewsets.ModelViewSet):
    queryset = Scrobble.objects.all()
    serializer_class = ScrobbleSerializer

    def perform_create(self, serializer):
        serializer.save(member=self.request.user)

    @detail_route(methods=['GET'])
    def by_artist(self, request, pk=None):
        if pk is not None:
            queryset = Scrobble.objects.filter(artist=pk)
            serializer = ScrobbleSerializer(instance=queryset, many=True)
            return Response(serializer.data)

    # TODO: Untested until initial frontend routes
    @detail_route(methods=['POST'])
    def write(self, request, pk=None):
        """pseudo get_or_create function<commit_msg>Find all scrobbles based on either artist, album, or song.<commit_after>from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.filters import BaseFilterBackend
from rest_framework.decorators import list_route, detail_route
from rest_framework.response import Response

from songs.models import Song

from .models import Scrobble
from .serializers import ScrobbleSerializer

def song_exists(title):
    does_exist = Song.objects.filter(title=title)
    if len(does_exist) != 0:
        return True
    else:
        return False


class ScrobbleView(viewsets.ModelViewSet):
    queryset = Scrobble.objects.all()
    serializer_class = ScrobbleSerializer

    def perform_create(self, serializer):
        serializer.save(member=self.request.user)

    # In a perfect world, these 3 functions would be
    # in a BaseFilterBackend. But, the world is an imperfect place..
    # Screws fall out all the time.
    @detail_route(methods=['GET'])
    def by_artist(self, request, pk=None):
        """List all scrobbles from one artist(pk)"""
        if pk is not None:
            queryset = Scrobble.objects.filter(artist=pk)
            serializer = ScrobbleSerializer(instance=queryset, many=True)
            return Response(serializer.data)

    @detail_route(methods=['GET'])
    def by_album(self, request, pk=None):
        """Lists all scrobbles from one album(pk)"""
        if pk is not None:
            queryset = Scrobble.objects.filter(song__album=pk)
            serializer = ScrobbleSerializer(instance=queryset, many=True)
            return Response(serializer.data)

    @detail_route(methods=['GET'])
    def by_song(self, request, pk=None):
        """Lists all scrobbles from one song(pk)"""
        if pk is not None:
            queryset = Scrobble.objects.filter(song=pk)
            serializer = ScrobbleSerializer(instance=queryset, many=True)
            return Response(serializer.data)

    # TODO: Untested until initial frontend routes
    @detail_route(methods=['POST'])
    def write(self, request, pk=None):
        """pseudo get_or_create function
{'fraction_non_alphanumeric': 0.08658795034816834, 'fraction_numerical': 0.0009082652134423251, 'mean_word_length': 3.5698478561549103, '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': '3903990', 'n_tokens_mistral': 966, 'n_tokens_neox': 945, 'n_words': 257}