From 4bb06a75a6f010384209ce7a6ae02f2fc4f5cff9 Mon Sep 17 00:00:00 2001 From: ErnestaP Date: Thu, 18 Jul 2024 15:21:51 +0200 Subject: [PATCH] Create article: add real date_published * Set to if is None in the serializer. * ref: https://github.com/cern-sis/issues-scoap3/issues/338 --- scoap3/articles/api/serializers.py | 6 ++ scoap3/articles/tests/test_article_views.py | 114 ++++++++++++++++++-- scoap3/tasks.py | 10 +- 3 files changed, 118 insertions(+), 12 deletions(-) diff --git a/scoap3/articles/api/serializers.py b/scoap3/articles/api/serializers.py index f010950e3..d86e64f15 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 dd1265b2f..31ad4b2dd 100644 --- a/scoap3/articles/tests/test_article_views.py +++ b/scoap3/articles/tests/test_article_views.py @@ -9,20 +9,24 @@ pytestmark = pytest.mark.django_db +@pytest.fixture +def record(shared_datadir): + contents = (shared_datadir / "workflow_record.json").read_text() + return json.loads(contents) + + class TestArticleViewSet: def test_get_article(self, client): url = reverse("api:article-list") response = client.get(url) assert response.status_code == status.HTTP_200_OK - def test_create_article_from_workflow(self, client, user, shared_datadir): + def test_create_article_from_workflow(self, client, user, record): 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, + record, content_type="application/json", ) assert response.status_code == status.HTTP_200_OK @@ -34,13 +38,11 @@ def test_create_article_from_workflow(self, client, user, shared_datadir): == "The Effective QCD Running Coupling Constant and a Dirac Model for the Charmonium Spectrum" ) - def test_update_article_from_workflow(self, client, user, shared_datadir): + def test_update_article_from_workflow(self, client, user, record): 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, + record, content_type="application/json", ) assert response.status_code == status.HTTP_200_OK @@ -52,10 +54,10 @@ def test_update_article_from_workflow(self, client, user, shared_datadir): == "The Effective QCD Running Coupling Constant and a Dirac Model for the Charmonium Spectrum" ) - data["titles"][0]["title"] = "New title" + record["titles"][0]["title"] = "New title" response = client.post( reverse("api:article-workflow-import-list"), - data, + record, content_type="application/json", ) assert response.status_code == status.HTTP_200_OK @@ -71,6 +73,98 @@ 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, record + ): + client.force_login(user) + del record["imprints"][0]["date"] + response = client.post( + reverse("api:article-workflow-import-list"), + record, + 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, record + ): + client.force_login(user) + response = client.post( + reverse("api:article-workflow-import-list"), + record, + 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 record["imprints"][0]["date"] + response = client.post( + reverse("api:article-workflow-import-list"), + record, + 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, + record, + ): + client.force_login(user) + + response = client.post( + reverse("api:article-workflow-import-list"), + record, + 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" + ) + record["imprints"][0]["date"] = "2024-06-20" + response = client.post( + reverse("api:article-workflow-import-list"), + record, + 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 0a7f643dd..b6c2f6188 100644 --- a/scoap3/tasks.py +++ b/scoap3/tasks.py @@ -83,16 +83,19 @@ 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", ""), } + publication_date = data["imprints"][0].get("date") if ( article_data.get("id") and Article.objects.filter(pk=article_data["id"]).exists() ): article = Article.objects.get(pk=article_data["id"]) + + if publication_date: + article_data["publication_date"] = publication_date article.__dict__.update(**article_data) elif ( data.get("dois")[0].get("value") @@ -103,13 +106,16 @@ def _create_article(data, licenses): article = ArticleIdentifier.objects.get( identifier_type="DOI", identifier_value=data.get("dois")[0].get("value") ).article_id + if publication_date: + article_data["publication_date"] = publication_date article.__dict__.update(**article_data) 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() + print(article_data) return article