mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 22:12:22 +01:00
Initial commit.
This commit is contained in:
145
marietje/queues/models.py
Normal file
145
marietje/queues/models.py
Normal 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
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user