8dca25f861
En een begin aan een playlist maker. Doet op het moment niet veel.
62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
from django.db import models
|
|
from random import choice
|
|
from .album import Album
|
|
from .artist import Artist
|
|
from .vote import Vote
|
|
import re
|
|
|
|
class TrackManager(models.Manager):
|
|
def get_queryset(self):
|
|
return super().get_queryset().filter(old=False)
|
|
|
|
class Track(models.Model):
|
|
def __str__(self):
|
|
s = f"'{self.name}'"
|
|
if self.artists.count() > 0:
|
|
artists = list(map(lambda a:a.name, self.artists.all()))
|
|
s += " door "
|
|
s += " & ".join(artists)
|
|
return s
|
|
@property
|
|
def artist(self):
|
|
if self.artists.count() > 0:
|
|
artists = list(map(lambda a:a.name, self.artists.all()))
|
|
return " & ".join(artists)
|
|
|
|
@property
|
|
def name_sanitized(self):
|
|
return re.match("[^\-\(]*\w", self.name)[0]
|
|
|
|
@property
|
|
def link(self):
|
|
return f'https://open.spotify.com/track/{self.id}'
|
|
|
|
@property
|
|
def score(self):
|
|
return Vote.objects.filter(track=self.id).aggregate(rating=models.Avg("points"))["rating"]
|
|
|
|
def is_duplicate_of(self):
|
|
tracks = Track.objects.exclude(id=self.id).filter(name__startswith=self.name_sanitized)
|
|
artists = [artist.name for artist in self.artists.all()]
|
|
for artist in artists:
|
|
tracks = tracks.filter(artists__name__exact=artist)
|
|
return tracks.first()
|
|
|
|
|
|
id = models.CharField(primary_key=True, max_length=128)
|
|
name = models.CharField(max_length=255)
|
|
artists = models.ManyToManyField(Artist)
|
|
album = models.ForeignKey(Album, null=True, on_delete=models.SET_NULL)
|
|
|
|
explicit = models.BooleanField(default=False)
|
|
duration_ms = models.PositiveIntegerField(null=True)
|
|
popularity = models.PositiveIntegerField(null=True)
|
|
|
|
banter = models.TextField(blank=True)
|
|
banter_done = models.BooleanField(default=False)
|
|
|
|
old = models.BooleanField(default=False)
|
|
|
|
objects = TrackManager()
|
|
all_tracks = models.Manager()
|