Skip to content

Commit

Permalink
article update
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestaP committed Feb 20, 2024
1 parent 386a986 commit 152ac68
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 34 deletions.
6 changes: 2 additions & 4 deletions scoap3/articles/tests/test_article_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def test_update_article_from_workflow(self, client, user, shared_datadir):
)

data["titles"][0]["title"] = "New title"
data["dois"].append({"value": "10.5506/APhysPolB.54.10-A5"})
response = client.post(
reverse("api:article-workflow-import-list"),
data,
Expand All @@ -73,8 +72,7 @@ def test_update_article_from_workflow(self, client, user, shared_datadir):
]

assert article.title == "New title"
assert len(expected_dois) == 2
assert "10.5506/APhysPolB.54.10-A5" in expected_dois
assert len(expected_dois) == 1
assert "10.5506/APhysPolB.54.10-A3" in expected_dois


Expand Down Expand Up @@ -120,4 +118,4 @@ def test_create_article_from_workflow(self):
data,
content_type="application/json",
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.status_code == status.HTTP_200_OK
51 changes: 21 additions & 30 deletions scoap3/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.core.exceptions import MultipleObjectsReturned, ValidationError
from django.core.files.storage import storages
from django.core.validators import URLValidator
from django.db import IntegrityError
from elasticsearch import ConnectionError, ConnectionTimeout, Elasticsearch
from sentry_sdk import capture_exception

Expand Down Expand Up @@ -77,20 +76,17 @@ def _create_article(data, licenses):
"subtitle": data["titles"][0].get("subtitle", ""),
"abstract": data["abstracts"][0].get("value", ""),
}
doi = article_data.get("dois")[0].get("value")
doi = data.get("dois")[0].get("value")
if (
doi
and ArticleIdentifier.objects.filter(
identifier_type="doi", identifier_value=doi
identifier_type="DOI", identifier_value=doi
).exists()
):
article = Article.objects.get(pk=article_data["id"])
article = ArticleIdentifier.objects.get(
identifier_type="DOI", identifier_value=doi
).article_id
article.__dict__.update(**article_data)
elif Article.objects.filter(**article_data).exists():
article_id = [
article.get("id") for article in Article.objects.filter(**article_data)
]
raise IntegrityError("The articles data already exists in DB:", article_id)
else:
article = Article.objects.create(**article_data)
article._created_at = data.get("_created") or data.get("record_creation_date")
Expand Down Expand Up @@ -118,15 +114,16 @@ def _create_article_identifier(data, article):
"identifier_value": doi.get("value"),
}
ArticleIdentifier.objects.get_or_create(**article_identifier_data)

for arxiv in data.get("arxiv_eprints", []):
article_identifier_data = {
"article_id": article,
"identifier_type": "arXiv",
"identifier_value": arxiv.get("value"),
}
doi = data.get("dois")[0].get("value")
if ArticleIdentifier.objects.filter(article_id=article).exists():
if ArticleIdentifier.objects.filter(
identifier_type="arXiv", article_id=article
).exists():
article_identifier = ArticleIdentifier.objects.get(
article_id=article, identifier_type="arXiv"
)
Expand All @@ -144,9 +141,7 @@ def _create_copyright(data, article):
"year": copyright.get("year"),
}
if Copyright.objects.filter(article_id=article).exists():
copyright = Copyright.objects.get(
article_id=article, identifier_type="arXiv"
)
copyright = Copyright.objects.get(article_id=article)
copyright.__dict__.update(**copyright_data)
else:
Copyright.objects.get_or_create(**copyright_data)
Expand All @@ -162,13 +157,15 @@ def _create_article_arxiv_category(data, article):
"category": arxiv_category,
"primary": True if idx == 0 else False,
}
if ArticleArxivCategory.objects.filter(article_id=article).exists():
article_arxiv_category = ArticleArxivCategory.objects.get(
article_id=article, identifier_type="arXiv"
)
article_arxiv_category.__dict__.update(**article_arxiv_category_data)
else:
ArticleArxivCategory.objects.get_or_create(**article_arxiv_category_data)
if ArticleArxivCategory.objects.filter(article_id=article).exists():
article_arxiv_category = ArticleArxivCategory.objects.get(
article_id=article
)
article_arxiv_category.__dict__.update(**article_arxiv_category_data)
else:
ArticleArxivCategory.objects.get_or_create(
**article_arxiv_category_data
)


def _create_publisher(data):
Expand Down Expand Up @@ -197,9 +194,7 @@ def _create_publication_info(data, article, publishers):
"publisher_id": publishers[idx].id,
}
if PublicationInfo.objects.filter(article_id=article).exists():
publication_info = PublicationInfo.objects.get(
article_id=article, identifier_type="arXiv"
)
publication_info = PublicationInfo.objects.get(article_id=article)
publication_info.__dict__.update(**publication_info_data)
else:
PublicationInfo.objects.get_or_create(**publication_info_data)
Expand Down Expand Up @@ -236,12 +231,8 @@ def _create_author(data, article):
"email": author.get("email", ""),
"author_order": idx,
}
if Author.objects.filter(article_id=article).exists():
author_ = Author.objects.get(article_id=article, identifier_type="arXiv")
author_.__dict__.update(**author_data)
else:
author_obj, _ = Author.objects.get_or_create(**author_data)
authors.append(author_obj)
author_obj, _ = Author.objects.get_or_create(**author_data)
authors.append(author_obj)
return authors


Expand Down

0 comments on commit 152ac68

Please sign in to comment.