diff --git a/scoap3/articles/api/serializers.py b/scoap3/articles/api/serializers.py index f010950e..d86e64f1 100644 --- a/scoap3/articles/api/serializers.py +++ b/scoap3/articles/api/serializers.py @@ -40,6 +40,12 @@ class Meta: model = Article fields = "__all__" + def to_representation(self, instance): + representation = super().to_representation(instance) + if instance.publication_date is None: + representation["publication_date"] = instance._created_at + return representation + class ArticleDocumentSerializer(DocumentSerializer): class Meta: diff --git a/scoap3/articles/tests/test_article_views.py b/scoap3/articles/tests/test_article_views.py index bc3d4264..7e12258e 100644 --- a/scoap3/articles/tests/test_article_views.py +++ b/scoap3/articles/tests/test_article_views.py @@ -71,6 +71,7 @@ def test_update_article_from_workflow(self, client, user, shared_datadir): client.force_login(user) contents = (shared_datadir / "workflow_record.json").read_text() data = json.loads(contents) + response = client.post( reverse("api:article-workflow-import-list"), data, @@ -138,6 +139,105 @@ def test_update_article_from_workflow(self, client, user, shared_datadir): assert len(expected_dois) == 1 assert "10.5506/APhysPolB.54.10-A3" in expected_dois + def test_create_article_from_workflow_without_publication_date( + self, client, user, shared_datadir + ): + client.force_login(user) + contents = (shared_datadir / "workflow_record.json").read_text() + data = json.loads(contents) + + del data["imprints"][0]["date"] + response = client.post( + reverse("api:article-workflow-import-list"), + data, + content_type="application/json", + ) + assert response.status_code == status.HTTP_200_OK + assert response.data["publication_date"] == response.data["_created_at"] + + article_id = response.data["id"] + article = Article.objects.get(id=article_id) + assert article.publication_date is None + + def test_create_update_from_workflow_without_publication_date( + self, client, user, shared_datadir + ): + client.force_login(user) + contents = (shared_datadir / "workflow_record.json").read_text() + data = json.loads(contents) + response = client.post( + reverse("api:article-workflow-import-list"), + data, + content_type="application/json", + ) + assert response.status_code == status.HTTP_200_OK + + article_id_with_publication_date = response.data["id"] + article_with_publication_date = Article.objects.get( + id=article_id_with_publication_date + ) + assert article_with_publication_date.publication_date is not None + + del data["imprints"][0]["date"] + response = client.post( + reverse("api:article-workflow-import-list"), + data, + content_type="application/json", + ) + assert response.status_code == status.HTTP_200_OK + + article_id_without_publication_date = response.data["id"] + assert article_id_with_publication_date == article_id_without_publication_date + article_without_publication_date = Article.objects.get( + id=article_id_without_publication_date + ) + assert article_without_publication_date.publication_date is not None + + def test_create_update_from_workflow_with_publication_date( + self, + client, + user, + shared_datadir, + ): + client.force_login(user) + contents = (shared_datadir / "workflow_record.json").read_text() + data = json.loads(contents) + + response = client.post( + reverse("api:article-workflow-import-list"), + data, + content_type="application/json", + ) + assert response.status_code == status.HTTP_200_OK + + article_id_with_publication_date = response.data["id"] + article_with_publication_date = Article.objects.get( + id=article_id_with_publication_date + ) + assert ( + article_with_publication_date.publication_date.strftime("%Y-%m-%d") + == "2023-10-31" + ) + data["imprints"][0]["date"] = "2024-06-20" + response = client.post( + reverse("api:article-workflow-import-list"), + data, + content_type="application/json", + ) + assert response.status_code == status.HTTP_200_OK + + article_id_with_updated_publication_date = response.data["id"] + assert ( + article_id_with_publication_date == article_id_with_updated_publication_date + ) + article_with_updated_publication_date = Article.objects.get( + id=article_id_with_updated_publication_date + ) + assert ( + article_with_updated_publication_date.publication_date.strftime("%Y-%m-%d") + == "2024-06-20" + ) + class TestArticleIdentifierViewSet: def test_get_article_identifier(self, client): diff --git a/scoap3/tasks.py b/scoap3/tasks.py index fc856c75..aa17f005 100644 --- a/scoap3/tasks.py +++ b/scoap3/tasks.py @@ -84,7 +84,6 @@ def _create_licenses(data): def _create_article(data, licenses): article_data = { - "publication_date": data["imprints"][0].get("date"), "title": data["titles"][0].get("title"), "subtitle": data["titles"][0].get("subtitle", ""), "abstract": data["abstracts"][0].get("value", ""), @@ -96,6 +95,10 @@ def _create_article(data, licenses): doi_exists = ArticleIdentifier.objects.filter( identifier_type="DOI", identifier_value=data.get("dois")[0].get("value") ).exists() + publication_date = data["imprints"][0].get("date") + + if publication_date: + article_data["publication_date"] = publication_date # if "control_number" present, means it is a legacy record if data.get("control_number"): @@ -117,12 +120,14 @@ def _create_article(data, licenses): article = ArticleIdentifier.objects.get( identifier_type="DOI", identifier_value=doi_value ).article_id + if publication_date: + article_data["publication_date"] = publication_date article.__dict__.update(**article_data) # else create new else: + article_data["publication_date"] = publication_date article = Article.objects.create(**article_data) article._created_at = data.get("_created") or data.get("record_creation_date") - article.related_licenses.set(licenses) article.save() return article