Initial commit.

This commit is contained in:
Jim Driessen
2017-01-20 17:45:58 +01:00
commit 47e7b5b59c
85 changed files with 12276 additions and 0 deletions

View File

11
marietje/queues/admin.py Normal file
View File

@ -0,0 +1,11 @@
from django.contrib import admin
from .models import Queue, Playlist, PlaylistSong
admin.site.register(Playlist)
admin.site.register(PlaylistSong)
@admin.register(Queue)
class OrderAdmin(admin.ModelAdmin):
list_display = ('name', 'playlist', 'random_playlist')

5
marietje/queues/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class QueuesConfig(AppConfig):
name = 'queues'

View File

@ -0,0 +1,51 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-01-10 17:14
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('songs', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Playlist',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='PlaylistSong',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('state', models.IntegerField(default=0)),
('order', models.IntegerField()),
('playlist', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='queues.Playlist')),
('song', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='songs.Song')),
('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Queue',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.TextField()),
('started_at', models.DateTimeField(blank=True, null=True)),
('player_token', models.TextField(blank=True, null=True)),
('playlist', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='queues.Playlist')),
('random_playlist', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='random_playlist_set', to='queues.Playlist')),
],
options={
'permissions': (('can_skip', 'Can skip the currently playing song'), ('can_move', 'Can move all songs in the queue'), ('can_cancel', 'Can cancel all songs in the queue')),
},
),
]

View File

145
marietje/queues/models.py Normal file
View File

@ -0,0 +1,145 @@
from django.db import models
from django.db.models import Q, Max
from django.conf import settings
from songs.models import Song
class Playlist(models.Model):
def __str__(self):
return 'Playlist #' + str(self.id)
class PlaylistSong(models.Model):
playlist = models.ForeignKey(
Playlist,
on_delete=models.SET_NULL,
blank=True,
null=True
)
song = models.ForeignKey(
Song,
on_delete=models.SET_NULL,
blank=True,
null=True
)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL,
blank=True,
null=True
)
# 0: Queued.
# 1: Playing.
# 2: Played.
# 3: Cancelled.
state = models.IntegerField(default=0)
order = models.IntegerField()
def move_up(self):
other_song = PlaylistSong.objects.filter(playlist=self.playlist, order__lt=self.order)\
.order_by('-order').first()
self.switch_order(other_song)
def move_down(self):
other_song = PlaylistSong.objects.filter(playlist=self.playlist, order__gt=self.order).order_by('order').first()
self.switch_order(other_song)
def switch_order(self, other_song):
old_order = self.order
self.order = other_song.order
self.save()
other_song.order = old_order
other_song.save()
def __str__(self):
return 'Playlist #' + str(self.id) + ': ' + str(self.song)
class Queue(models.Model):
class Meta:
permissions = (
('can_skip', 'Can skip the currently playing song'),
('can_move', 'Can move all songs in the queue'),
('can_cancel', 'Can cancel all songs in the queue'),
)
name = models.TextField()
playlist = models.ForeignKey(
Playlist,
on_delete=models.SET_NULL,
blank=True,
null=True,
)
random_playlist = models.ForeignKey(
Playlist,
on_delete=models.SET_NULL,
blank=True,
null=True,
related_name='random_playlist_set'
)
started_at = models.DateTimeField(blank=True, null=True)
songs = None
player_token = models.TextField(blank=True, null=True)
def get_songs(self):
if self.songs is None:
self.fill_random_queue()
self.songs = PlaylistSong.objects\
.filter(Q(playlist=self.playlist_id) | Q(playlist_id=self.random_playlist_id),
Q(state=0) | Q(state=1))\
.order_by('-state', 'playlist_id', 'order')\
.prefetch_related('song', 'user')
print(self.songs)
return self.songs
def current_song(self):
print('Hoi doei')
songs = self.get_songs()
if len(songs) < 1:
return None
song = songs[0]
if song.state != 1:
song.state = 1
song.save()
return song
def queue(self):
songs = self.get_songs()
if len(songs) < 2:
return []
return songs[1:]
def request(self, song, user):
order = PlaylistSong.objects.filter(playlist=self.playlist).aggregate(Max('order'))['order__max']
if order is None:
order = 0
order += 1
playlist_song = PlaylistSong(playlist=self.playlist, song=song, user=user, order=order)
playlist_song.save()
def fill_random_queue(self):
song_count = PlaylistSong.objects.filter(playlist_id=self.random_playlist_id, state=0).count()
while song_count < 5:
order = PlaylistSong.objects.filter(playlist_id=self.random_playlist_id)\
.aggregate(Max('order'))['order__max']
if order is None:
order = 0
order += 1
song = Song.objects.filter(deleted=False).order_by('?').first()
if song is None:
return
playlist_song = PlaylistSong(playlist=self.random_playlist,
song=song,
user=None, order=order)
playlist_song.save()
song_count += 1
def __str__(self):
return self.name

