mirror of
https://github.com/Dadechin/Dashboard-XRoom.git
synced 2025-07-03 00:34:34 +00:00
33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
from rest_framework import serializers
|
|
from ..models.image import Image
|
|
from ..models.customer import Customer
|
|
|
|
from rest_framework.exceptions import ValidationError
|
|
import os
|
|
|
|
|
|
class ImageSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Image
|
|
fields = ['id', 'url', 'image', '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 'image' in request.FILES:
|
|
image = request.FILES['image']
|
|
if image.size > 5 * 1024 * 1024: # 5MB limit
|
|
raise serializers.ValidationError("Image file too large ( > 5MB )")
|
|
|
|
# Check file extension
|
|
ext = os.path.splitext(image.name)[1].lower()
|
|
valid_extensions = ['.jpg', '.jpeg', '.png', '.gif']
|
|
if ext not in valid_extensions:
|
|
raise serializers.ValidationError(f"Unsupported file extension. Supported: {', '.join(valid_extensions)}")
|
|
|
|
return data |