Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 62 additions & 43 deletions video/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,59 +55,71 @@ def change_video_status_to_queued(modeladmin, request, queryset):
try:
video.to_queued()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


def change_video_status_to_queued_failed(modeladmin, request, queryset):
for video in queryset:
try:
video.to_queued_failed()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


def change_video_status_to_processing(modeladmin, request, queryset):
for video in queryset:
try:
video.to_processing()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


def change_video_status_to_processing_failed(modeladmin, request, queryset):
for video in queryset:
try:
video.to_processing_failed()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


def change_video_status_to_finished(modeladmin, request, queryset):
for video in queryset:
try:
video.to_finished()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


def re_process_video(modeladmin, request, queryset):
for video in queryset:
try:
video.re_process()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


# Videos status function for live videos
Expand All @@ -116,46 +128,58 @@ def change_live_video_status_to_on(modeladmin, request, queryset):
try:
video.to_on()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the live video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the live video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


def change_live_video_status_to_off(modeladmin, request, queryset):
for video in queryset:
try:
video.to_off()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the live video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the live video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


def change_live_video_status_to_starting(modeladmin, request, queryset):
for video in queryset:
try:
video.to_starting()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the live video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the live video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


def change_live_video_status_to_stopping(modeladmin, request, queryset):
for video in queryset:
try:
video.to_stopping()
except TransitionNotAllowed:
modeladmin.message_user(request,
f'The status of the live video {video.name} cannot be changed to the one entered.',
level=messages.ERROR, )
modeladmin.message_user(
request,
f'The status of the live video {video.name} cannot be changed to the one entered.',
level=messages.ERROR,
)


# Videos status description for VOD
change_video_status_to_queued.short_description = 'Change status to Queued'
change_video_status_to_queued_failed.short_description = 'Change status to Queued (Failed)'
change_video_status_to_queued_failed.short_description = (
'Change status to Queued (Failed)'
)
change_video_status_to_processing.short_description = 'Change status to Processing'
change_video_status_to_processing_failed.short_description = 'Change status to Processing (Failed)'
change_video_status_to_processing_failed.short_description = (
'Change status to Processing (Failed)'
)
change_video_status_to_finished.short_description = 'Change status to Finished'
re_process_video.short_description = 'Re-process video'

Expand Down Expand Up @@ -187,7 +211,7 @@ class VideoAdmin(admin.ModelAdmin):
'organization__name',
'video_id',
)

readonly_fields = (
'video_id',
'created_by',
Expand All @@ -202,7 +226,7 @@ class VideoAdmin(admin.ModelAdmin):
change_video_status_to_processing,
change_video_status_to_processing_failed,
change_video_status_to_finished,
re_process_video
re_process_video,
]
form = VideoForm

Expand All @@ -211,14 +235,9 @@ def save_model(self, request, obj, form, change):
# Only set added_by during the first save.
obj.created_by = request.user
super().save_model(request, obj, form, change)

def get_readonly_fields(self, request, obj=None):
readonly_fields = [
'video_id',
'created_by',
'state',
'created_at'
]
readonly_fields = ['video_id', 'created_by', 'state', 'created_at']

if obj is not None:
readonly_fields.append('media_type')
Expand All @@ -244,7 +263,7 @@ class LiveVideoAdmin(admin.ModelAdmin):
'organization',
'created_by',
'state',
'created_at'
'created_at',
)
list_filter = (
'organization',
Expand All @@ -259,7 +278,7 @@ class LiveVideoAdmin(admin.ModelAdmin):
change_live_video_status_to_on,
change_live_video_status_to_off,
change_live_video_status_to_starting,
change_live_video_status_to_stopping
change_live_video_status_to_stopping,
]
form = LiveVideoForm

Expand Down
2 changes: 1 addition & 1 deletion video/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .tag import Tag
from .media import Media
from .live import LiveVideo
from .cuts import LiveVideoCut
from .cuts import LiveVideoCut
90 changes: 54 additions & 36 deletions video/models/cuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,37 @@ class State:
CHOICES = (
(SCHEDULED, SCHEDULED),
(EXECUTING, EXECUTING),
(PERFORMED, PERFORMED)
(PERFORMED, PERFORMED),
)

