mirror of
https://github.com/mediacms-io/mediacms.git
synced 2025-11-21 22:07:59 -05:00
just your typical stub
This commit is contained in:
@@ -85,6 +85,15 @@ class Media(models.Model):
|
||||
|
||||
likes = models.IntegerField(db_index=True, default=1)
|
||||
|
||||
linked_playlist = models.ForeignKey(
|
||||
"Playlist",
|
||||
on_delete=models.CASCADE,
|
||||
blank=True,
|
||||
null=True,
|
||||
related_name="media_representation",
|
||||
help_text="If set, this Media represents a Playlist in listings",
|
||||
)
|
||||
|
||||
listable = models.BooleanField(default=False, help_text="Whether it will appear on listings")
|
||||
|
||||
md5sum = models.CharField(max_length=50, blank=True, null=True, help_text="Not exposed, used internally")
|
||||
@@ -93,6 +102,8 @@ class Media(models.Model):
|
||||
"media file",
|
||||
upload_to=original_media_file_path,
|
||||
max_length=500,
|
||||
blank=True,
|
||||
null=True,
|
||||
help_text="media file",
|
||||
)
|
||||
|
||||
@@ -240,7 +251,10 @@ class Media(models.Model):
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.title:
|
||||
self.title = self.media_file.path.split("/")[-1]
|
||||
if self.media_file:
|
||||
self.title = self.media_file.path.split("/")[-1]
|
||||
elif self.linked_playlist:
|
||||
self.title = self.linked_playlist.title
|
||||
|
||||
strip_text_items = ["title", "description"]
|
||||
for item in strip_text_items:
|
||||
@@ -295,6 +309,10 @@ class Media(models.Model):
|
||||
|
||||
self.state = helpers.get_default_state(user=self.user)
|
||||
|
||||
# Set encoding_status to success for playlist type
|
||||
if self.media_type == "playlist":
|
||||
self.encoding_status = "success"
|
||||
|
||||
# condition to appear on listings
|
||||
if self.state == "public" and self.encoding_status == "success" and self.is_reviewed is True:
|
||||
self.listable = True
|
||||
@@ -383,11 +401,16 @@ class Media(models.Model):
|
||||
Performs all related tasks, as check for media type,
|
||||
video duration, encode
|
||||
"""
|
||||
# Skip media_init for playlist type as it has no media file to process
|
||||
if self.media_type == "playlist" or self.linked_playlist:
|
||||
return True
|
||||
|
||||
self.set_media_type()
|
||||
from ..methods import is_media_allowed_type
|
||||
|
||||
if not is_media_allowed_type(self):
|
||||
helpers.rm_file(self.media_file.path)
|
||||
if self.media_file and self.media_file.path:
|
||||
helpers.rm_file(self.media_file.path)
|
||||
if self.state == "public":
|
||||
self.state = "unlisted"
|
||||
self.save(update_fields=["state"])
|
||||
@@ -765,6 +788,9 @@ class Media(models.Model):
|
||||
Prioritize uploaded_thumbnail, if exists, then thumbnail
|
||||
that is auto-generated
|
||||
"""
|
||||
# If this media represents a playlist, use playlist's thumbnail
|
||||
if self.linked_playlist:
|
||||
return self.linked_playlist.thumbnail_url
|
||||
|
||||
if self.uploaded_thumbnail:
|
||||
return helpers.url_from_path(self.uploaded_thumbnail.path)
|
||||
@@ -780,6 +806,9 @@ class Media(models.Model):
|
||||
Prioritize uploaded_poster, if exists, then poster
|
||||
that is auto-generated
|
||||
"""
|
||||
# If this media represents a playlist, use playlist's thumbnail
|
||||
if self.linked_playlist:
|
||||
return self.linked_playlist.thumbnail_url
|
||||
|
||||
if self.uploaded_poster:
|
||||
return helpers.url_from_path(self.uploaded_poster.path)
|
||||
@@ -917,6 +946,14 @@ class Media(models.Model):
|
||||
return helpers.url_from_path(self.user.logo.path)
|
||||
|
||||
def get_absolute_url(self, api=False, edit=False):
|
||||
# If this media represents a playlist, redirect to playlist page
|
||||
if self.linked_playlist:
|
||||
if edit:
|
||||
# For now, playlist editing is not supported via media edit page
|
||||
return self.linked_playlist.get_absolute_url(api=api)
|
||||
# Start playback from first media when clicking on playlist in listings
|
||||
return self.linked_playlist.get_absolute_url(api=api, start_playback=True)
|
||||
|
||||
if edit:
|
||||
return f"{reverse('edit_media')}?m={self.friendly_token}"
|
||||
if api:
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import uuid
|
||||
|
||||
from django.db import models
|
||||
from django.db.models.signals import post_save, pre_delete
|
||||
from django.dispatch import receiver
|
||||
from django.urls import reverse
|
||||
from django.utils.html import strip_tags
|
||||
|
||||
@@ -31,7 +33,25 @@ class Playlist(models.Model):
|
||||
def media_count(self):
|
||||
return self.media.filter(listable=True).count()
|
||||
|
||||
def get_absolute_url(self, api=False):
|
||||
def get_first_media(self):
|
||||
"""Get the first media item in the playlist"""
|
||||
pm = self.playlistmedia_set.filter(media__listable=True).first()
|
||||
return pm.media if pm else None
|
||||
|
||||
def get_absolute_url(self, api=False, start_playback=False):
|
||||
"""
|
||||
Get the URL for this playlist.
|
||||
|
||||
Args:
|
||||
api: If True, return API URL
|
||||
start_playback: If True, return URL to first media with playlist context
|
||||
"""
|
||||
if start_playback and not api:
|
||||
# Get first media and return its URL with playlist parameter
|
||||
first_media = self.get_first_media()
|
||||
if first_media:
|
||||
return f"{first_media.get_absolute_url()}&pl={self.friendly_token}"
|
||||
|
||||
if api:
|
||||
return reverse("api_get_playlist", kwargs={"friendly_token": self.friendly_token})
|
||||
else:
|
||||
@@ -41,6 +61,11 @@ class Playlist(models.Model):
|
||||
def url(self):
|
||||
return self.get_absolute_url()
|
||||
|
||||
@property
|
||||
def playback_url(self):
|
||||
"""URL that starts playing the first media in the playlist"""
|
||||
return self.get_absolute_url(start_playback=True)
|
||||
|
||||
@property
|
||||
def api_url(self):
|
||||
return self.get_absolute_url(api=True)
|
||||
@@ -95,3 +120,46 @@ class PlaylistMedia(models.Model):
|
||||
|
||||
class Meta:
|
||||
ordering = ["ordering", "-action_date"]
|
||||
|
||||
|
||||
@receiver(post_save, sender=Playlist)
|
||||
def create_or_update_playlist_media(sender, instance, created, **kwargs):
|
||||
"""
|
||||
Automatically create or update a Media object that represents this Playlist in listings.
|
||||
This allows playlists to appear alongside regular media in search results and listings.
|
||||
"""
|
||||
from .media import Media
|
||||
|
||||
# Check if a Media representation already exists for this playlist
|
||||
media_representation = Media.objects.filter(linked_playlist=instance).first()
|
||||
|
||||
if media_representation:
|
||||
# Update existing media representation
|
||||
media_representation.title = instance.title
|
||||
media_representation.description = instance.description
|
||||
media_representation.user = instance.user
|
||||
media_representation.media_type = "playlist"
|
||||
media_representation.encoding_status = "success"
|
||||
media_representation.save()
|
||||
else:
|
||||
# Create new media representation for this playlist
|
||||
Media.objects.create(
|
||||
title=instance.title,
|
||||
description=instance.description,
|
||||
user=instance.user,
|
||||
linked_playlist=instance,
|
||||
media_type="playlist",
|
||||
encoding_status="success",
|
||||
# Inherit the same state and review status defaults
|
||||
)
|
||||
|
||||
|
||||
@receiver(pre_delete, sender=Playlist)
|
||||
def delete_playlist_media(sender, instance, **kwargs):
|
||||
"""
|
||||
Delete the associated Media representation when a Playlist is deleted.
|
||||
"""
|
||||
from .media import Media
|
||||
|
||||
# Delete any Media objects that represent this playlist
|
||||
Media.objects.filter(linked_playlist=instance).delete()
|
||||
|
||||
@@ -29,6 +29,7 @@ MEDIA_TYPES_SUPPORTED = (
|
||||
("image", "Image"),
|
||||
("pdf", "Pdf"),
|
||||
("audio", "Audio"),
|
||||
("playlist", "Playlist"),
|
||||
)
|
||||
|
||||
ENCODE_EXTENSIONS = (
|
||||
|
||||
Reference in New Issue
Block a user