mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2026-02-04 16:04:43 +01:00
Disallow duplicate queues
This commit is contained in:
@ -224,16 +224,13 @@ def cancel(request):
|
|||||||
def request(request):
|
def request(request):
|
||||||
queue = request.user.queue
|
queue = request.user.queue
|
||||||
song = get_object_or_404(Song, id=request.POST.get('id'), deleted=False)
|
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({
|
err = queue.request(song, request.user)
|
||||||
'success': False,
|
if err != None:
|
||||||
'message': 'You cannot request more than ' + str(settings.MAX_MINUTES_IN_A_ROW) + ' minutes in a row.'
|
return JsonResponse({ 'success': False, 'message': msg })
|
||||||
})
|
|
||||||
|
request_counter.labels(queue=queue.name).inc()
|
||||||
|
return JsonResponse({ 'success': True })
|
||||||
|
|
||||||
|
|
||||||
@require_http_methods(["POST"])
|
@require_http_methods(["POST"])
|
||||||
|
|||||||
@ -112,23 +112,31 @@ class Queue(models.Model):
|
|||||||
return songs[1:]
|
return songs[1:]
|
||||||
|
|
||||||
def request(self, song, user):
|
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:
|
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 settings.LIMIT_ALWAYS:
|
||||||
if seconds_in_a_row > settings.MAX_MINUTES_IN_A_ROW * 60:
|
if seconds_in_a_row > settings.MAX_MINUTES_IN_A_ROW * 60:
|
||||||
return False
|
return msg
|
||||||
else:
|
else:
|
||||||
|
now = timezone.now()
|
||||||
if seconds_in_a_row > 0 and \
|
if seconds_in_a_row > 0 and \
|
||||||
seconds_in_a_row + song.duration > settings.MAX_MINUTES_IN_A_ROW * 60 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]:
|
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 = PlaylistSong(playlist=self.playlist, song=song, user=user)
|
||||||
playlist_song.save()
|
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):
|
def fill_random_queue(self):
|
||||||
song_count = PlaylistSong.objects.filter(playlist_id=self.random_playlist_id, state=0).count()
|
song_count = PlaylistSong.objects.filter(playlist_id=self.random_playlist_id, state=0).count()
|
||||||
|
|||||||
Reference in New Issue
Block a user