live = models.ForeignKey(LiveVideo,
related_name='cuts',
verbose_name='Video related to the cut',
on_delete=models.CASCADE,
)
initial_time = models.DateTimeField(null=True, blank=True,
verbose_name='Cut start time')
final_time = models.DateTimeField(null=True, blank=True,
verbose_name='Cut end time')
description = models.CharField(max_length=200,
default='',
verbose_name='Cut reason'
)
created_by = models.ForeignKey(Account,
models.SET_NULL,
related_name='uploaded_live_video_cut',
verbose_name='Created by',
null=True)
state = FSMField(default=State.SCHEDULED,
verbose_name='Cut state',
choices=State.CHOICES,
protected=True)
live = models.ForeignKey(
LiveVideo,
related_name='cuts',
verbose_name='Video related to the cut',
on_delete=models.CASCADE,
)
initial_time = models.DateTimeField(
null=True, blank=True, verbose_name='Cut start time'
)
final_time = models.DateTimeField(
null=True, blank=True, verbose_name='Cut end time'
)
description = models.CharField(
max_length=200, default='', verbose_name='Cut reason'
)
created_by = models.ForeignKey(
Account,
models.SET_NULL,
related_name='uploaded_live_video_cut',
verbose_name='Created by',
null=True,
)
state = FSMField(
default=State.SCHEDULED,
verbose_name='Cut state',
choices=State.CHOICES,
protected=True,
)

def __str__(self):
return f'{self.live.name} (executed at:{self.initial_time} resumed at:{self.final_time})'
Expand All @@ -65,15 +71,21 @@ def _to_performed(self):
def to_executing(self):
self._to_executing()

if self.live.state not in [LiveVideo.State.STOPPING, LiveVideo.State.OFF]:
if self.live.state not in [
LiveVideo.State.STOPPING,
LiveVideo.State.OFF,
]:
self.live.to_stopping()

self.save(update_fields=['state'])

def to_performed(self):
self._to_performed()

if self.live.state not in [LiveVideo.State.STARTING, LiveVideo.State.ON]:
if self.live.state not in [
LiveVideo.State.STARTING,
LiveVideo.State.ON,
]:
self.live.to_starting()

self.save(update_fields=['state'])
Expand All @@ -94,18 +106,22 @@ def live_pre_save_receiver(sender, instance, update_fields, **kwargs):

if not validate_dates(instance):
raise IntegrityError(
'There is already a break in the interval entered or the start date is in the past.')
'There is already a break in the interval entered or the start date is in the past.'
)


def validate_dates(instance):
if instance.final_time <= instance.initial_time or \
(instance.initial_time < timezone.now() and
instance.state in [LiveVideoCut.State.SCHEDULED]):
if instance.final_time <= instance.initial_time or (
instance.initial_time < timezone.now()
and instance.state in [LiveVideoCut.State.SCHEDULED]
):
return False

queryset = LiveVideoCut.objects.filter(live=instance.live,
initial_time__lt=instance.final_time,
final_time__gt=instance.initial_time)
queryset = LiveVideoCut.objects.filter(
live=instance.live,
initial_time__lt=instance.final_time,
final_time__gt=instance.initial_time,
)
if instance.id:
queryset = queryset.exclude(id=instance.id)

Expand All @@ -118,9 +134,11 @@ def validate_dates(instance):
@shared_task
def check_live_cuts():
now = timezone.now()
cuts_to_off = LiveVideoCut.objects.filter(state=LiveVideoCut.State.SCHEDULED,
initial_time__lte=now)
cuts_to_on = LiveVideoCut.objects.filter(state=LiveVideoCut.State.EXECUTING,
final_time__lte=now)
cuts_to_off = LiveVideoCut.objects.filter(
state=LiveVideoCut.State.SCHEDULED, initial_time__lte=now
)
cuts_to_on = LiveVideoCut.objects.filter(
state=LiveVideoCut.State.EXECUTING, final_time__lte=now
)
list(map(lambda cut: cut.to_executing(), cuts_to_off))
list(map(lambda cut: cut.to_performed(), cuts_to_on))
Loading