Dashboard-XRoom/core/serializers/VideoSerializer.py

33 lines
1.2 KiB
Python
Raw Normal View History

2025-04-19 12:04:52 +00:00
from rest_framework import serializers
from ..models.video import Video
from ..models.customer import Customer
from rest_framework.exceptions import ValidationError
import os
class VideoSerializer(serializers.ModelSerializer):
class Meta:
model = Video
fields = ['id', 'url', 'video', 'name', 'created_at']
read_only_fields = ['id', 'created_at', 'user']
def validate(self, data):
# Safely get the request from context
request = self.context.get('request')
if not request:
raise serializers.ValidationError("Request context not provided")
# Check file size if image is provided
if 'video' in request.FILES:
video = request.FILES['video']
2025-06-17 08:01:56 +00:00
if video.size > 200 * 1024 * 1024: # 500MB limit
2025-04-19 12:04:52 +00:00
raise serializers.ValidationError("Video file too large ( > 500MB )")
# Check file extension
ext = os.path.splitext(Video.name)[1].lower()
valid_extensions = ['.mp4']
if ext not in valid_extensions:
raise serializers.ValidationError(f"Unsupported file extension. Supported: {', '.join(valid_extensions)}")
return data