No images in this repository’s iceberg at this time
Download raw (11.3 KB)
import urlparse import xml.sax.saxutils import urllib import os.path import datetime from django.template.defaultfilters import stringfilter from django import template from django.utils.safestring import mark_safe from django.core.urlresolvers import reverse from aacore.models import Namespace from aacore import app, NS import rdflib register = template.Library() @register.filter def browseurl(node): """ Reverses the browse url of a resource or a litteral. """ try: return reverse('aa-browse') + "?" + urllib.urlencode({'node': node.encode("utf-8")}) except: pass @register.filter def browseurl3(node): """ Reverses the browse url of a resource or a litteral. """ try: return reverse('aa-browse3') + "?" + urllib.urlencode({'node': node.encode("utf-8")}) except: pass @register.filter def rdfnodedisplay(node): if node.is_resource(): uri = rdfnode(node) link = reverse('aacore.views.browse') + "?" + urllib.urlencode({'node': uri}) return mark_safe('<a href="%s">%s</a>' % (link, compacturl(uri))) elif node.is_literal(): return rdfnode(node) elif node.is_blank(): return "blank" + node.blank_identifier else: return node @register.filter def rdfbrowselink(node): """ filter by aa-resource """ if node.is_resource(): uri = rdfnode(node) return mark_safe('<a href="%s" title="%s">%s</a>' % (browseurl(uri), uri, compacturl(uri))) elif node.is_literal(): literal = rdfnode(node) link = reverse('aa-browse') + "?" + urllib.urlencode({'node': literal}) return mark_safe('<a href="%s">%s</a>' % (link, literal)) elif node.is_blank(): link = reverse('aa-browse') + "?" + urllib.urlencode({'node': "blank:" + node.blank_identifier}) return mark_safe('<a href="%s">%s</a>' % (link, "blank" + node.blank_identifier)) else: return node @register.filter def rdfrellink(node): """ filter by aa-resource """ if node.is_resource(): uri = rdfnode(node) link = reverse('aa-browse') + "?" + urllib.urlencode({'node': uri}) return mark_safe('<a href="%s" title="%s">%s</a>' % (link, uri, compacturl(uri))) elif node.is_literal(): literal = rdfnode(node) link = reverse('aa-browse') + "?" + urllib.urlencode({'node': literal}) return mark_safe('<a href="%s">%s</a>' % (link, literal)) elif node.is_blank(): link = reverse('aa-browse') + "?" + urllib.urlencode({'node': "blank:" + node.blank_identifier}) return mark_safe('<a href="%s">%s</a>' % (link, "blank" + node.blank_identifier)) else: return node @register.filter def rdfviewslink(node): """ filter used in aacore.views.browse (debug) """ if node.is_resource(): uri = rdfnode(node) link = reverse('aa-rdf-browse') + "?" + urllib.urlencode({'node': uri}) return mark_safe('<a href="%s">%s</a>' % (link, compacturl(uri))) elif node.is_literal(): literal = rdfnode(node) link = reverse('aa-rdf-browse') + "?" + urllib.urlencode({'node': literal}) return mark_safe('<a href="%s">%s</a>' % (link, literal)) elif node.is_blank(): link = reverse('aa-rdf-browse') + "?" + urllib.urlencode({'node': "blank:" + node.blank_identifier}) return mark_safe('<a href="%s">%s</a>' % (link, "blank" + node.blank_identifier)) else: return node @register.filter @stringfilter def compacturl(url): # url = rdfnode(url) for ns in Namespace.objects.all(): if url.startswith(ns.url): return ns.name + ":" + url[len(ns.url):] return url @register.filter @stringfilter def namespace_for_url(url): url = rdfnode(url) for ns in Namespace.objects.all(): if url.startswith(ns.url): return ns.name return url @register.filter @stringfilter def url_filename(url): parts = urlparse.urlparse(url) if parts.path: d, p = os.path.split(parts.path.rstrip('/')) if p: return p else: return d else: return url @register.filter @stringfilter def url_hostname(url): """ Returns the hostname of the given URL Usage format:: <dl> <dt>Host name</dt> <dd property="http:hostname">{{ resource.url|url_hostname }}</dd> </dl> """ return urlparse.urlparse(url).netloc @register.filter @stringfilter def xmlescape(src): return xml.sax.saxutils.escape(src) @register.filter def iso8601_date(date): """ Renders the given datetime object to its iso8601 representations """ t = type(date) if t == datetime.datetime: return date.date().isoformat() elif t == datetime.date: return date.isoformat() else: return date # literal = unicode(rdfnode(node)).encode('utf-8') # literal = rdfnode(node) # # FIXME: urlencode does not seem to appreciate Arabic: # UnicodeEncodeError at /browse/ with URL <http://www.youtube.com/watch?v=YeoF74Vu180> @register.filter def dashify(value): """ Substitues underscores with dashes """ return value.replace("_", "-") @register.inclusion_tag('aacore/partials/infobox.html') def infobox(node, kind="work"): if kind == "work": infos = app.graph.query("""\ SELECT DISTINCT ?creator ?creatorName ?title ?date ?description WHERE { OPTIONAL { ?subject purl:description ?description . } ?subject purl:creator ?creator. ?creator foaf:name ?creatorName . ?subject purl:title ?title . }""", initNs=NS, initBindings={'subject': node}) ret = {} ret["kind"] = "work" nsstats = rdflib.Namespace('http://kavan.land/vocab/stats#') # word count res = next(app.graph.triples((node, nsstats.hasWordCount, None)), None) ret["wc"] = res[2] if res else None # uniq word count res = next(app.graph.triples((node, nsstats.hasUniqueWordCount, None)), None) ret["uwc"] = res[2] if res else None # diversity indice res = next(app.graph.triples((node, nsstats.hasDiversityIndice, None)), None) ret["div"] = res[2] if res else None # character count res = next(app.graph.triples((node, nsstats.hasCharacterCount, None)), None) ret["cc"] = res[2] if res else None alt_node = rdflib.URIRef(u"%s" % node.rstrip("/")) res = app.graph.query("""\ SELECT DISTINCT ?edition_date WHERE { ?edition wemi:workManifested ?subject . ?edition purl:issued ?edition_date } ORDER BY ?edition_date """, initNs=NS, initBindings={'subject': alt_node}) res = [i for i in res] # FIXME: the open library RDF does not sign the edition date as an integer/date ret['first_published'] = int(res[0][0]) if res else None ret['last_edition'] = int(res[-1][0]) if res else None ret['edition_nbr'] = len(res) if res else None perso_list = app.graph.query("""\ SELECT DISTINCT ?perso WHERE { ?subject stats:hasCharacter ?perso . }""", initNs=NS, initBindings={'subject': node}) ret["perso_list"] = perso_list # print(perso_list) bigram_list = app.graph.query("""\ SELECT DISTINCT ?bigram WHERE { ?subject stats:bigram ?bigram . }""", initNs=NS, initBindings={'subject': node}) ret["bigram_list"] = bigram_list # print(bigram_list) coverage_list = app.graph.query("""\ SELECT DISTINCT ?coverage WHERE { ?subject purl:coverage ?coverage . }""", initNs=NS, initBindings={'subject': node}) ret["coverage_list"] = coverage_list subject_list = app.graph.query("""\ SELECT DISTINCT ?subjects WHERE { ?subject purl:subject ?subjects . }""", initNs=NS, initBindings={'subject': node}) ret["subject_list"] = subject_list noeud = rdflib.term.URIRef(u"%s" % node.rstrip("/")) edition_list = app.graph.query("""\ SELECT DISTINCT ?edition ?edition_title ?edition_date WHERE { ?edition wemi:workManifested ?subject ; purl:title ?edition_title . OPTIONAL { ?edition purl:issued ?edition_date } } GROUP BY ?edition """, initNs=NS, initBindings={'subject': noeud}) ret["edition_list"] = edition_list # query = 'SELECT ?s WHERE { ?s wemi:workManifested <http://openlibrary.org/works/OL11334034W> . }' elif kind == "edition": infos = app.graph.query("""\ SELECT DISTINCT ?work ?workTitle ?title ?publisher ?language ?placeOfPublication ?issued ?isbn10 ?isbn13 ?extent ?dimensions ?date ?description ?note WHERE { OPTIONAL { ?subject purl:publisher ?publisher . } OPTIONAL { ?subject purl:language ?language . } OPTIONAL { ?subject rdvocab:placeOfPublication ?placeOfPublication . } OPTIONAL { ?subject purl:issued ?issued . } OPTIONAL { ?subject bibo:isbn10 ?isbn10 . } OPTIONAL { ?subject bibo:isbn13 ?isbn13 . } OPTIONAL { ?subject purl:extent ?extent . } OPTIONAL { ?subject rdvocab:dimensions ?dimensions . } OPTIONAL { ?subject purl:date ?date . } OPTIONAL { ?subject purl:description ?description . } OPTIONAL { ?subject rdvocab:note ?note . } ?subject wemi:workManifested ?work; purl:title ?workTitle . ?subject purl:title ?title . }""", initNs=NS, initBindings={'subject': node}) ret = {} ret["kind"] = "edition" elif kind == "zotero": # print("zotero") infos = app.graph.query("""\ SELECT DISTINCT ?subject ?zItemType ?title ?date ?ztype ?subjects ?submitted ?abstract ?author ?language ?isPartOf ?url WHERE { ?subject z:itemType ?zItemType . ?subject dc:title ?title . OPTIONAL { ?subject dc:date ?date . } OPTIONAL { ?subject z:type ?ztype . } OPTIONAL { ?subject dc:subject ?subjects . } OPTIONAL { ?subject dcterms:dateSubmitted ?submitted . } OPTIONAL { ?subject dcterms:abstract ?abstract . } OPTIONAL { ?subject z:language ?language . } OPTIONAL { ?subject bib:authors ?seq_a . } OPTIONAL { ?seq_a ?seq_index ?seq_anode . } OPTIONAL { ?seq_anode foaf:surname ?author . } OPTIONAL { ?subject dcterms:isPartOf ?isPart . } OPTIONAL { ?isPart dc:title ?isPartOf . } OPTIONAL { ?subject dc:identifier ?identifier . } OPTIONAL { ?identifier dcterms:URI ?uri . } OPTIONAL { ?uri rdf:value ?url . } }""", initNs=NS, initBindings={'subject': node}) ret = {} ret["kind"] = "zotero" if len(infos.bindings): for key, value in infos.bindings[0].iteritems(): ret[str(key)] = value return ret