Add maximum length a person can queue in a row.

This commit is contained in:
Jim Driessen
2017-06-01 14:13:25 +02:00
parent 2386c7a68d
commit df1a31efb7
5 changed files with 61 additions and 6 deletions

View File

@ -8,6 +8,7 @@ from django.db.models import Q
from django.http import JsonResponse, HttpResponseForbidden
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_http_methods
from django.conf import settings
from mutagen import File
from marietje.utils import song_to_dict, playlist_song_to_dict, send_to_bertha
@ -211,12 +212,16 @@ def cancel(request):
def request(request):
queue = request.user.queue
song = get_object_or_404(Song, id=request.POST.get('id'), deleted=False)
queue.request(song, request.user)
if queue.request(song, request.user):
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.'
})
@require_http_methods(["POST"])
@api_auth_required

View File

@ -144,3 +144,4 @@ BERTHA_HOST = ('hardcoding.nl', 1819)
MAIL_FROM = 'marietje@hardcoding.nl'
MAX_MINUTES_IN_A_ROW = 20

View File

@ -32,8 +32,14 @@ $(function () {
$(document).on('click', '[data-song-id]', function () {
var songId = $(this).data('song-id');
$.post('/api/request', {id: songId, csrfmiddlewaretoken: csrf_token}, function () {
$.post('/api/request', {id: songId, csrfmiddlewaretoken: csrf_token}, function (result) {
if(result.success) {
refreshQueue();
} else {
// Close request table and show message.
hideRequestTable();
createAlert('danger', result.message);
}
});
$(this).parent().text($(this).text());
return false;
@ -117,7 +123,6 @@ $(function () {
$('#queue-time-header').text(showTimeToPlay ? 'Plays In' : 'Plays At');
Cookies.set('showtimetoplay', showTimeToPlay ? '1' : '0');
});
getSongs();
});
@ -326,3 +331,14 @@ Number.prototype.timestampToHHMMSS = function () {
}
return hours + ':' + minutes + ':' + seconds;
}
function createAlert(type, message) {
alertText = '<div class="alert alert-' + type + ' alert-dismissable">'+
'<button type="button" class="close" ' +
'data-dismiss="alert" aria-hidden="true">' +
'&times;' +
'</button>' +
message +
'</div>';
$('body > div.container').prepend(alertText);
}

View File

@ -1,6 +1,7 @@
import socket, struct, binascii
from django.conf import settings
from queues.models import Queue, Playlist
from django.http import StreamingHttpResponse
def song_to_dict(song, hash=False, user=False):
@ -52,3 +53,20 @@ def get_first_queue():
queue = Queue(name='Queue', playlist=playlist, random_playlist=random_playlist)
queue.save()
return queue
def bertha_stream(hash):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(settings.BERTHA_HOST)
sock.sendall(struct.pack("<B", 2) + binascii.unhexlify(hash))
data = sock.recv(4096)
while data:
yield data
data = sock.recv(4096)
sock.close()
def get_from_bertha(hash):
response = StreamingHttpResponse(bertha_stream(hash))
response['Content-Disposition'] = 'attachment; filename="{}"'.format(hash)
return response

View File

@ -111,12 +111,27 @@ class Queue(models.Model):
return songs[1:]
def request(self, song, user):
order = PlaylistSong.objects.filter(playlist=self.playlist).aggregate(Max('order'))['order__max']
playlist_songs = PlaylistSong.objects.filter(playlist=self.playlist, state=0).order_by('order')
order = 0
seconds_in_a_row = 0
for playlist_song in playlist_songs:
order = playlist_song.order
if playlist_song.user != user:
seconds_in_a_row = 0
else:
seconds_in_a_row += playlist_song.song.duration
if seconds_in_a_row > 0 and seconds_in_a_row + song.duration > settings.MAX_MINUTES_IN_A_ROW * 60\
and not user.is_superuser:
return False
if order is None:
order = 0
order += 1
playlist_song = PlaylistSong(playlist=self.playlist, song=song, user=user, order=order)
playlist_song.save()
return True
def fill_random_queue(self):
song_count = PlaylistSong.objects.filter(playlist_id=self.random_playlist_id, state=0).count()