diff --git a/apps/hadis/serializers/serializers_admin.py b/apps/hadis/serializers/serializers_admin.py index 0ff0752..3038824 100644 --- a/apps/hadis/serializers/serializers_admin.py +++ b/apps/hadis/serializers/serializers_admin.py @@ -1278,6 +1278,8 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): number_of_volumes = serializers.SerializerMethodField() researchers = AdminBookResearcherSerializer(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: model = BookEdition @@ -1297,6 +1299,8 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): "volumes_detail", "researchers", "editors", + "researcher_author_id", + "editor_author_id", "created_at", "updated_at", ] @@ -1318,73 +1322,66 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): 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: + 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: 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 - ) + 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: + 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: 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 - ) + 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): researchers_data = validated_data.pop("researchers", []) 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) for res_data in researchers_data: BookResearcher.objects.create(book_edition=edition, **res_data) for ed_data in editors_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 def update(self, instance, validated_data): researchers_data = validated_data.pop("researchers", 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) if researchers_data is not None: # Delete researchers no longer in the request payload @@ -1420,5 +1417,10 @@ class AdminBookEditionSerializer(serializers.ModelSerializer): # Create new editor 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 diff --git a/apps/hadis/views_admin.py b/apps/hadis/views_admin.py index 7ee332f..c742e94 100644 --- a/apps/hadis/views_admin.py +++ b/apps/hadis/views_admin.py @@ -541,9 +541,11 @@ class AdminBookReferenceViewSet(ModelViewSet): queryset = queryset.filter( Q(title__icontains=search_query) | Q(slug__icontains=search_query) - | Q(isbn__icontains=search_query) - | Q(publisher__icontains=search_query) + | Q(editions__isbn__icontains=search_query) + | Q(editions__publisher__icontains=search_query) | Q(description__icontains=search_query) + | Q(author__name__icontains=search_query) + | Q(tags__title__icontains=search_query) ) type_id = self.request.query_params.get("type")