|
|
@ -12,6 +12,21 @@ from ..models import ( |
|
|
from .category import LocalizedField |
|
|
from .category import LocalizedField |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def safe_copy_data(data): |
|
|
|
|
|
if hasattr(data, "lists"): |
|
|
|
|
|
new_data = {} |
|
|
|
|
|
for key, values in data.lists(): |
|
|
|
|
|
if key in ["delete_image_ids", "uploaded_images"]: |
|
|
|
|
|
new_data[key] = values |
|
|
|
|
|
else: |
|
|
|
|
|
new_data[key] = values[0] if values else None |
|
|
|
|
|
return new_data |
|
|
|
|
|
elif hasattr(data, "copy"): |
|
|
|
|
|
return data.copy() |
|
|
|
|
|
else: |
|
|
|
|
|
return dict(data) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AbsoluteImageField(serializers.ImageField): |
|
|
class AbsoluteImageField(serializers.ImageField): |
|
|
def to_representation(self, value): |
|
|
def to_representation(self, value): |
|
|
if not value: |
|
|
if not value: |
|
|
@ -113,12 +128,12 @@ class AdminHadisCollectionSerializer(serializers.ModelSerializer): |
|
|
read_only_fields = ["id", "created_at", "updated_at"] |
|
|
read_only_fields = ["id", "created_at", "updated_at"] |
|
|
|
|
|
|
|
|
def to_internal_value(self, data): |
|
|
def to_internal_value(self, data): |
|
|
mutable_data = data.copy() if hasattr(data, "copy") else data |
|
|
|
|
|
|
|
|
mutable_data = safe_copy_data(data) |
|
|
thumbnail = mutable_data.get("thumbnail") if hasattr(mutable_data, "get") else None |
|
|
thumbnail = mutable_data.get("thumbnail") if hasattr(mutable_data, "get") else None |
|
|
if thumbnail and hasattr(thumbnail, "read"): |
|
|
if thumbnail and hasattr(thumbnail, "read"): |
|
|
compressed_bytes = maybe_compress_uploaded_file(thumbnail) |
|
|
compressed_bytes = maybe_compress_uploaded_file(thumbnail) |
|
|
if compressed_bytes is not None: |
|
|
if compressed_bytes is not None: |
|
|
mutable_data = mutable_data.copy() if hasattr(mutable_data, "copy") else mutable_data |
|
|
|
|
|
|
|
|
mutable_data = safe_copy_data(mutable_data) |
|
|
mutable_data["thumbnail"] = SimpleUploadedFile( |
|
|
mutable_data["thumbnail"] = SimpleUploadedFile( |
|
|
name=getattr(thumbnail, "name", "image"), |
|
|
name=getattr(thumbnail, "name", "image"), |
|
|
content=compressed_bytes, |
|
|
content=compressed_bytes, |
|
|
@ -329,7 +344,7 @@ class AdminHadisCorrectionSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
|
def to_internal_value(self, data): |
|
|
def to_internal_value(self, data): |
|
|
import json |
|
|
import json |
|
|
data = data.copy() if hasattr(data, "copy") else dict(data) |
|
|
|
|
|
|
|
|
data = safe_copy_data(data) |
|
|
for json_field in ["title", "translation", "links"]: |
|
|
for json_field in ["title", "translation", "links"]: |
|
|
val = data.get(json_field) |
|
|
val = data.get(json_field) |
|
|
if isinstance(val, str): |
|
|
if isinstance(val, str): |
|
|
@ -446,7 +461,7 @@ class AdminHadisInterpretationSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
|
def to_internal_value(self, data): |
|
|
def to_internal_value(self, data): |
|
|
import json |
|
|
import json |
|
|
data = data.copy() if hasattr(data, "copy") else dict(data) |
|
|
|
|
|
|
|
|
data = safe_copy_data(data) |
|
|
for json_field in ["title", "translation", "links"]: |
|
|
for json_field in ["title", "translation", "links"]: |
|
|
val = data.get(json_field) |
|
|
val = data.get(json_field) |
|
|
if isinstance(val, str): |
|
|
if isinstance(val, str): |
|
|
@ -1069,7 +1084,7 @@ class AdminTransmitterOriginalTextSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
|
def to_internal_value(self, data): |
|
|
def to_internal_value(self, data): |
|
|
import json |
|
|
import json |
|
|
data = data.copy() if hasattr(data, "copy") else dict(data) |
|
|
|
|
|
|
|
|
data = safe_copy_data(data) |
|
|
for json_field in ["title", "text", "translation"]: |
|
|
for json_field in ["title", "text", "translation"]: |
|
|
val = data.get(json_field) |
|
|
val = data.get(json_field) |
|
|
if isinstance(val, str): |
|
|
if isinstance(val, str): |
|
|
|