Skip to content

Commit

Permalink
transaction save and rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
Ammarmoulla committed Oct 12, 2023
1 parent 9e7d0ba commit a2a8d43
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 52 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/django.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: Django CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
# on:
# push:
# branches: [ "main" ]
# pull_request:
# branches: [ "main" ]

jobs:
build:
Expand Down
1 change: 0 additions & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault('C_FORCE_ROOT', 'true')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
try:
from django.core.management import execute_from_command_line
Expand Down
4 changes: 2 additions & 2 deletions video/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

def new_paths(video_path):

name_video = os.path.basename(video_path)[:-4] + "_edit.mp4"
full_path = video_path[:-4] + "_edit.mp4"
name_video = os.path.basename(video_path)
full_path = video_path[:-4] + "_temp.mp4"
return full_path, name_video

def encode_image(frame):
Expand Down
1 change: 1 addition & 0 deletions video/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
class VideoSerializer(serializers.ModelSerializer):
class Meta:
model = Video
read_only_fields = ("id")
fields = ["id", "video", "slug"]
16 changes: 4 additions & 12 deletions video/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ def process_clip(clip, text, x, y, fontsize):
processed_frames.ready()

processed_frames.successful()

# for frame in clip.iter_frames():
# processed_frame = process_frame.delay(frame, text, x, y, fontsize).get()
# processed_frames.append(processed_frame)

print(processed_frames[0])
emoji_clip = ImageSequenceClip(processed_frames.join(), fps=clip.fps)
processed_video = CompositeVideoClip([clip, emoji_clip])

return processed_video

# @shared_task
def edit_video(video_slug, video_path, text_clip, x, y, timestep, duration, fontsize):
def edit_video(video_path, text_clip, x, y, timestep, duration, fontsize):

clip = VideoFileClip(video_path)

Expand All @@ -62,15 +62,7 @@ def edit_video(video_slug, video_path, text_clip, x, y, timestep, duration, font

final_clip = concatenate_videoclips([clip1, final_clip2, clip3])

full_path, name_video = new_paths(video_path)

new_video = Video()
final_clip.write_videofile(full_path)
content = open(full_path, 'rb').read()
new_video.video.save(name_video, ContentFile(content))
new_video.slug = video_slug
# final_clip.delete()
new_video.save()
# full_path, name_video = new_paths(video_path)

# return final_clip
return final_clip

66 changes: 34 additions & 32 deletions video/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.decorators import action
Expand All @@ -8,8 +9,10 @@
from rest_framework.exceptions import APIException
import time
from .models import Video
from django.core.files.base import ContentFile
from .serializers import VideoSerializer
from .tasks import edit_video
from .helper import new_paths



Expand All @@ -22,36 +25,35 @@ class VideoViewSet(viewsets.ModelViewSet):
def add_text(self, request, pk=None):

video_orginal = self.get_object()

# try:
# with transaction.atomic():

start_time = time.time()
edit_video(
video_orginal.slug,
video_orginal.video.path,
request.data['text'],
int(request.data['x']),
int(request.data['y']),
int(request.data['t']),
int(request.data['d']),
int(request.data['s'])
)
print(time.time() - start_time)
# edit_video.delay(
# video_orginal.slug,
# video_orginal.video.path,
# request.data['text'],
# int(request.data['x']),
# int(request.data['y']),
# int(request.data['t']),
# int(request.data['d']),
# int(request.data['s'])
# )

# transaction.on_commit(lambda: task_execute.delay(job_params))

# except Exception as e:
# raise APIException(str(e))
video_path = video_orginal.video.path
video_slug = video_orginal.slug
video_obj = video_orginal.video

try:
with transaction.atomic():
final_clip = edit_video(
video_path,
request.data['text'],
int(request.data['x']),
int(request.data['y']),
int(request.data['t']),
int(request.data['d']),
int(request.data['s'])
)

full_path, name_video = new_paths(video_path)

final_clip.write_videofile(full_path, threads=4, audio = False, progress_bar = False)
content = open(full_path, 'rb').read()

new_video = Video(slug = video_slug + "edit")
new_video.video.save(name_video, ContentFile(content))
new_video.save()

os.remove(full_path)

except Exception as e:
transaction.rollback()
raise APIException(str(e))

return Response({"edit":True}, status=status.HTTP_200_OK)
return Response({"edit":True}, status=status.HTTP_200_OK)

0 comments on commit a2a8d43

Please sign in to comment.