View File

@ -0,0 +1,97 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Queue{% endblock %}
{% block content %}
<h1 class="h1">Request</h1>
<button id="request-button" class="btn btn-primary">Request</button>
<br><br>
<div id="request-container" class="hidden">
<div class="table-responsive">
<table id="request-table" class="table table-striped">
<thead>
<tr>
<th>Artist</th>
<th>Title</th>
<th>Uploader</th>
</tr>
<tr>
<th><input id="search-artist" class="search-input" type="text"></th>
<th><input id="search-title" class="search-input" type="text"></th>
<th><input id="search-uploader" class="search-input" type="text"></th>
</tr>
</thead>
<tfoot>
<tr>
<th colspan="3" class="ts-pager form-horizontal">
<button type="button" class="btn first"><i class="icon-step-backward glyphicon glyphicon-step-backward"></i></button>
<button type="button" class="btn prev"><i class="icon-arrow-left glyphicon glyphicon-backward"></i></button>
<button type="button" class="btn next"><i class="icon-arrow-right glyphicon glyphicon-forward"></i></button>
<button type="button" class="btn last"><i class="icon-step-forward glyphicon glyphicon-step-forward"></i></button>
<select class="pagesize input-mini" title="Select page size">
<option selected value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
<select class="pagenum input-mini" title="Select page number"></select>
</th>
</tr>
</tfoot>
<tbody>
</tbody>
</table>
</div>
</div>
<div id="queue-container">
<h1 class="h1">Now Playing</h1>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th class="col-md-4">Artist</th>
<th class="col-md-4">Title</th>
<th class="col-md-2">Requested By</th>
<th class="col-md-1">Time Left</th>
<th class="col-md-1">Skip</th>
</tr>
</thead>
<tbody>
<tr class="currentsong">
<td class="artist"></td>
<td class="title"></td>
<td class="requestedby"></td>
<td class="timeleft"></td>
<td><a id="skip" class="glyphicon glyphicon-fast-forward" href="#"></a></td>
</tr>
</tbody>
</table>
</div>
<h1 class="h1">Queue</h1>
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th class="col-md-4">Artist</th>
<th class="col-md-4">Title</th>
<th class="col-md-2">Requested By</th>
<th class="col-md-1">Plays At</th>
<th class="col-md-1">Control</th>
</tr>
</thead>
<tbody class="queuebody">
</tbody>
</table>
</div>
</div>
<script type="text/javascript" src="{% static 'js/queue.js' %}"></script>
<script type="text/javascript">
var csrf_token = "{{ csrf_token }}";
var canSkip = {{ perms.queues.can_skip|yesno:"1,0" }};
var canCancel = {{ perms.queues.can_cancel|yesno:"1,0" }};
var canMoveSongs = {{ perms.queues.can_move|yesno:"1,0" }};
</script>
{% endblock %}

3
marietje/queues/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

8
marietje/queues/urls.py Normal file
View File

@ -0,0 +1,8 @@
from django.conf.urls import url
from . import views
app_name = 'queue'
urlpatterns = [
]

6
marietje/queues/views.py Normal file
View File

@ -0,0 +1,6 @@
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
@login_required
def index(request):
return render(request, 'queues/queue.html')