Skip to content

Commit

Permalink
Merge pull request #27 from acdh-oeaw/feature/citation
Browse files Browse the repository at this point in the history
Feature/citation
  • Loading branch information
gythaogg authored May 21, 2024
2 parents 4377251 + e9ff6f8 commit 501ee3d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
40 changes: 40 additions & 0 deletions apis_ontology/management/commands/fetch_citation_from_zotero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import json
import logging
import os

import pandas as pd
import requests
from apis_ontology.models import ZoteroEntry
from django.core.management.base import BaseCommand
from tqdm.auto import tqdm

USER = os.environ.get("APIS_BIBSONOMY_USER")
KEY = os.environ.get("APIS_BIBSONOMY_PASSWORD")
GROUP = "4394244"


QUERY_URL = f"https://api.zotero.org/groups/{GROUP}/items"
HEADERS = {"Authorization": f"Bearer {KEY}"}
PARAMS = {
"v": 3,
"format": "json",
# "itemKey": "Z5AUD7GQ,UJDXNMGD,KUYBGM7V,5DSBYG9K"
}

logger = logging.getLogger(__name__)

KEYS = ["Z5AUD7GQ", "UJDXNMGD", "KUYBGM7V", "5DSBYG9K"]


class Command(BaseCommand):
def handle(self, *args, **kwargs):
destination = "apis_ontology/static/citations.json"
citations = {}
for i, k in enumerate(KEYS):
res = requests.get(f"{QUERY_URL}/{k}", headers=HEADERS, params=PARAMS)
res.raise_for_status()
citations[f"Set {i + 1}"] = res.json()

with open(destination, "w") as f:
f.write(json.dumps(citations))
self.stdout.write(self.style.SUCCESS(f"Imported citations"))
1 change: 1 addition & 0 deletions apis_ontology/static/citations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Set 1": {"key": "Z5AUD7GQ", "version": 1488, "library": {"type": "group", "id": 4394244, "name": "TibSchol", "links": {"alternate": {"href": "https://www.zotero.org/groups/tibschol", "type": "text/html"}}}, "links": {"self": {"href": "https://api.zotero.org/groups/4394244/items/Z5AUD7GQ", "type": "application/json"}, "alternate": {"href": "https://www.zotero.org/groups/tibschol/items/Z5AUD7GQ", "type": "text/html"}}, "meta": {"createdByUser": {"id": 8397006, "username": "PascaleHugon", "name": "", "links": {"alternate": {"href": "https://www.zotero.org/pascalehugon", "type": "text/html"}}}, "creatorSummary": "dpal brtsegs bod yig dpe rnying zhib 'jug khang", "parsedDate": "2006", "numChildren": 1}, "data": {"key": "Z5AUD7GQ", "version": 1488, "itemType": "book", "title": "bKa\u02bc gdams gsung \u02bcbum phyogs bsgrigs thengs dang po", "creators": [{"creatorType": "editor", "firstName": "", "lastName": "dpal brtsegs bod yig dpe rnying zhib 'jug khang"}], "abstractNote": "", "series": "", "seriesNumber": "", "volume": "1-30", "numberOfVolumes": "30", "edition": "", "place": "Khreng tu'u", "publisher": "Si khron mi rigs dpe skrun khang", "date": "2006", "numPages": "", "language": "tib", "ISBN": "978-7-5409-32-49-X", "shortTitle": "", "url": "", "accessDate": "", "archive": "", "archiveLocation": "", "libraryCatalog": "", "callNumber": "", "rights": "", "extra": "", "tags": [{"tag": "APIS"}], "collections": ["IEP4UFSV"], "relations": {"dc:relation": "http://zotero.org/groups/4394244/items/S8ND2M27"}, "dateAdded": "2023-09-26T09:30:46Z", "dateModified": "2023-09-26T09:30:59Z"}}, "Set 2": {"key": "UJDXNMGD", "version": 1587, "library": {"type": "group", "id": 4394244, "name": "TibSchol", "links": {"alternate": {"href": "https://www.zotero.org/groups/tibschol", "type": "text/html"}}}, "links": {"self": {"href": "https://api.zotero.org/groups/4394244/items/UJDXNMGD", "type": "application/json"}, "alternate": {"href": "https://www.zotero.org/groups/tibschol/items/UJDXNMGD", "type": "text/html"}}, "meta": {"createdByUser": {"id": 8397006, "username": "PascaleHugon", "name": "", "links": {"alternate": {"href": "https://www.zotero.org/pascalehugon", "type": "text/html"}}}, "creatorSummary": "dpal brtsegs bod yig dpe rnying zhib 'jug khang", "parsedDate": "2007", "numChildren": 0}, "data": {"key": "UJDXNMGD", "version": 1587, "itemType": "book", "title": "bKa\u02bc gdams gsung \u02bcbum phyogs bsgrigs thengs gnyis pa", "creators": [{"creatorType": "editor", "firstName": "", "lastName": "dpal brtsegs bod yig dpe rnying zhib 'jug khang"}], "abstractNote": "", "series": "", "seriesNumber": "", "volume": "31-60", "numberOfVolumes": "30", "edition": "", "place": "Khreng tu'u", "publisher": "Si khron mi rigs dpe skrun khang", "date": "2007", "numPages": "", "language": "tib", "ISBN": "978-7-5409-3431-6 978-7-5409-4283-0", "shortTitle": "", "url": "", "accessDate": "", "archive": "", "archiveLocation": "", "libraryCatalog": "K10plus ISBN", "callNumber": "", "rights": "", "extra": "", "tags": [{"tag": "APIS"}], "collections": ["IEP4UFSV"], "relations": {"dc:relation": "http://zotero.org/groups/4394244/items/AEAX7W8D"}, "dateAdded": "2023-09-26T09:30:46Z", "dateModified": "2023-09-26T09:31:03Z"}}, "Set 3": {"key": "KUYBGM7V", "version": 1453, "library": {"type": "group", "id": 4394244, "name": "TibSchol", "links": {"alternate": {"href": "https://www.zotero.org/groups/tibschol", "type": "text/html"}}}, "links": {"self": {"href": "https://api.zotero.org/groups/4394244/items/KUYBGM7V", "type": "application/json"}, "alternate": {"href": "https://www.zotero.org/groups/tibschol/items/KUYBGM7V", "type": "text/html"}}, "meta": {"createdByUser": {"id": 8397006, "username": "PascaleHugon", "name": "", "links": {"alternate": {"href": "https://www.zotero.org/pascalehugon", "type": "text/html"}}}, "creatorSummary": "dpal brtsegs bod yig dpe rnying zhib 'jug khang", "parsedDate": "2009", "numChildren": 0}, "data": {"key": "KUYBGM7V", "version": 1453, "itemType": "book", "title": "bKa\u02bc gdams gsung \u02bcbum phyogs bsgrigs thengs gsum pa", "creators": [{"creatorType": "editor", "firstName": "", "lastName": "dpal brtsegs bod yig dpe rnying zhib 'jug khang"}], "abstractNote": "", "series": "", "seriesNumber": "", "volume": "61-90", "numberOfVolumes": "30", "edition": "", "place": "Khreng tu'u", "publisher": "Si khron mi rigs dpe skrun khang", "date": "2009", "numPages": "", "language": "tib", "ISBN": "978-7-5409-4283-0", "shortTitle": "", "url": "", "accessDate": "", "archive": "", "archiveLocation": "", "libraryCatalog": "", "callNumber": "", "rights": "", "extra": "", "tags": [{"tag": "APIS"}], "collections": ["IEP4UFSV"], "relations": {}, "dateAdded": "2023-09-26T09:30:46Z", "dateModified": "2023-09-26T09:31:09Z"}}, "Set 4": {"key": "5DSBYG9K", "version": 1450, "library": {"type": "group", "id": 4394244, "name": "TibSchol", "links": {"alternate": {"href": "https://www.zotero.org/groups/tibschol", "type": "text/html"}}}, "links": {"self": {"href": "https://api.zotero.org/groups/4394244/items/5DSBYG9K", "type": "application/json"}, "alternate": {"href": "https://www.zotero.org/groups/tibschol/items/5DSBYG9K", "type": "text/html"}}, "meta": {"createdByUser": {"id": 8397006, "username": "PascaleHugon", "name": "", "links": {"alternate": {"href": "https://www.zotero.org/pascalehugon", "type": "text/html"}}}, "creatorSummary": "dpal brtsegs bod yig dpe rnying zhib 'jug khang", "parsedDate": "2015", "numChildren": 0}, "data": {"key": "5DSBYG9K", "version": 1450, "itemType": "book", "title": "bKa\u02bc gdams gsung \u02bcbum phyogs bsgrigs thengs bzhi pa", "creators": [{"creatorType": "editor", "firstName": "", "lastName": "dpal brtsegs bod yig dpe rnying zhib 'jug khang"}], "abstractNote": "", "series": "", "seriesNumber": "", "volume": "91-120", "numberOfVolumes": "30", "edition": "", "place": "Khreng tu'u", "publisher": "Si khron mi rigs dpe skrun khang", "date": "2015", "numPages": "", "language": "tib", "ISBN": "978-7-5409-6135-0", "shortTitle": "", "url": "", "accessDate": "", "archive": "", "archiveLocation": "", "libraryCatalog": "", "callNumber": "", "rights": "", "extra": "", "tags": [{"tag": "APIS"}], "collections": ["IEP4UFSV"], "relations": {}, "dateAdded": "2023-09-26T09:30:46Z", "dateModified": "2023-09-26T09:30:54Z"}}}
11 changes: 11 additions & 0 deletions apis_ontology/templates/generic/partials/object_table.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{% load apisgeneric %}
{% load filter_utils %}
{% load parse_comment %}
{% load citation %}

