mirror of
https://github.com/codingforentrepreneurs/DjangoFlix.git
synced 2022-03-27 20:26:53 +03:00
46 - Instance Methods for Videos
This commit is contained in:
3
ToDo.md
3
ToDo.md
@@ -27,8 +27,9 @@
|
||||
- Elasticsearch
|
||||
- Video Analytics & Watch History
|
||||
- Video hosts API
|
||||
- Vimeo, YouTube,
|
||||
- Vimeo, YouTube, Wistia,
|
||||
- Open Source Option (ffmpeg)
|
||||
- Nginx, stream via ffmpeg
|
||||
- API-Driven Video Details
|
||||
- Cast Members (actors, directors)
|
||||
- Release Year
|
||||
|
||||
@@ -68,6 +68,20 @@ class Playlist(models.Model):
|
||||
def get_short_display(self):
|
||||
return ""
|
||||
|
||||
def get_video_id(self):
|
||||
"""
|
||||
get main video id to render video for users
|
||||
"""
|
||||
if self.video is None:
|
||||
return None
|
||||
return self.video.get_video_id()
|
||||
|
||||
def get_clips(self):
|
||||
"""
|
||||
get clips to render clips for users
|
||||
"""
|
||||
return self.playlistitem_set.all().published()
|
||||
|
||||
@property
|
||||
def is_published(self):
|
||||
return self.active
|
||||
@@ -86,6 +100,12 @@ class MovieProxy(Playlist):
|
||||
|
||||
objects = MovieProxyManager()
|
||||
|
||||
def get_movie_id(self):
|
||||
"""
|
||||
get movie id to render movie for users
|
||||
"""
|
||||
return self.get_video_id()
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'Movie'
|
||||
verbose_name_plural = 'Movies'
|
||||
@@ -121,6 +141,8 @@ class TVShowProxy(Playlist):
|
||||
def get_short_display(self):
|
||||
return f"{self.seasons.count()} Seasons"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class TVShowSeasonProxyManager(PlaylistManager):
|
||||
@@ -140,6 +162,38 @@ class TVShowSeasonProxy(Playlist):
|
||||
self.type = Playlist.PlaylistTypeChoices.SEASON
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def get_season_trailer(self):
|
||||
"""
|
||||
get episodes to render for users
|
||||
"""
|
||||
return self.get_video_id()
|
||||
|
||||
def get_episodes(self):
|
||||
"""
|
||||
get episodes to render for users
|
||||
"""
|
||||
return self.playlistitem_set.all().published()
|
||||
|
||||
|
||||
|
||||
|
||||
class PlaylistItemQuerySet(models.QuerySet):
|
||||
def published(self):
|
||||
now = timezone.now()
|
||||
return self.filter(
|
||||
playlist__state=PublishStateOptions.PUBLISH,
|
||||
playlist__publish_timestamp__lte= now,
|
||||
video__state=PublishStateOptions.PUBLISH,
|
||||
video__publish_timestamp__lte= now
|
||||
)
|
||||
|
||||
class PlaylistItemManager(models.Manager):
|
||||
def get_queryset(self):
|
||||
return PlaylistItemQuerySet(self.model, using=self._db)
|
||||
|
||||
def published(self):
|
||||
return self.get_queryset().published()
|
||||
|
||||
|
||||
class PlaylistItem(models.Model):
|
||||
playlist = models.ForeignKey(Playlist, on_delete=models.CASCADE)
|
||||
|
||||
@@ -34,10 +34,24 @@ class Video(models.Model):
|
||||
publish_timestamp = models.DateTimeField(auto_now_add=False, auto_now=False, blank=True, null=True)
|
||||
|
||||
objects = VideoManager()
|
||||
|
||||
def get_video_id(self):
|
||||
if not self.is_published:
|
||||
return None
|
||||
return self.video_id
|
||||
|
||||
@property
|
||||
def is_published(self):
|
||||
return self.active
|
||||
if self.active is False:
|
||||
return False
|
||||
state = self.state
|
||||
if state != PublishStateOptions.PUBLISH:
|
||||
return False
|
||||
pub_timestamp = self.publish_timestamp
|
||||
if pub_timestamp is None:
|
||||
return False
|
||||
now = timezone.now()
|
||||
return pub_timestamp <= now
|
||||
|
||||
def get_playlist_ids(self):
|
||||
# self.<foreigned_obj>_set.all()
|
||||
|
||||
@@ -29,6 +29,10 @@ class VideoModelTestCase(TestCase):
|
||||
qs = Video.objects.filter(state=PublishStateOptions.DRAFT)
|
||||
self.assertEqual(qs.count(), 1)
|
||||
|
||||
def test_draft_case(self):
|
||||
obj = Video.objects.filter(state=PublishStateOptions.DRAFT).first()
|
||||
self.assertFalse(obj.is_published)
|
||||
|
||||
def test_publish_case(self):
|
||||
qs = Video.objects.filter(state=PublishStateOptions.PUBLISH)
|
||||
now = timezone.now()
|
||||
@@ -38,6 +42,10 @@ class VideoModelTestCase(TestCase):
|
||||
)
|
||||
self.assertTrue(published_qs.exists())
|
||||
|
||||
def test_publish_case(self):
|
||||
obj = Video.objects.filter(state=PublishStateOptions.PUBLISH).first()
|
||||
self.assertTrue(obj.is_published)
|
||||
|
||||
def test_publish_manager(self):
|
||||
published_qs = Video.objects.all().published()
|
||||
published_qs_2 = Video.objects.published()
|
||||
|
||||
Reference in New Issue
Block a user