|
|
@ -1301,22 +1301,90 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): |
|
|
"updated_at", |
|
|
"updated_at", |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
def to_representation(self, instance): |
|
|
|
|
|
ret = super().to_representation(instance) |
|
|
|
|
|
res = instance.researchers.first() |
|
|
|
|
|
ret["researcher_author_id"] = res.author_id if res and res.author_id else None |
|
|
|
|
|
|
|
|
|
|
|
ed_obj = instance.editors.first() |
|
|
|
|
|
ret["editor_author_id"] = ed_obj.author_id if ed_obj and ed_obj.author_id else None |
|
|
|
|
|
return ret |
|
|
|
|
|
|
|
|
def get_number_of_volumes(self, obj): |
|
|
def get_number_of_volumes(self, obj): |
|
|
return obj.volumes.count() |
|
|
return obj.volumes.count() |
|
|
|
|
|
|
|
|
|
|
|
def _handle_author_links(self, edition, validated_data): |
|
|
|
|
|
request = self.context.get("request") |
|
|
|
|
|
data = request.data if request else {} |
|
|
|
|
|
|
|
|
|
|
|
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: |
|
|
|
|
|
try: |
|
|
|
|
|
author_id = int(res_val) |
|
|
|
|
|
author_obj = BookAuthor.objects.filter(id=author_id).first() |
|
|
|
|
|
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 |
|
|
|
|
|
) |
|
|
|
|
|
except (ValueError, TypeError): |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
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: |
|
|
|
|
|
try: |
|
|
|
|
|
author_id = int(ed_val) |
|
|
|
|
|
author_obj = BookAuthor.objects.filter(id=author_id).first() |
|
|
|
|
|
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 |
|
|
|
|
|
) |
|
|
|
|
|
except (ValueError, TypeError): |
|
|
|
|
|
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) |
|
|
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) |
|
|
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) |
|
|
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 |
|
|
@ -1352,4 +1420,5 @@ 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) |
|
|
return edition |
|
|
return edition |