Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions coldfront/core/publication/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import bibtexparser.bibdatabase
import bibtexparser.bparser
import doi2bib
from django.test import TestCase

import coldfront.core.publication
Expand Down Expand Up @@ -153,8 +152,7 @@ def mock_get_bib(unique_id):
if unique_id == self._unique_id:
return sentinel.status, sentinel.bib_str

crossref = Mock(spec_set=doi2bib.crossref)
crossref.get_bib.side_effect = mock_get_bib
mocked_get_bib = Mock(side_effect=mock_get_bib)

def mock_parse(thing_to_parse):
# ensure bib_str from get_bib() is used
Expand All @@ -170,7 +168,7 @@ def mock_parse(thing_to_parse):
as_text = Mock(spec_set=bibtexparser.bibdatabase.as_text)
as_text.side_effect = lambda bib_entry: "as_text({})".format(bib_entry)

self.crossref = crossref
self.mocked_get_bib = mocked_get_bib
self.bibtexparser_cls = bibtexparser_cls
self.as_text = as_text

Expand All @@ -183,7 +181,7 @@ def dotpath(qualname):
with contextlib.ExitStack() as stack:
patches = [
patch(dotpath("BibTexParser"), new=self.bibtexparser_cls),
patch(dotpath("crossref"), new=self.crossref),
patch(dotpath("get_bib"), new=self.mocked_get_bib),
patch(dotpath("as_text"), new=self.as_text),
]
for p in patches:
Expand Down
17 changes: 17 additions & 0 deletions coldfront/core/publication/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-FileCopyrightText: (C) ColdFront Authors
#
# SPDX-License-Identifier: AGPL-3.0-or-later

import requests


def get_bib(doi_number):
api_url = "http://api.crossref.org/works/{}/transform/application/x-bibtex"
api_url = api_url.format(doi_number)
req = requests.get(api_url)
valid = True
if req.status_code != 200:
valid = False
bib_entry = str(req.content, encoding="utf-8")

return valid, bib_entry
6 changes: 3 additions & 3 deletions coldfront/core/publication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from django.urls import reverse
from django.views.generic import TemplateView, View
from django.views.generic.edit import FormView
from doi2bib import crossref

from coldfront.core.project.models import Project
from coldfront.core.publication.forms import (
Expand All @@ -29,6 +28,7 @@
PublicationSearchForm,
)
from coldfront.core.publication.models import Publication, PublicationSource
from coldfront.core.publication.utils import get_bib

MANUAL_SOURCE = "manual"

Expand Down Expand Up @@ -103,7 +103,7 @@ def _search_id(self, unique_id):
for source in PublicationSource.objects.all():
if source.name == "doi":
try:
status, bib_str = crossref.get_bib(unique_id)
status, bib_str = get_bib(unique_id)
bp = BibTexParser(interpolate_strings=False)
bib_database = bp.parse(bib_str)
bib_json = bib_database.entries[0]
Expand Down Expand Up @@ -486,7 +486,7 @@ def post(self, request, *args, **kwargs):
)
print("id is" + publication_obj.display_uid())
publication_obj.display_uid()
status, bib_str = crossref.get_bib(publication_obj.display_uid())
status, bib_str = get_bib(publication_obj.display_uid())
bp = BibTexParser(interpolate_strings=False)
bp.parse(bib_str)
bib_text += bib_str
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ classifiers = [
'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
]
dependencies = [
"bibtexparser==1.4.3",
"crispy-bootstrap4>=2024.10",
"django>4.2,<5",
"django-crispy-forms>=2.3",
Expand All @@ -36,13 +37,14 @@ dependencies = [
"django-split-settings>=1.3.2",
"django-su>=1.0.0",
"djangorestframework>=3.16.0",
"doi2bib>=0.4.0",
"fontawesome-free>=5.15.4",
"formencode>=2.1.1",
"gunicorn>=23.0.0",
"humanize>=4.12.2",
"python-dateutil>=2.9.0.post0",
"redis>=5.2.1",
"requests==2.32.3",
"urllib3==2.5.0",
]

[project.urls]
Expand Down
Loading