From df1a31efb7e47f2cc18fbfc35637dbe787c46b30 Mon Sep 17 00:00:00 2001 From: Jim Driessen Date: Thu, 1 Jun 2017 14:13:25 +0200 Subject: [PATCH] Add maximum length a person can queue in a row. --- marietje/api/views.py | 9 +++++++-- marietje/marietje/settings.py | 1 + marietje/marietje/static/js/queue.js | 22 +++++++++++++++++++--- marietje/marietje/utils.py | 18 ++++++++++++++++++ marietje/queues/models.py | 17 ++++++++++++++++- 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/marietje/api/views.py b/marietje/api/views.py index 39c9c1a..086f935 100644 --- a/marietje/api/views.py +++ b/marietje/api/views.py @@ -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,10 +212,14 @@ 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': True + 'success': False, + 'message': 'You cannot request more than ' + str(settings.MAX_MINUTES_IN_A_ROW) + ' minutes in a row.' }) diff --git a/marietje/marietje/settings.py b/marietje/marietje/settings.py index 51f05c9..1642d39 100644 --- a/marietje/marietje/settings.py +++ b/marietje/marietje/settings.py @@ -144,3 +144,4 @@ BERTHA_HOST = ('hardcoding.nl', 1819) MAIL_FROM = 'marietje@hardcoding.nl' +MAX_MINUTES_IN_A_ROW = 20 diff --git a/marietje/marietje/static/js/queue.js b/marietje/marietje/static/js/queue.js index 37ad265..85bf16d 100644 --- a/marietje/marietje/static/js/queue.js +++ b/marietje/marietje/static/js/queue.js @@ -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 () { - refreshQueue(); + $.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 = '
'+ + '' + + message + + '
'; + $('body > div.container').prepend(alertText); +} diff --git a/marietje/marietje/utils.py b/marietje/marietje/utils.py index a0dc2a7..2c78de5 100644 --- a/marietje/marietje/utils.py +++ b/marietje/marietje/utils.py @@ -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(" 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()