Toggle Light / Dark / Auto color theme
Toggle table of contents sidebar
Source code for nkdsu.apps.vote.managers
from __future__ import annotations
from typing import Self , TYPE_CHECKING
from django.conf import settings
from django.db import models
from .utils import split_query_into_keywords
if TYPE_CHECKING :
from .models import Show , Track
from .models import Note # noqa - it's used for the NoteQuerySet annotation
[docs]
class NoteQuerySet ( models . QuerySet [ "Note" ]):
[docs]
def for_show_or_none ( self , show : Show ) -> Self :
return self . filter ( models . Q ( show = show ) | models . Q ( show = None ))
[docs]
class TrackQuerySet ( models . QuerySet [ "Track" ]):
[docs]
def _everything ( self , show_secret_tracks : bool = False ) -> Self :
if show_secret_tracks :
return self . all ()
else :
return self . public ()
[docs]
def for_decade ( self , start_year : int ) -> Self :
return self . filter ( year__gte = start_year , year__lt = start_year + 10 )
[docs]
def public ( self ) -> Self :
return self . filter ( hidden = False , inudesu = False , archived = False )
[docs]
def by_artist ( self , artist : str , show_secret_tracks : bool = False ) -> list [ Track ]:
"""
Filters with Python, so does not return a queryset and is not lazy.
"""
base_qs = self . _everything ( show_secret_tracks )
qs = base_qs . filter ( id3_artist__contains = artist ) . order_by ( 'id3_title' )
return [ t for t in qs if artist in t . artist_names ()]
[docs]
def by_composer (
self , composer : str , show_secret_tracks : bool = False
) -> list [ Track ]:
base_qs = self . _everything ( show_secret_tracks )
qs = base_qs . filter ( composer__contains = composer ) . order_by ( 'id3_title' )
return [ t for t in qs if composer in t . composer_names ()]
[docs]
def by_anime ( self , anime : str , show_secret_tracks : bool = False ) -> list [ Track ]:
"""
Behaves similarly to by_artist.
"""
base_qs = self . _everything ( show_secret_tracks )
qs = base_qs . filter ( id3_title__contains = anime ) . order_by ( 'id3_title' )
return [ t for t in qs if t . has_anime ( anime )]
[docs]
def search ( self , query : str , show_secret_tracks : bool = False ) -> Self :
keywords = split_query_into_keywords ( query )
if len ( keywords ) == 0 :
return self . none ()
qs = self . _everything ( show_secret_tracks )
for keyword in keywords :
if (
settings . DATABASES [ 'default' ][ 'ENGINE' ]
== 'django.db.backends.postgresql'
):
title_q = models . Q ( id3_title__unaccent__icontains = keyword )
artist_q = models . Q ( id3_artist__unaccent__icontains = keyword )
composer_q = models . Q ( composer__unaccent__icontains = keyword )
else :
title_q = models . Q ( id3_title__icontains = keyword )
artist_q = models . Q ( id3_artist__icontains = keyword )
composer_q = models . Q ( composer__icontains = keyword )
qs = qs . exclude ( ~ title_q & ~ artist_q & ~ composer_q )
return qs