Skip to content

Commit

Permalink
Create article: add real date_published
Browse files Browse the repository at this point in the history
* Set  to  if  is None in the serializer.
* ref: cern-sis/issues-scoap3#338
  • Loading branch information
ErnestaP authored and pamfilos committed Jul 24, 2024
1 parent 91f1f30 commit ef31bf9
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
6 changes: 6 additions & 0 deletions scoap3/articles/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
100 changes: 100 additions & 0 deletions scoap3/articles/tests/test_article_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 7 additions & 2 deletions scoap3/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""),
Expand All @@ -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"):
Expand All @@ -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
Expand Down

0 comments on commit ef31bf9

Please sign in to comment.