Files
muzak/playlist/views.py
T
mark 10703beaca dithered images
still needs to be integrated with celery, but works for now
2024-02-21 17:46:31 +01:00

130 lines
4.8 KiB
Python

from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.contrib.auth.mixins import LoginRequiredMixin
import re
from .models import Track, Artist, Album, Vote, Background, Profile
from .utils import from_json, get_unvoted
from . import spotify
from .tasks import get_banter, get_and_dither_image
spt = spotify.Spotify()
class AuthView(View):
def get(self, request):
if request.user.is_authenticated:
#return TemplateResponse(request, "Nominate.html", {})
return HttpResponseRedirect('/vote')
else:
return TemplateResponse(request, "Login.html", {})
class VoteView(LoginRequiredMixin, View):
def get(self, request, error=None):
track = get_unvoted(request.user)
if not track.album.image:
get_and_dither_image(track.album.image_url, track.album, "image")
return TemplateResponse(request, "Vote.html", {"track": track, "error": error})
def post(self, request):
try:
spotify_id = self.request.POST['spotify_id']
score = int(self.request.POST['vote'])
score = min(max(-2, score), 2)
track = Track.objects.get(pk=spotify_id)
try:
Vote.objects.get(user=request.user, track=track)
except Vote.DoesNotExist:
pass
else:
raise Exception(f"{request.user} already voted for {track}")
Vote.objects.create(track=track, user=request.user, points=score)
print(track)
except Exception as e:
print(e)
return self.get(request, str(e))
else:
return self.get(request)
class NominateView(LoginRequiredMixin, View):
def get(self, request):
return TemplateResponse(request, "Nominate.html", {})
def post(self, request):
spotify_link = self.request.POST['spotify_link']
m = re.match(r"^https:\/\/open.spotify.com\/track\/(\w+)\??", spotify_link)
spotify_id = m.group(1)
try:
track = Track.all_tracks.get(pk=spotify_id)
if track.old:
return TemplateResponse(request, "Nominate.html", {
"warning": "Hey een klassieker.",
"nominated": track
})
return TemplateResponse(request, "Nominate.html", {
"error": "Deze is al genomineerd dit jaar, probeer eens iets anders.",
"nominated": track
})
except Track.DoesNotExist:
json = spt.get_song_info(spotify_id)
# Album artists
album_artists = []
for artist in json["album"]["artists"]:
album_artists.append(from_json(Artist, artist))
del json["album"]["artists"]
# Album
album = 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(from_json(Artist, artist))
del json["artists"]
# Track
track = from_json(Track, json)
for artist in track_artists:
track.artists.add(artist)
track.save()
dup = track.is_duplicate_of()
if dup:
track.delete()
return TemplateResponse(request, "Nominate.html", {
"error": f"Die hadden we al knul.",
"nominated": dup
})
track.album = album
track.save()
get_banter.delay(track.id)
return TemplateResponse(request, "Nominate.html", {"nominated": track})
class SettingsView(LoginRequiredMixin, View):
def get(self, request):
backgrounds = Background.objects.all()
print(backgrounds)
(profile,created) = Profile.objects.get_or_create(user=request.user)
context = {
"backgrounds": backgrounds,
"currentBackground": (profile.background.id if profile.background else 1)
}
return TemplateResponse(request, "Settings.html", context)
def post(self, request):
print(request.POST)
try:
bg = Background.objects.get(pk=request.POST['background'])
print(f"{request.user} wants their background set to {bg}")
(profile,_) = Profile.objects.get_or_create(user=request.user)
print(profile)
profile.background = bg
profile.save()
except:
pass
finally:
return self.get(request)