track nomination
moet nog wel een hoop oude code weg, maar dat is een probleem voor later
This commit is contained in:
@@ -55,14 +55,14 @@ class Profile(models.Model):
|
||||
"""Is this user allowed to nominate at this point in time?"""
|
||||
end_date = os.getenv('DATE_NOM_END')
|
||||
if self.user.is_superuser:
|
||||
return (True,)
|
||||
return (True, None)
|
||||
if end_date:
|
||||
end_date = datetime.fromisoformat(end_date)
|
||||
if end_date <= datetime.now(tz=UTC):
|
||||
return (False, "over")
|
||||
if self.quota < 1:
|
||||
return (False, "quota")
|
||||
return (True,)
|
||||
return (True, None)
|
||||
|
||||
@property
|
||||
def can_vote(self):
|
||||
|
||||
@@ -4,12 +4,92 @@ from random import choice
|
||||
from .album import Album
|
||||
from .artist import Artist
|
||||
from .vote import Vote
|
||||
from playlist.spotify import spt
|
||||
#from playlist.utils import track_from_json
|
||||
import re
|
||||
|
||||
class TrackManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return super().get_queryset().filter(old=False)
|
||||
|
||||
def from_json(self, cls, json):
|
||||
try:
|
||||
return cls.objects.get(pk=json["id"])
|
||||
except cls.DoesNotExist:
|
||||
keys = [f.name for f in cls._meta.get_fields()]
|
||||
subset = {key:json[key] for key in set(keys) & set(json.keys())}
|
||||
return cls.objects.create(**subset)
|
||||
|
||||
def track_from_json(self, json):
|
||||
"""Creates the track, album and artist from spotify response."""
|
||||
# Album artists
|
||||
album_artists = []
|
||||
for artist in json["album"]["artists"]:
|
||||
album_artists.append(self.from_json(Artist, artist))
|
||||
del json["album"]["artists"]
|
||||
|
||||
# Album
|
||||
album = self.from_json(Album, json["album"])
|
||||
album.image_url = json["album"]["images"][0]["url"]
|
||||
album.save()
|
||||
del json["album"]
|
||||
for artist in album_artists:
|
||||
album.artists.add(artist)
|
||||
|
||||
# Track artists
|
||||
track_artists = []
|
||||
for artist in json["artists"]:
|
||||
track_artists.append(self.from_json(Artist, artist))
|
||||
del json["artists"]
|
||||
|
||||
# Track
|
||||
track = self.from_json(Track, json)
|
||||
for artist in track_artists:
|
||||
track.artists.add(artist)
|
||||
track.album = album
|
||||
track.save()
|
||||
return track
|
||||
|
||||
def create_from_spotify(self, spotify_link, profile):
|
||||
if re.match(r"^https:\/\/spotify.link.*", spotify_link):
|
||||
spotify_link = spt.unshort(spotify_link)
|
||||
|
||||
m = re.match(r"^https:\/\/open.spotify.com\/track\/([a-zA-Z0-9]+)\??", spotify_link)
|
||||
if m is None:
|
||||
return None, Exception("Invalid Spotify track URL")
|
||||
|
||||
spotify_id = m.group(1)
|
||||
|
||||
try:
|
||||
existing_track = self.model.all_tracks.get(pk=spotify_id)
|
||||
if existing_track.old:
|
||||
existing_track.old = False
|
||||
existing_track.user = profile.user
|
||||
existing_track.save()
|
||||
return (existing_track, "Updated old track")
|
||||
else:
|
||||
return (None, "Track already exists")
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
json = spt.get_song_info(spotify_id)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return (None, "Error fetching track")
|
||||
#return TemplateResponse(request, "Nominate.html", {"message_type": "error", "message": "Fout bij het ophalen van het liedje."})
|
||||
|
||||
track = self.track_from_json(json)
|
||||
track.nominated_by = profile.user
|
||||
track.save()
|
||||
dup = track.is_duplicate_of()
|
||||
if dup:
|
||||
track.delete()
|
||||
return (None, "Track already exists")
|
||||
#get_banter.delay(track.id)
|
||||
profile.quota -= 1
|
||||
profile.save()
|
||||
return (track, None)
|
||||
|
||||
class Track(models.Model):
|
||||
def __str__(self):
|
||||
s = f"'{self.name}'"
|
||||
|
||||
@@ -2,8 +2,10 @@ from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from playlist.models import Track
|
||||
from playlist.models import Track, Profile
|
||||
from playlist.serializers import TrackSerializer
|
||||
from playlist.spotify import spt
|
||||
from playlist.tasks import get_banter, get_and_dither_image
|
||||
|
||||
class TrackListView(APIView):
|
||||
authentication_classes = [SessionAuthentication]
|
||||
@@ -13,6 +15,22 @@ class TrackListView(APIView):
|
||||
serializer = TrackSerializer(tracks, many=True, context={'request': request})
|
||||
return Response(serializer.data)
|
||||
|
||||
def post(self, request):
|
||||
(profile, _) = Profile.objects.get_or_create(user=request.user)
|
||||
(can_nom, reason) = profile.can_nominate
|
||||
if not can_nom:
|
||||
return Response({'error': reason}, status=403)
|
||||
(track, message) = Track.objects.create_from_spotify(request.data['spotify_link'], profile)
|
||||
serializer = TrackSerializer(track, context={'request': request})
|
||||
return Response(serializer.data)
|
||||
|
||||
def delete(self, request):
|
||||
if request.user.is_superuser:
|
||||
Track.objects.all().delete()
|
||||
return Response(status=204)
|
||||
else:
|
||||
return Response({'error': 'Only superusers can delete all tracks'}, status=403)
|
||||
|
||||
class TrackDetailView(APIView):
|
||||
authentication_classes = [SessionAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
Reference in New Issue
Block a user