2017-06-23 21:00:42 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2018-06-10 08:55:16 +00:00
|
|
|
from django.contrib.postgres.fields import JSONField
|
2018-04-11 21:13:33 +00:00
|
|
|
from django.core.serializers.json import DjangoJSONEncoder
|
2018-06-10 08:55:16 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
from funkwhale_api.music.models import Track
|
|
|
|
|
2018-01-07 21:12:46 +00:00
|
|
|
from . import filters
|
|
|
|
|
|
|
|
|
|
|
|
class Radio(models.Model):
|
|
|
|
CONFIG_VERSION = 0
|
|
|
|
user = models.ForeignKey(
|
2018-06-09 13:36:16 +00:00
|
|
|
"users.User",
|
|
|
|
related_name="radios",
|
2018-01-07 21:12:46 +00:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
2018-06-09 13:36:16 +00:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2018-01-07 21:12:46 +00:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
description = models.TextField(blank=True)
|
|
|
|
creation_date = models.DateTimeField(default=timezone.now)
|
|
|
|
is_public = models.BooleanField(default=False)
|
|
|
|
version = models.PositiveIntegerField(default=0)
|
2018-04-11 21:13:33 +00:00
|
|
|
config = JSONField(encoder=DjangoJSONEncoder)
|
2018-01-07 21:12:46 +00:00
|
|
|
|
|
|
|
def get_candidates(self):
|
|
|
|
return filters.run(self.config)
|
|
|
|
|
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
class RadioSession(models.Model):
|
2017-12-15 23:36:06 +00:00
|
|
|
user = models.ForeignKey(
|
2018-06-09 13:36:16 +00:00
|
|
|
"users.User",
|
|
|
|
related_name="radio_sessions",
|
2017-12-15 23:36:06 +00:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
2018-06-09 13:36:16 +00:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
session_key = models.CharField(max_length=100, null=True, blank=True)
|
|
|
|
radio_type = models.CharField(max_length=50)
|
2018-01-07 21:12:46 +00:00
|
|
|
custom_radio = models.ForeignKey(
|
2018-06-09 13:36:16 +00:00
|
|
|
Radio, related_name="sessions", null=True, blank=True, on_delete=models.CASCADE
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
creation_date = models.DateTimeField(default=timezone.now)
|
2017-12-15 23:36:06 +00:00
|
|
|
related_object_content_type = models.ForeignKey(
|
2018-06-09 13:36:16 +00:00
|
|
|
ContentType, blank=True, null=True, on_delete=models.CASCADE
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
related_object_id = models.PositiveIntegerField(blank=True, null=True)
|
2018-06-09 13:36:16 +00:00
|
|
|
related_object = GenericForeignKey(
|
|
|
|
"related_object_content_type", "related_object_id"
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
def save(self, **kwargs):
|
|
|
|
self.radio.clean(self)
|
|
|
|
super().save(**kwargs)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def next_position(self):
|
|
|
|
next_position = 1
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
last_session_track = self.session_tracks.all().order_by("-position").first()
|
2017-06-23 21:00:42 +00:00
|
|
|
if last_session_track:
|
|
|
|
next_position = last_session_track.position + 1
|
|
|
|
|
|
|
|
return next_position
|
|
|
|
|
|
|
|
def add(self, track):
|
2018-06-09 13:36:16 +00:00
|
|
|
new_session_track = RadioSessionTrack.objects.create(
|
|
|
|
track=track, session=self, position=self.next_position
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
return new_session_track
|
|
|
|
|
|
|
|
@property
|
|
|
|
def radio(self):
|
|
|
|
from .registries import registry
|
2018-06-09 13:36:16 +00:00
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
return registry[self.radio_type](session=self)
|
|
|
|
|
2018-01-07 21:12:46 +00:00
|
|
|
|
2017-06-23 21:00:42 +00:00
|
|
|
class RadioSessionTrack(models.Model):
|
2017-12-15 23:36:06 +00:00
|
|
|
session = models.ForeignKey(
|
2018-06-09 13:36:16 +00:00
|
|
|
RadioSession, related_name="session_tracks", on_delete=models.CASCADE
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
position = models.IntegerField(default=1)
|
2017-12-15 23:36:06 +00:00
|
|
|
track = models.ForeignKey(
|
2018-06-09 13:36:16 +00:00
|
|
|
Track, related_name="radio_session_tracks", on_delete=models.CASCADE
|
|
|
|
)
|
2017-06-23 21:00:42 +00:00
|
|
|
|
|
|
|
class Meta:
|
2018-06-09 13:36:16 +00:00
|
|
|
ordering = ("session", "position")
|
|
|
|
unique_together = ("session", "position")
|