Skip to content

Commit

Permalink
feat: move bibtex fetching to utils
Browse files Browse the repository at this point in the history
The logic to fill the bibtex data is now part of the `utils` module and
can therefore be used in the `save()` method of the model as well as in
other places, like the api_views.py module.
The logic was copied from the api_views.SaveBibsonomyEntry.post() method
and refactored a bit. If there is any kind of error, the method simply
returns `None`.
  • Loading branch information
b1rger committed May 22, 2023
1 parent d6ed3c2 commit b0af808
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 22 deletions.
24 changes: 3 additions & 21 deletions apis_bibsonomy/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rest_framework.views import APIView

from .models import Reference
from .utils import BibsonomyEntry
from .utils import BibsonomyEntry, get_bibtex_from_url


class SaveBibsonomyEntry(APIView):
Expand All @@ -37,29 +37,11 @@ def post(self, request, format=None):
pages_end = request.data.get("pages_end", None)
folio = request.data.get("folio", None)
notes = request.data.get("notes", None)
kind = None
sett = getattr(settings, "APIS_BIBSONOMY", [])
for s in sett:
if "url" in s.keys():
if s["url"] in bib_ref:
sett1 = s
kind = s["type"]
if bib_ref is not None:
r = {"bibs_url": bib_ref}
if kind == "bibsonomy":
bib_e = BibsonomyEntry(bib_hash=bib_ref, base_set=sett1)
r["bibtex"] = json.dumps(bib_e.bibtex)
elif kind == "zotero":
for s in sett:
if s["type"] == "zotero":
headers = {
"Zotero-API-Key": s["API key"],
"Zotero-API-Version": "3",
}
params = {"include": "csljson"}
res = requests.get(bib_ref, headers=headers, params=params)
r["bibtex"] = json.dumps(res.json()["csljson"])
else:
r["bibtex"] = get_bibtex_from_url(bib_ref)
if r["bibtex"] is None:
m = {"message": "You need to select a publication."}
return Response(data=m, status=status.HTTP_400_BAD_REQUEST)
if obj_id is not None:
Expand Down
22 changes: 21 additions & 1 deletion apis_bibsonomy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from django.urls import reverse
from django.conf import settings
from django.db.models import Q
from .utils import BibsonomyEntry, get_bibtex_from_url
from django.conf import settings
import requests

import json


class Reference(models.Model):
"""Model that holds the reference to a bibsonomy entry"""

Expand Down Expand Up @@ -51,3 +53,21 @@ def similar_references(self, with_self=False):
if not with_self:
return Reference.objects.exclude(pk=self.pk).filter(similarity)
return Reference.objects.filter(similarity)



def save(
self, force_insert=False, force_update=False, using=None, update_fields=None
):

if update_fields is not None and "bibtex" in update_fields:
self.bibtex = None
if self.bibtex is None and self.bibs_url is not None:
self.bibtex = get_bibtex_from_url(self.bibs_url)

super().save(
force_insert=force_insert,
force_update=force_update,
using=using,
update_fields=update_fields,
)
24 changes: 24 additions & 0 deletions apis_bibsonomy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from requests.auth import HTTPBasicAuth
from django.conf import settings
import json
import logging
logger = logging.getLogger(__name__)


class BibsonomyEntry:
Expand Down Expand Up @@ -51,3 +53,25 @@ def get_autocomplete(self):
return res_str
html = property(get_html)
autocomplete = property(get_autocomplete)

def get_bibtex_from_url(url):
sources = getattr(settings, "APIS_BIBSONOMY", [])
sources = [s for s in sources if s.get("url") and s.get("url") in url]
source = sources[-1]
btype = source.get("type", None)
bibtex = None
try:
if btype == "bibsonomy":
bib_e = BibsonomyEntry(bib_hash=url, base_set=source)
bibtex = json.dumps(bib_e.bibtex)
elif btype == "zotero":
headers = {
"Zotero-API-Key": source.get("API key", None),
"Zotero-API-Version": "3",
}
params = {"include": "csljson"}
res = requests.get(url, headers=headers, params=params)
bibtex = json.dumps(res.json()["csljson"])
except Exception as e:
logger.warning(f"Could not fetch bibtex from {url}: {e}")
return bibtex

0 comments on commit b0af808

Please sign in to comment.