Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(autocompletes): small refactoring of BibsonomyAutocomplete #119

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
46 changes: 21 additions & 25 deletions apis_bibsonomy/autocompletes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests
from requests.auth import HTTPBasicAuth
import json

from django import http
from dal import autocomplete
Expand Down Expand Up @@ -49,6 +48,10 @@ def query_zotero(q, conf, page_size=20, offset=0):


class BibsonomyAutocomplete(autocomplete.Select2ListView):
def __init__(self, page_size=20, group=None, user=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.page_size = page_size

def get(self, request, *args, **kwargs):
choices = []
offset = (int(self.request.GET.get("page", 1)) - 1) * self.page_size
Expand All @@ -57,31 +60,24 @@ def get(self, request, *args, **kwargs):
if len(self.q) < 3:
choices = []
else:
for idx, c in enumerate(self.conf):
if c["type"] == "bibsonomy":
r = query_bibsonomy(q, c, self.page_size, offset)
res = r.json()
more = res["posts"].get("next", False)
if "post" in res["posts"].keys():
for r in res["posts"]["post"]:
for config in getattr(settings, "APIS_BIBSONOMY", []):
match config.get("type", None):
case "bibsonomy":
response = query_bibsonomy(q, config, self.page_size, offset)
data = response.json()
for r in data["posts"].get("post", []):
ent = BibsonomyEntry(bib_entry={"post": r})
choices.append(
{"id": ent.bib_hash, "text": ent.autocomplete}
)
elif c["type"] == "zotero":
res = query_zotero(q, c, self.page_size, offset)
if int(res.headers["Total-Results"]) > (self.page_size + offset):
more = True
for r in res.json():
choices.append(
{"id": r["links"]["self"]["href"], "text": r["bib"]}
)
return http.HttpResponse(
json.dumps({"results": choices + [], "pagination": {"more": more}}),
content_type="application/json",
)

def __init__(self, page_size=20, group=None, user=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.page_size = page_size
self.conf = getattr(settings, "APIS_BIBSONOMY", None)
more = data["posts"].get("next", False)
case "zotero":
res = query_zotero(q, config, self.page_size, offset)
total_results = res.headers["Total-Results"]
more = int(total_results) > (self.page_size + offset)
for result in res.json():
id = result["links"]["self"]["href"]
text = result["bib"]
choices.append({"id": id, "text": text})
response = {"results": choices + [], "pagination": {"more": more}}
return http.JsonResponse(response)
Loading