|
|
@ -1277,6 +1277,8 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): |
|
|
number_of_volumes = serializers.SerializerMethodField() |
|
|
number_of_volumes = serializers.SerializerMethodField() |
|
|
researchers = AdminBookResearcherSerializer(many=True, required=False) |
|
|
researchers = AdminBookResearcherSerializer(many=True, required=False) |
|
|
editors = AdminBookEditorSerializer(many=True, required=False) |
|
|
editors = AdminBookEditorSerializer(many=True, required=False) |
|
|
|
|
|
researcher_author_id = serializers.IntegerField(write_only=True, required=False, allow_null=True) |
|
|
|
|
|
editor_author_id = serializers.IntegerField(write_only=True, required=False, allow_null=True) |
|
|
|
|
|
|
|
|
class Meta: |
|
|
class Meta: |
|
|
model = BookEdition |
|
|
model = BookEdition |
|
|
@ -1296,6 +1298,8 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): |
|
|
"volumes_detail", |
|
|
"volumes_detail", |
|
|
"researchers", |
|
|
"researchers", |
|
|
"editors", |
|
|
"editors", |
|
|
|
|
|
"researcher_author_id", |
|
|
|
|
|
"editor_author_id", |
|
|
"created_at", |
|
|
"created_at", |
|
|
"updated_at", |
|
|
"updated_at", |
|
|
] |
|
|
] |
|
|
@ -1317,73 +1321,66 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): |
|
|
data = request.data if request else {} |
|
|
data = request.data if request else {} |
|
|
|
|
|
|
|
|
if "researcher_author_id" in data or "researcher_author_id" in validated_data: |
|
|
if "researcher_author_id" in data or "researcher_author_id" in validated_data: |
|
|
res_val = data.get("researcher_author_id") if "researcher_author_id" in data else validated_data.get("researcher_author_id") |
|
|
|
|
|
if not res_val or res_val in ["none", "", "null", None]: |
|
|
|
|
|
edition.researchers.all().delete() |
|
|
|
|
|
else: |
|
|
|
|
|
|
|
|
res_val = validated_data.get("researcher_author_id") if "researcher_author_id" in validated_data else data.get("researcher_author_id") |
|
|
|
|
|
edition.researchers.all().delete() |
|
|
|
|
|
if res_val and res_val not in ["none", "", "null", None]: |
|
|
try: |
|
|
try: |
|
|
author_id = int(res_val) |
|
|
author_id = int(res_val) |
|
|
author_obj = BookAuthor.objects.filter(id=author_id).first() |
|
|
author_obj = BookAuthor.objects.filter(id=author_id).first() |
|
|
if author_obj: |
|
|
if author_obj: |
|
|
res = edition.researchers.first() |
|
|
|
|
|
if res: |
|
|
|
|
|
res.author = author_obj |
|
|
|
|
|
res.name = author_obj.name |
|
|
|
|
|
res.slug = author_obj.slug |
|
|
|
|
|
res.save() |
|
|
|
|
|
else: |
|
|
|
|
|
BookResearcher.objects.create( |
|
|
|
|
|
book_edition=edition, |
|
|
|
|
|
author=author_obj, |
|
|
|
|
|
name=author_obj.name, |
|
|
|
|
|
slug=author_obj.slug |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
BookResearcher.objects.create( |
|
|
|
|
|
book_edition=edition, |
|
|
|
|
|
author=author_obj, |
|
|
|
|
|
name=author_obj.name, |
|
|
|
|
|
slug=author_obj.slug |
|
|
|
|
|
) |
|
|
except (ValueError, TypeError): |
|
|
except (ValueError, TypeError): |
|
|
pass |
|
|
pass |
|
|
|
|
|
|
|
|
if "editor_author_id" in data or "editor_author_id" in validated_data: |
|
|
if "editor_author_id" in data or "editor_author_id" in validated_data: |
|
|
ed_val = data.get("editor_author_id") if "editor_author_id" in data else validated_data.get("editor_author_id") |
|
|
|
|
|
if not ed_val or ed_val in ["none", "", "null", None]: |
|
|
|
|
|
edition.editors.all().delete() |
|
|
|
|
|
else: |
|
|
|
|
|
|
|
|
ed_val = validated_data.get("editor_author_id") if "editor_author_id" in validated_data else data.get("editor_author_id") |
|
|
|
|
|
edition.editors.all().delete() |
|
|
|
|
|
if ed_val and ed_val not in ["none", "", "null", None]: |
|
|
try: |
|
|
try: |
|
|
author_id = int(ed_val) |
|
|
author_id = int(ed_val) |
|
|
author_obj = BookAuthor.objects.filter(id=author_id).first() |
|
|
author_obj = BookAuthor.objects.filter(id=author_id).first() |
|
|
if author_obj: |
|
|
if author_obj: |
|
|
ed_obj = edition.editors.first() |
|
|
|
|
|
if ed_obj: |
|
|
|
|
|
ed_obj.author = author_obj |
|
|
|
|
|
ed_obj.name = author_obj.name |
|
|
|
|
|
ed_obj.slug = author_obj.slug |
|
|
|
|
|
ed_obj.save() |
|
|
|
|
|
else: |
|
|
|
|
|
BookEditor.objects.create( |
|
|
|
|
|
book_edition=edition, |
|
|
|
|
|
author=author_obj, |
|
|
|
|
|
name=author_obj.name, |
|
|
|
|
|
slug=author_obj.slug |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
BookEditor.objects.create( |
|
|
|
|
|
book_edition=edition, |
|
|
|
|
|
author=author_obj, |
|
|
|
|
|
name=author_obj.name, |
|
|
|
|
|
slug=author_obj.slug |
|
|
|
|
|
) |
|
|
except (ValueError, TypeError): |
|
|
except (ValueError, TypeError): |
|
|
pass |
|
|
pass |
|
|
|
|
|
|
|
|
def create(self, validated_data): |
|
|
def create(self, validated_data): |
|
|
researchers_data = validated_data.pop("researchers", []) |
|
|
researchers_data = validated_data.pop("researchers", []) |
|
|
editors_data = validated_data.pop("editors", []) |
|
|
editors_data = validated_data.pop("editors", []) |
|
|
validated_data.pop("researcher_author_id", None) |
|
|
|
|
|
validated_data.pop("editor_author_id", None) |
|
|
|
|
|
|
|
|
has_res_id = "researcher_author_id" in validated_data |
|
|
|
|
|
has_ed_id = "editor_author_id" in validated_data |
|
|
|
|
|
res_author_id = validated_data.pop("researcher_author_id", None) |
|
|
|
|
|
ed_author_id = validated_data.pop("editor_author_id", None) |
|
|
edition = super().create(validated_data) |
|
|
edition = super().create(validated_data) |
|
|
for res_data in researchers_data: |
|
|
for res_data in researchers_data: |
|
|
BookResearcher.objects.create(book_edition=edition, **res_data) |
|
|
BookResearcher.objects.create(book_edition=edition, **res_data) |
|
|
for ed_data in editors_data: |
|
|
for ed_data in editors_data: |
|
|
BookEditor.objects.create(book_edition=edition, **ed_data) |
|
|
BookEditor.objects.create(book_edition=edition, **ed_data) |
|
|
self._handle_author_links(edition, validated_data) |
|
|
|
|
|
|
|
|
author_links_dict = {} |
|
|
|
|
|
if has_res_id: |
|
|
|
|
|
author_links_dict["researcher_author_id"] = res_author_id |
|
|
|
|
|
if has_ed_id: |
|
|
|
|
|
author_links_dict["editor_author_id"] = ed_author_id |
|
|
|
|
|
self._handle_author_links(edition, author_links_dict) |
|
|
return edition |
|
|
return edition |
|
|
|
|
|
|
|
|
def update(self, instance, validated_data): |
|
|
def update(self, instance, validated_data): |
|
|
researchers_data = validated_data.pop("researchers", None) |
|
|
researchers_data = validated_data.pop("researchers", None) |
|
|
editors_data = validated_data.pop("editors", None) |
|
|
editors_data = validated_data.pop("editors", None) |
|
|
validated_data.pop("researcher_author_id", None) |
|
|
|
|
|
validated_data.pop("editor_author_id", None) |
|
|
|
|
|
|
|
|
has_res_id = "researcher_author_id" in validated_data |
|
|
|
|
|
has_ed_id = "editor_author_id" in validated_data |
|
|
|
|
|
res_author_id = validated_data.pop("researcher_author_id", None) |
|
|
|
|
|
ed_author_id = validated_data.pop("editor_author_id", None) |
|
|
edition = super().update(instance, validated_data) |
|
|
edition = super().update(instance, validated_data) |
|
|
if researchers_data is not None: |
|
|
if researchers_data is not None: |
|
|
# Delete researchers no longer in the request payload |
|
|
# Delete researchers no longer in the request payload |
|
|
@ -1419,5 +1416,10 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): |
|
|
# Create new editor |
|
|
# Create new editor |
|
|
BookEditor.objects.create(book_edition=instance, **ed_data) |
|
|
BookEditor.objects.create(book_edition=instance, **ed_data) |
|
|
|
|
|
|
|
|
self._handle_author_links(edition, validated_data) |
|
|
|
|
|
|
|
|
author_links_dict = {} |
|
|
|
|
|
if has_res_id: |
|
|
|
|
|
author_links_dict["researcher_author_id"] = res_author_id |
|
|
|
|
|
if has_ed_id: |
|
|
|
|
|
author_links_dict["editor_author_id"] = ed_author_id |
|
|
|
|
|
self._handle_author_links(edition, author_links_dict) |
|
|
return edition |
|
|
return edition |