Disallow duplicate queues

This commit is contained in:
Daan Sprenkels
2018-11-23 15:17:29 +01:00
parent 6b8fdbab2e
commit a692106c5b
2 changed files with 21 additions and 16 deletions

View File

@ -224,16 +224,13 @@ def cancel(request):
def request(request):
queue = request.user.queue
song = get_object_or_404(Song, id=request.POST.get('id'), deleted=False)
if queue.request(song, request.user):
request_counter.labels(queue=queue.name).inc()
return JsonResponse({
'success': True
})
return JsonResponse({
'success': False,
'message': 'You cannot request more than ' + str(settings.MAX_MINUTES_IN_A_ROW) + ' minutes in a row.'
})
err = queue.request(song, request.user)
if err != None:
return JsonResponse({ 'success': False, 'message': msg })
request_counter.labels(queue=queue.name).inc()
return JsonResponse({ 'success': True })
@require_http_methods(["POST"])

View File

@ -112,23 +112,31 @@ class Queue(models.Model):
return songs[1:]
def request(self, song, user):
playlist_songs = PlaylistSong.objects.filter(playlist=self.playlist, state=0).order_by('id')
seconds_in_a_row = sum(ps.song.duration for ps in playlist_songs if ps.user == user)
now = timezone.now()
if not user.is_superuser:
playlist_songs = PlaylistSong.objects.filter(playlist=self.playlist, state=0).order_by('id')
seconds_in_a_row = sum(ps.song.duration for ps in playlist_songs if ps.user == user)
msg = 'You cannot request more than ' + str(settings.MAX_MINUTES_IN_A_ROW) + ' minutes in a row.'
if settings.LIMIT_ALWAYS:
if seconds_in_a_row > settings.MAX_MINUTES_IN_A_ROW * 60:
return False
return msg
else:
now = timezone.now()
if seconds_in_a_row > 0 and \
seconds_in_a_row + song.duration > settings.MAX_MINUTES_IN_A_ROW * 60 and \
settings.LIMIT_HOURS[0] <= now.hour < settings.LIMIT_HOURS[1]:
return False
return msg
if {ps for ps in playlist_songs if ps.song == song}:
return 'This song is already in the queue.'
playlist_song = PlaylistSong(playlist=self.playlist, song=song, user=user)
playlist_song.save()
return True
# If the song was auto-queue'd, then remove it from the auto-queue
autolist_songs = PLaylistSong.objects.filter(playlist=self.random_playlist, state=0, song=song).delete()
return None
def fill_random_queue(self):
song_count = PlaylistSong.objects.filter(playlist_id=self.random_playlist_id, state=0).count()