feat: The correct way to get/post chapters in django

This commit is contained in:
Yiannis Christodoulou 2025-09-19 12:05:46 +03:00
parent b96134bd9f
commit 0456051a11
3 changed files with 19 additions and 8 deletions

View File

@ -604,7 +604,9 @@ def handle_video_chapters(media, chapters):
else:
video_chapter = models.VideoChapterData.objects.create(media=media, data=chapters)
return media.chapter_data
return {
'chapters': media.chapter_data,
}
def change_media_owner(media_id, new_user):

View File

@ -18,8 +18,13 @@ class VideoChapterData(models.Model):
data = []
if self.data and isinstance(self.data, list):
for item in self.data:
if item.get("startTime") and item.get("endTime") and item.get("text"):
data.append(item)
if item.get("startTime") and item.get("endTime") and item.get("chapterTitle"):
chapter_item = {
'startTime': item.get("startTime"),
'endTime': item.get("endTime"),
'chapterTitle': item.get("chapterTitle"),
}
data.append(chapter_item)
return data

View File

@ -256,22 +256,26 @@ def video_chapters(request, friendly_token):
return HttpResponseRedirect("/")
try:
data = json.loads(request.body)["segments"]
request_data = json.loads(request.body)
data = request_data.get("chapters")
if not data:
return JsonResponse({'success': False, 'error': 'Request must contain "chapters" array'}, status=400)
chapters = []
for _, chapter_data in enumerate(data):
start_time = chapter_data.get('startTime')
end_time = chapter_data.get('endTime')
text = chapter_data.get('text')
if start_time and end_time and text:
chapter_title = chapter_data.get('chapterTitle')
if start_time and end_time and chapter_title:
chapters.append(
{
'startTime': start_time,
'endTime': end_time,
'text': text,
'chapterTitle': chapter_title,
}
)
except Exception as e: # noqa
return JsonResponse({'success': False, 'error': 'Request data must be a list of video chapters with startTime, endTime, text'}, status=400)
return JsonResponse({'success': False, 'error': 'Request data must be a list of video chapters with startTime, endTime, chapterTitle'}, status=400)
ret = handle_video_chapters(media, chapters)