<table class="table table-hover">
{% modeldict object as d %}

{% for key, value in d.items %}
{% if not key|endswith:'.rootobject_ptr' %}
{% if value %}

<tr>
<th>
{{ key.verbose_name | title}}
Expand All @@ -27,6 +31,13 @@
{% endif %}
</td>
</tr>
{% if key|endswith:'.pp_kdsb' %}
<tr>
<th> Citation</th>
<td>{{object|cite}}</td>
</tr>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
</table>
31 changes: 31 additions & 0 deletions apis_ontology/templatetags/citation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json
import logging
from django import template
from django.utils.safestring import mark_safe

register = template.Library()

logger = logging.getLogger(__name__)


with open("apis_ontology/static/citations.json", "r") as f:
citations = json.loads(f.read())


@register.filter
def cite(object):
print(object.set_num, object.pp_kdsb, object.volume)
print(citations[object.set_num])
zotero_info = citations[object.set_num]

title = zotero_info["data"]["title"]
key = zotero_info["key"]
creator = zotero_info["data"]["creators"][0]["lastName"]
constant = "[dPe sgrig 'gan 'khur ba: dByang can lha mo et al.], Chengdu"
place = zotero_info["data"]["place"]
publisher = zotero_info["data"]["publisher"]
date = zotero_info["data"]["date"]
link = f"""<a target="_BLANK" href="https://www.zotero.org/groups/4394244/tibschol/items/{key}/item-details#">{title}</a>"""
return mark_safe(
f"""{link}, vol. {object.volume}, {creator} {constant} [{place}]: {publisher}, {date}, pp. {object.pp_kdsb}"""
)

0 comments on commit 501ee3d

Please sign in to comment.