diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-09-04 16:22:44 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-02-28 12:15:21 +0100 |
commit | de0dff09b8dff23dd50922ba5afe61abcefe5557 (patch) | |
tree | cdf909dfa35be53ca15e5391e4baa97e455b00ae /ishtar_common/models.py | |
parent | 7ca549609edbe1c833abd929a9552c1339b2e4cf (diff) | |
download | Ishtar-de0dff09b8dff23dd50922ba5afe61abcefe5557.tar.bz2 Ishtar-de0dff09b8dff23dd50922ba5afe61abcefe5557.zip |
Documents: COinS and Dublin Core tags
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index a5e8e6341..df9fe0c20 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -43,6 +43,8 @@ from ooopy.Transformer import Transformer as OOTransformer import ooopy.Transforms as OOTransforms import uuid import zipfile +from urllib.parse import urlencode +from xml.etree import ElementTree as ET from django import forms from django.apps import apps @@ -4616,6 +4618,10 @@ class Person(Address, Merge, OwnPerms, ValueGetter, MainItem): return (self.uuid,) @property + def first_name(self): + return self.surname + + @property def full_title(self): return " ".join( [str(getattr(self, attr)) @@ -6014,6 +6020,155 @@ class Document(BaseHistorizedItem, QRCodeItem, OwnPerms, ImageModel, def import_get_next_index(cls, context, value): context["index"] = cls.get_next_index() + @property + def dublin_core_identifier(self): + identifier = None + if self.isbn: + identifier = self.isbn + elif self.issn: + identifier = self.issn + elif self.associated_url: + identifier = self.associated_url + elif Site.objects.count(): + identifier = "http://{}{}".format( + Site.objects.all()[0].domain, self.get_absolute_url()) + return identifier + + def dublin_core_tags(self): + if not self.title: + return "" + tags = [ + ("link", + {"rel": "schema.DC", "href": "http://purl.org/dc/elements/1.1/"}), + ("link", + {"rel": "schema.DCTERMS", "href": "http://purl.org/dc/terms/"}), + ] + title = {"name": "DC.title", "content": self.title} + tags.append(("meta", title)) + if self.creation_date: + date = {"name": "DC.date", "scheme": "DCTERMS.W3CDTF", + "content": self.creation_date.strftime("%Y-%m-%d")} + tags.append(("meta", date)) + if self.tags.count(): + content = ", ".join([str(t) for t in self.tags.all()]) + tg = {"name": "DC.subject", "content": content} + tags.append(("meta", tg)) + if self.description: + tags.append( + ("meta", {"name": "DC.description", + "content": self.description})) + if self.publisher: + tags.append( + ("meta", {"name": "DC.publisher", + "content": self.publisher.name})) + if self.authors.count(): + content = ", ".join([str(t.person.raw_name) for t in + self.authors.all()]) + tags.append( + ("meta", {"name": "DC.creator", + "content": content})) + if self.source_type: + tags.append( + ("meta", {"name": "DC.type", + "content": str(self.source_type)})) + if self.format_type: + tags.append( + ("meta", {"name": "DC.format", + "content": str(self.format_type)})) + identifier = self.dublin_core_identifier + if identifier: + tags.append( + ("meta", {"name": "DC.identifier", + "content": identifier})) + if self.language: + lang = self.language.iso_code + tags.append( + ("meta", {"name": "DC.language", + "content": lang})) + if self.licenses.count(): + licences = ", ".join([str(l) for l in self.licenses.all()]) + tags.append( + ("meta", {"name": "DC.rights", + "content": licences})) + src = None + if self.source: + src = self.source.dublin_core_identifier + if src: + tags.append( + ("meta", {"name": "DC.relation", + "content": src})) + tags.append( + ("meta", {"name": "DC.source", + "content": src})) + elif self.source_free_input: + tags.append( + ("meta", {"name": "DC.source", + "content": self.source_free_input})) + html = "" + for tag, attrs in tags: + et = ET.Element(tag, attrib=attrs) + html += ET.tostring(et, encoding="unicode", method="html") + return html + + @property + def extra_meta(self): + return self.dublin_core_tags() + + def coins_tag(self): + if not self.title: + return + info = [ + ("url_ver", "Z39.88-2004"), + ("ctx_ver", "Z39.88-2004"), + ("rft_val_fmt", "info:ofi/fmt:kev:mtx:dc"), + ("rft.title", self.title), + ("rft.btitle", self.title) + ] + if self.associated_url: + info.append(("rft.identifier", self.associated_url)) + elif Site.objects.count(): + info.append(("rft.identifier", "http://{}{}".format( + Site.objects.all()[0].domain, self.get_absolute_url()))) + for author in self.authors.all(): + person = author.person + if not person.raw_name: + continue + if person.first_name and person.name: + info.append(("rft.aulast", person.name)) + info.append(("rft.aufirst", person.first_name)) + info.append(("rft.au", "{}+{}".format(person.first_name, + person.name))) + else: + info.append(("rft.au", person.raw_name)) + if self.source_type: + info.append(("rft.type", self.source_type.coins_type)) + if self.source_type.coins_genre: + info.append(("rft.genre", self.source_type.coins_genre)) + if self.description: + info.append(("rft.description", self.description)) + if self.publisher: + info.append(("rft.publisher", self.publisher.name)) + if self.creation_date: + if self.creation_date.day == 1 and self.creation_date.month == 1: + info.append(("rft.date", self.creation_date.year)) + else: + info.append(("rft.date", + self.creation_date.strftime("%Y-%m-%d"))) + if self.source and self.source.title: + info.append(("rft.source", self.source.title)) + elif self.source_free_input: + info.append(("rft.source", self.source_free_input)) + if self.issn: + info.append(("rft.issn", self.issn)) + if self.isbn: + info.append(("rft.isbn", self.isbn)) + if self.licenses.count(): + licenses = ";".join([str(l) for l in self.licenses.all()]) + info.append(("rft.rights", licenses)) + if self.language: + info.append(("rft.language", self.language.iso_code)) + return '<span class="Z3988" title="{}">'.format(urlencode(info)) + def save(self, *args, **kwargs): no_path_change = 'no_path_change' in kwargs \ and kwargs.pop('no_path_change') |