|
|
|
@ -10,7 +10,8 @@ from ..models import ( |
|
|
|
BookReference, BookReferenceImage, BookReferenceDocument, BookAuthor, BookAttribute, BookSubjectArea, BookType, BookTag, BookEdition, BookVolume, BookResearcher, BookEditor, |
|
|
|
Transmitters, TransmitterReliability, NarratorLayer, OpinionStatus, TransmitterOpinion, TransmitterOriginalText, |
|
|
|
CorrectionReference, CorrectionReferenceImage, InterpretationReference, InterpretationReferenceImage, |
|
|
|
OriginalTextReference, OriginalTextReferenceImage |
|
|
|
OriginalTextReference, OriginalTextReferenceImage, |
|
|
|
ContentRelease |
|
|
|
) |
|
|
|
from .category import LocalizedField |
|
|
|
|
|
|
|
@ -1098,6 +1099,11 @@ class AdminBookTypeSerializer(serializers.ModelSerializer): |
|
|
|
class AdminBookAuthorSerializer(serializers.ModelSerializer): |
|
|
|
image = AbsoluteImageField(required=False, allow_null=True) |
|
|
|
remove_image = serializers.BooleanField(write_only=True, required=False, default=False) |
|
|
|
book_references = serializers.PrimaryKeyRelatedField( |
|
|
|
many=True, |
|
|
|
required=False, |
|
|
|
queryset=BookReference.objects.all(), |
|
|
|
) |
|
|
|
|
|
|
|
class Meta: |
|
|
|
model = BookAuthor |
|
|
|
@ -1125,28 +1131,47 @@ class AdminBookAuthorSerializer(serializers.ModelSerializer): |
|
|
|
import json |
|
|
|
mutable_data = safe_copy_data(data) |
|
|
|
for json_field in ["name", "known_as", "scholarity", "tabaqa", "rutba", "features", "book_references"]: |
|
|
|
val = mutable_data.get(json_field) |
|
|
|
if isinstance(val, str): |
|
|
|
try: |
|
|
|
mutable_data[json_field] = json.loads(val) |
|
|
|
except ValueError: |
|
|
|
pass |
|
|
|
if json_field in mutable_data: |
|
|
|
val = mutable_data.get(json_field) |
|
|
|
if isinstance(val, str): |
|
|
|
try: |
|
|
|
mutable_data[json_field] = json.loads(val) |
|
|
|
except ValueError: |
|
|
|
pass |
|
|
|
for int_field in ["birth_year_hijri", "death_year_hijri", "birth_year_miladi", "death_year_miladi", "related_narrator"]: |
|
|
|
val = mutable_data.get(int_field) |
|
|
|
if val == "" or val == "null" or val == "undefined": |
|
|
|
mutable_data[int_field] = None |
|
|
|
if int_field in mutable_data: |
|
|
|
val = mutable_data.get(int_field) |
|
|
|
if val == "" or val == "null" or val == "undefined": |
|
|
|
mutable_data[int_field] = None |
|
|
|
return super().to_internal_value(mutable_data) |
|
|
|
|
|
|
|
def create(self, validated_data): |
|
|
|
validated_data.pop("remove_image", False) |
|
|
|
return super().create(validated_data) |
|
|
|
book_references = validated_data.pop("book_references", None) |
|
|
|
author = super().create(validated_data) |
|
|
|
if book_references: |
|
|
|
for book in book_references: |
|
|
|
book.author = author |
|
|
|
book.save(update_fields=["author"]) |
|
|
|
return author |
|
|
|
|
|
|
|
def update(self, instance, validated_data): |
|
|
|
remove_image = validated_data.pop("remove_image", False) |
|
|
|
book_references = validated_data.pop("book_references", None) |
|
|
|
if remove_image and instance.image: |
|
|
|
instance.image.delete(save=False) |
|
|
|
instance.image = None |
|
|
|
return super().update(instance, validated_data) |
|
|
|
author = super().update(instance, validated_data) |
|
|
|
if book_references is not None: |
|
|
|
current_ids = set(author.book_references.values_list('id', flat=True)) |
|
|
|
new_ids = {b.id for b in book_references} |
|
|
|
to_remove = current_ids - new_ids |
|
|
|
if to_remove: |
|
|
|
BookReference.objects.filter(id__in=to_remove, author=author).update(author=None) |
|
|
|
to_add = new_ids - current_ids |
|
|
|
if to_add: |
|
|
|
BookReference.objects.filter(id__in=to_add).update(author=author) |
|
|
|
return author |
|
|
|
|
|
|
|
|
|
|
|
class AdminBookAttributeSerializer(serializers.ModelSerializer): |
|
|
|
@ -1775,3 +1800,17 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): |
|
|
|
author_links_dict["editor_author_id"] = ed_author_id |
|
|
|
self._handle_author_links(edition, author_links_dict) |
|
|
|
return edition |
|
|
|
|
|
|
|
|
|
|
|
class AdminContentReleaseSerializer(serializers.ModelSerializer): |
|
|
|
class Meta: |
|
|
|
model = ContentRelease |
|
|
|
fields = [ |
|
|
|
"id", |
|
|
|
"version_name", |
|
|
|
"published_at", |
|
|
|
"description", |
|
|
|
"is_active", |
|
|
|
] |
|
|
|
read_only_fields = ["id", "published_at"] |
|
|
|
|