from django.db import models from django.contrib.auth.models import User 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) nominated_by = models.ForeignKey(User, blank=True, null=True, default=None, on_delete=models.SET_NULL) name = models.CharField(max_length=255) artists = models.ManyToManyField(Artist) album = models.ForeignKey(Album, blank=True, null=True, on_delete=models.SET_NULL) explicit = models.BooleanField(default=False) duration_ms = models.PositiveIntegerField(blank=True, null=True) popularity = models.PositiveIntegerField(blank=True, null=True) banter = models.TextField(null=True, blank=True) banter_done = models.BooleanField(default=False) old = models.BooleanField(default=False) from_playlist = models.CharField(max_length=255, null=True, blank=True, default=None) objects = TrackManager() all_tracks = models.Manager()