2018-03-20 18:56:42 +00:00
|
|
|
from django.conf import settings
|
2017-06-23 21:00:42 +00:00
|
|
|
from django.db import models
|
2018-03-19 13:11:09 +00:00
|
|
|
from django.db import transaction
|
2017-06-23 21:00:42 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2018-03-20 18:56:42 +00:00
|
|
|
from rest_framework import exceptions
|
|
|
|
|
2018-03-16 22:30:37 +00:00
|
|
|
from funkwhale_api.common import fields
|
2018-04-28 03:30:23 +00:00
|
|
|
from funkwhale_api.common import preferences
|
2018-03-16 22:30:37 +00:00
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-05-08 21:06:29 +00:00
|
|
|
class PlaylistQuerySet(models.QuerySet):
|
|
|
|
def with_tracks_count(self):
|
|
|
|
return self.annotate(
|
|
|
|
_tracks_count=models.Count('playlist_tracks'))
|
|
|
|
|
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
class Playlist(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
2017-12-15 23:36:06 +00:00
|
|
|
user = models.ForeignKey(
|
|
|
|
'users.User', related_name="playlists", on_delete=models.CASCADE)
|
2017-06-23 21:00:42 +00:00
|
|
|
creation_date = models.DateTimeField(default=timezone.now)
|
2018-03-19 18:07:45 +00:00
|
|
|
modification_date = models.DateTimeField(
|
|
|
|
auto_now=True)
|
2018-03-16 22:30:37 +00:00
|
|
|
privacy_level = fields.get_privacy_field()
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-05-08 21:06:29 +00:00
|
|
|
objects = PlaylistQuerySet.as_manager()
|
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-03-19 13:11:09 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def insert(self, plt, index=None):
|
|
|
|
"""
|
|
|
|
Given a PlaylistTrack, insert it at the correct index in the playlist,
|
|
|
|
and update other tracks index if necessary.
|
|
|
|
"""
|
|
|
|
old_index = plt.index
|
|
|
|
move = old_index is not None
|
|
|
|
if index is not None and index == old_index:
|
|
|
|
# moving at same position, just skip
|
|
|
|
return index
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-03-19 13:11:09 +00:00
|
|
|
existing = self.playlist_tracks.select_for_update()
|
|
|
|
if move:
|
|
|
|
existing = existing.exclude(pk=plt.pk)
|
|
|
|
total = existing.filter(index__isnull=False).count()
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-03-19 13:11:09 +00:00
|
|
|
if index is None:
|
|
|
|
# we simply increment the last track index by 1
|
|
|
|
index = total
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-03-19 13:11:09 +00:00
|
|
|
if index > total:
|
2018-03-20 18:56:42 +00:00
|
|
|
raise exceptions.ValidationError('Index is not continuous')
|
2018-03-19 13:11:09 +00:00
|
|
|
|
|
|
|
if index < 0:
|
2018-03-20 18:56:42 +00:00
|
|
|
raise exceptions.ValidationError('Index must be zero or positive')
|
2018-03-19 13:11:09 +00:00
|
|
|
|
|
|
|
if move:
|
|
|
|
# we remove the index temporarily, to avoid integrity errors
|
|
|
|
plt.index = None
|
|
|
|
plt.save(update_fields=['index'])
|
|
|
|
if index > old_index:
|
|
|
|
# new index is higher than current, we decrement previous tracks
|
|
|
|
to_update = existing.filter(
|
|
|
|
index__gt=old_index, index__lte=index)
|
|
|
|
to_update.update(index=models.F('index') - 1)
|
|
|
|
if index < old_index:
|
|
|
|
# new index is lower than current, we increment next tracks
|
2018-03-20 18:56:42 +00:00
|
|
|
to_update = existing.filter(index__lt=old_index, index__gte=index)
|
2018-03-19 13:11:09 +00:00
|
|
|
to_update.update(index=models.F('index') + 1)
|
|
|
|
else:
|
|
|
|
to_update = existing.filter(index__gte=index)
|
|
|
|
to_update.update(index=models.F('index') + 1)
|
|
|
|
|
|
|
|
plt.index = index
|
|
|
|
plt.save(update_fields=['index'])
|
2018-03-19 18:07:45 +00:00
|
|
|
self.save(update_fields=['modification_date'])
|
2018-03-19 13:11:09 +00:00
|
|
|
return index
|
|
|
|
|
2018-03-19 18:07:45 +00:00
|
|
|
@transaction.atomic
|
2018-03-19 14:28:33 +00:00
|
|
|
def remove(self, index):
|
|
|
|
existing = self.playlist_tracks.select_for_update()
|
2018-03-19 18:07:45 +00:00
|
|
|
self.save(update_fields=['modification_date'])
|
2018-03-19 14:28:33 +00:00
|
|
|
to_update = existing.filter(index__gt=index)
|
|
|
|
return to_update.update(index=models.F('index') - 1)
|
|
|
|
|
2018-03-20 18:56:42 +00:00
|
|
|
@transaction.atomic
|
|
|
|
def insert_many(self, tracks):
|
|
|
|
existing = self.playlist_tracks.select_for_update()
|
|
|
|
now = timezone.now()
|
|
|
|
total = existing.filter(index__isnull=False).count()
|
2018-04-28 03:30:23 +00:00
|
|
|
max_tracks = preferences.get('playlists__max_tracks')
|
|
|
|
if existing.count() + len(tracks) > max_tracks:
|
2018-03-20 18:56:42 +00:00
|
|
|
raise exceptions.ValidationError(
|
|
|
|
'Playlist would reach the maximum of {} tracks'.format(
|
2018-04-28 03:30:23 +00:00
|
|
|
max_tracks))
|
2018-03-20 18:56:42 +00:00
|
|
|
self.save(update_fields=['modification_date'])
|
|
|
|
start = total
|
|
|
|
plts = [
|
|
|
|
PlaylistTrack(
|
|
|
|
creation_date=now, playlist=self, track=track, index=start+i)
|
|
|
|
for i, track in enumerate(tracks)
|
|
|
|
]
|
|
|
|
return PlaylistTrack.objects.bulk_create(plts)
|
|
|
|
|
2018-03-20 21:44:28 +00:00
|
|
|
class PlaylistTrackQuerySet(models.QuerySet):
|
|
|
|
def for_nested_serialization(self):
|
|
|
|
return (self.select_related()
|
|
|
|
.select_related('track__album__artist')
|
|
|
|
.prefetch_related(
|
|
|
|
'track__tags',
|
|
|
|
'track__files',
|
|
|
|
'track__artist__albums__tracks__tags'))
|
|
|
|
|
2018-03-19 13:11:09 +00:00
|
|
|
|
|
|
|
class PlaylistTrack(models.Model):
|
2017-12-15 23:36:06 +00:00
|
|
|
track = models.ForeignKey(
|
|
|
|
'music.Track',
|
|
|
|
related_name='playlist_tracks',
|
|
|
|
on_delete=models.CASCADE)
|
2018-03-19 14:28:33 +00:00
|
|
|
index = models.PositiveIntegerField(null=True, blank=True)
|
2017-12-15 23:36:06 +00:00
|
|
|
playlist = models.ForeignKey(
|
|
|
|
Playlist, related_name='playlist_tracks', on_delete=models.CASCADE)
|
2018-03-19 13:11:09 +00:00
|
|
|
creation_date = models.DateTimeField(default=timezone.now)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
2018-03-20 21:44:28 +00:00
|
|
|
objects = PlaylistTrackQuerySet.as_manager()
|
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
class Meta:
|
2018-03-19 13:06:51 +00:00
|
|
|
ordering = ('-playlist', 'index')
|
|
|
|
unique_together = ('playlist', 'index')
|
2018-03-19 14:28:33 +00:00
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
playlist = self.playlist
|
|
|
|
index = self.index
|
|
|
|
update_indexes = kwargs.pop('update_indexes', False)
|
|
|
|
r = super().delete(*args, **kwargs)
|
|
|
|
if index is not None and update_indexes:
|
|
|
|
playlist.remove(index)
|
|
|
|
return r
|