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 committed Jul 18, 2024
1 parent 27dfb1e commit 4bb06a7
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 12 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
114 changes: 104 additions & 10 deletions scoap3/articles/tests/test_article_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down
10 changes: 8 additions & 2 deletions scoap3/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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


Expand Down

0 comments on commit 4bb06a7

Please sign in to comment.