fill labels with sparql request on the go
authorcavaliet
Mon, 02 Sep 2013 16:26:21 +0200
changeset 17 b31a67614f76
parent 15 be595253ced5
child 18 88d675d5e27e
fill labels with sparql request on the go
src/p4l/models/data.py
src/p4l/templates/p4l/p4l_home.html
src/p4l/templates/p4l/p4l_record_view.html
src/p4l/utils.py
src/p4l/views.py
--- a/src/p4l/models/data.py	Mon Sep 02 11:45:01 2013 +0200
+++ b/src/p4l/models/data.py	Mon Sep 02 16:26:21 2013 +0200
@@ -20,7 +20,7 @@
 class ProjectName(P4lModel):
     uri = models.URLField(max_length=2048, unique=True, db_index=True) 
     label = models.CharField(max_length=2048, blank=False, null=False, db_index=True)
-    acronym = models.CharField(max_length=2048, blank=True, null=True, db_index=True) #iiep:acronym
+    acronym = models.CharField(max_length=2048, blank=True, null=True, db_index=True) #iiep:acronym        # 
     class Meta(P4lModel.Meta):
         unique_together = ['label', 'acronym']
 
@@ -139,16 +139,16 @@
 
 class Record(P4lModel):
     uri = models.URLField(max_length=2048, unique=True, db_index=True) #subject
-    subjects = models.ManyToManyField('p4l.Subject') #dct:subject                                                       # Thesaurus with no country
-    themes = models.ManyToManyField('p4l.Theme') #iiep:theme                                                            # Theme
-    countries = models.ManyToManyField('p4l.Country') #iiep:country                                                     # Thesaurus filtered with country only
+    subjects = models.ManyToManyField('p4l.Subject') #dct:subject                                                       # <Thesaurus> with no country
+    themes = models.ManyToManyField('p4l.Theme') #iiep:theme                                                            # <Themes>
+    countries = models.ManyToManyField('p4l.Country') #iiep:country                                                     # <Thesaurus> filtered with country only
     identifier = models.CharField(max_length=128, unique=True, db_index=True) #dct:identifier    
     notes = models.TextField(blank=True, null=True) #iiep:notes    
     #issns foreign key from Isbn #iiep:issn
     #isbns foreign key from Isbn #iiep:isbn
     #documentCodes foreign key from Isbn #iiep:documentCode
-    language = models.ForeignKey('p4l.Language', blank=True, null=True) #dct:language                                    # Language
-    otherLanguages = models.ManyToManyField('p4l.Language', related_name='otherLanguage_record') #iiep:otherLanguage     # Language
+    language = models.ForeignKey('p4l.Language', blank=True, null=True) #dct:language                                    # <Languages>
+    otherLanguages = models.ManyToManyField('p4l.Language', related_name='otherLanguage_record') #iiep:otherLanguage     # <Languages>
     #titles foreign Key from Title #dct:title
     #abstracts foreign Key from Abstract #dct:abstract
     #addedTitles foreign Key from AddedTitle #iiep:addedTitle
@@ -157,15 +157,15 @@
     #imprints foreign Key from Imprint #iiep:imprint
     #collations = foreign Key from Collation #iiep:collation
     #volumeIssues = foreign Key from VolumeIssue #iiep:volumeIssue
-    projectNames = models.ManyToManyField('p4l.ProjectName') #iiep:projectName
+    projectNames = models.ManyToManyField('p4l.ProjectName') #iiep:projectName                                            # <Projects>
     #periodicals = foreign key from 'p4l.Periodical' #iiep:periodical
     #meetings = foreign key from 'p4l.Meeting' #iiep:meeting
     #series = foreign key from 'p4l.Serie'  #iiep:serie
     #authors = foreign key from 'p4l.Author' #iiep:author
     #subjectPersons = foreign key from 'p4l.SubjectPerson' #iiep:subjectPerson
-    subjectCorporateBodies = models.ManyToManyField('p4l.CorporateAuthor', related_name='recordsSubjectCorporateBody') #iiep:subjectCorporateBody
+    subjectCorporateBodies = models.ManyToManyField('p4l.CorporateAuthor', related_name='recordsSubjectCorporateBody') #iiep:subjectCorporateBody # <Organizations>
     #subjectMeetings = foreign key from 'p4l.SubjectMeeting' #iiep:subjectMeeting
-    corporateAuthors = models.ManyToManyField('p4l.CorporateAuthor', related_name='recordsCorporateAuthor') #iiep:subjectCorporateBody
+    corporateAuthors = models.ManyToManyField('p4l.CorporateAuthor', related_name='recordsCorporateAuthor') #iiep:subjectCorporateBody # <Organizations>
     #urls foreign Key from Url #iiep:url
     recordType = models.URLField(max_length=2048) #dct:type
     
--- a/src/p4l/templates/p4l/p4l_home.html	Mon Sep 02 11:45:01 2013 +0200
+++ b/src/p4l/templates/p4l/p4l_home.html	Mon Sep 02 16:26:21 2013 +0200
@@ -42,7 +42,7 @@
   {% for record in object_list %}
     <tr>
       <td title="{{ record.uri }}">{{ record.uri|reduce:"4,13" }}</td>
-      <td title="{{ record.language.language }}">{{ record.language.language|reduce:"0,3" }}</td>
+      <td title="{{ record.language.uri }}">{{ record.language.uri|reduce:"0,3" }}</td>
       <td><ul>{% for t in record.titles.all %}
           <li>{{ t.title }}</li>
           {% endfor %}</ul></td>
--- a/src/p4l/templates/p4l/p4l_record_view.html	Mon Sep 02 11:45:01 2013 +0200
+++ b/src/p4l/templates/p4l/p4l_record_view.html	Mon Sep 02 16:26:21 2013 +0200
@@ -17,15 +17,15 @@
     </tr>
     <tr>
       <td>{% trans 'subjects' %}</td>
-      <td><ul>{% for i in record.subjects.all %}<li>{{ i.label|default:'' }} <small class="text-muted">({{ i.subject|default:'' }})</small></li>{% endfor %}</ul></td>
+      <td><ul>{% for k,v in subjects_labels.items %}<li>{{ v }} <small class="text-muted">({{ k }})</small></li>{% endfor %}</ul></td>
     </tr>
     <tr>
       <td>{% trans 'themes' %}</td>
-      <td><ul>{% for i in record.themes.all %}<li>{{ i.label|default:'' }} <small class="text-muted">({{ i.theme|default:'' }})</small></li>{% endfor %}</ul></td>
+      <td><ul>{% for k,v in themes_labels.items %}<li>{{ v }} <small class="text-muted">({{ k }})</small></li>{% endfor %}</ul></td>
     </tr>
     <tr>
       <td>{% trans 'countries' %}</td>
-      <td><ul>{% for i in record.countries.all %}<li>{{ i.label|default:'' }} <small class="text-muted">({{ i.country|default:'' }})</small></li>{% endfor %}</ul></td>
+      <td><ul>{% for k,v in countries_labels.items %}<li>{{ v }} <small class="text-muted">({{ k }})</small></li>{% endfor %}</ul></td>
     </tr>
     <tr>
       <td>{% trans 'identifier' %}</td>
@@ -49,11 +49,11 @@
     </tr>
     <tr>
       <td>{% trans 'language' %}</td>
-      <td>{{ record.language.label|default:'' }} <small class="text-muted">({{ record.language.language|default:'' }})</small></td>
+      <td>{% if record.language %}{{ language_label }} <small class="text-muted">({{ record.language.uri|default:'' }})</small>{% endif %}</td>
     </tr>
     <tr>
       <td>{% trans 'other languages' %}</td>
-      <td><ul>{% for i in record.otherLanguages.all %}<li>{{ i.language|default:'' }}</li>{% endfor %}</ul></td>
+      <td><ul>{% for k,v in otherLanguages_labels.items %}<li>{{ v }} <small class="text-muted">({{ k }})</small></li>{% endfor %}</ul></td>
     </tr>
     <tr>
       <td>{% trans 'titles' %}</td>
@@ -108,7 +108,7 @@
     </tr>
     <tr>
       <td>{% trans 'project names' %}</td>
-      <td><ul>{% for i in record.projectNames.all %}<li>{% if i.acronym %}{{ i.acronym }} : {% endif %}{{ i.label|default:'' }}</li>{% endfor %}</ul></td>
+      <td><ul>{% for k,v in projects_labels.items %}<li>{{ v }} <small class="text-muted">({{ k }})</small></li>{% endfor %}</ul></td>
     </tr>
     <tr>
       <td>{% trans 'periodicals' %}</td>{# ajouter langue #}
@@ -157,7 +157,7 @@
     </tr>
     <tr>
       <td>{% trans 'subject corporate bodies' %}</td>
-      <td><ul>{% for i in record.subjectCorporateBodies.all %}<li>{% if i.acronym %}{{ i.acronym }} : {% endif %}{{ i.label|default:'' }}</li>{% endfor %}</ul></td>
+      <td><ul>{% for k,v in subjectCorporateBodies_labels.items %}<li>{{ v }} <small class="text-muted">({{ k }})</small></li>{% endfor %}</ul></td>
     </tr>
     <tr>
       <td>{% trans 'subject meetings' %}</td>{# ajouter langue #}
@@ -179,7 +179,7 @@
     </tr>
     <tr>
       <td>{% trans 'corporate authors' %}</td>
-      <td><ul>{% for i in record.corporateAuthors.all %}<li>{% if i.acronym %}{{ i.acronym }} : {% endif %}{{ i.label|default:'' }}</li>{% endfor %}</ul></td>
+      <td><ul>{% for k,v in corporateAuthors_labels.items %}<li>{{ v }} <small class="text-muted">({{ k }})</small></li>{% endfor %}</ul></td>
     </tr>
     <tr>
       <td>{% trans 'Urls' %}</td>{# ajouter langue #}
@@ -199,12 +199,16 @@
     </tr>
     <tr>
       <td>{% trans 'record type' %}</td>
-      <td>{{ record.recordType|default:'' }}</td>
+      <td>{{ recordType_label }} <small class="text-muted">({{ record.recordType|default:'' }})</small></td>
     </tr>
     <tr>
       <td>{% trans 'is document part' %}</td>
       <td><input type="checkbox" disabled="disabled" {% if record.isDocumentPart %}checked="checked"{% endif %} /></td>
     </tr>
+    <tr>
+      <td>{% trans 'is multilingual' %}</td>
+      <td><input type="checkbox" disabled="disabled" {% if record.isMultilingual %}checked="checked"{% endif %} /></td>
+    </tr>
   </tbody>
   <!--
     uri = models.URLField(max_length=2048, unique=True, db_index=True) #subject
--- a/src/p4l/utils.py	Mon Sep 02 11:45:01 2013 +0200
+++ b/src/p4l/utils.py	Mon Sep 02 16:26:21 2013 +0200
@@ -4,6 +4,10 @@
 import codecs #@UnresolvedImport
 import math
 import requests #@UnresolvedImport
+import logging
+
+
+logger = logging.getLogger(__name__)
 
 def show_progress(current_line, total_line, label, width, writer=None):
 
@@ -41,6 +45,74 @@
     return LANGUAGE_URI_MAP.get(lang_uri, None)
 
 
+
+def get_labels_for_uris(uri_list, scheme_uri, lang, acronyms=False):
+    query_without_acronym = """
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT ?uri ?label
+WHERE {
+    ?uri skos:inScheme <%s> .
+    ?uri skos:prefLabel|skos:label ?label .
+    FILTER (?uri = $root)
+}
+"""
+    query_with_acronym = """
+PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
+PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
+PREFIX owl:<http://www.w3.org/2002/07/owl#>
+PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
+SELECT ?uri ?label ?acro
+WHERE {
+    ?uri skos:inScheme <%s> .
+    ?uri skos:prefLabel|skos:label ?label .
+    OPTIONAL { ?uri skos:altLabel ?acro }
+    FILTER (?uri = $root)
+}
+"""
+    if acronyms:
+        query = query_with_acronym
+    else:
+        query = query_without_acronym
+    res_dict = {}
+    for uri in uri_list:
+        res_dict[uri] = ""
+        res = requests.get(
+            settings.SPARQL_QUERY_ENDPOINT,
+            params={'query':query % scheme_uri, 'timeout':10, '$root' : "<"+uri+">"},
+            headers={'accept':'application/sparql-results+json'},
+        )
+        if not res.ok:
+            continue
+        elif res.text:
+            json_res = res.json()
+            if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0:
+                # json_res['results']['bindings'] has several languages. If we find french, we save the french label.
+                # If not, we save the first one.
+                tmp_dict = {}
+                first_label = None
+                # We create a temporary dict with the lang code and the label
+                for b in json_res['results']['bindings']:
+                    if lang:
+                        if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']:
+                            tmp_dict[b['label']['xml:lang']] = b['label']['value']
+                            if not first_label:
+                                first_label = b['label']['value']
+                    else:
+                        if 'acro' in b and 'value' in b['acro']:
+                            first_label = b['acro']['value'] + " : " + b['label']['value']
+                        else:
+                            first_label = b['label']['value']
+                if lang in tmp_dict or first_label:
+                    if lang in tmp_dict:
+                        label = tmp_dict[lang]
+                    else:
+                        label = first_label
+                    res_dict[uri] = label
+    return res_dict
+
     
     
 def fill_label_for_model(model, property_uri, scheme_uri):
--- a/src/p4l/views.py	Mon Sep 02 11:45:01 2013 +0200
+++ b/src/p4l/views.py	Mon Sep 02 16:26:21 2013 +0200
@@ -14,6 +14,7 @@
 #from django.views.generic.list import MultipleObjectMixin
 from .models import Record
 from .forms import RecordFilterForm
+from .utils import get_labels_for_uris
 import logging
 
 
@@ -55,6 +56,49 @@
     def get_object(self, queryset=None):
         if "uri" not in self.request.GET:
             raise AttributeError(u"Record view must be called uri GET parameter")
+        return get_object_or_404(Record.objects.select_related("language"), uri=self.request.GET["uri"])
+    
+    def get_context_data(self, **kwargs):
+        context = DetailView.get_context_data(self, **kwargs)
+        # self.object is the record entry
+        # We get the subjects'labels with the Thesaurus repository
+        uri_list = [s.uri for s in self.object.subjects.all()]
+        uris_labels = get_labels_for_uris(uri_list, "http://skos.um.es/unescothes/CS000", "fr", False)
+        context['subjects_labels'] = uris_labels
+        # We get the themes'labels with the Themes repository
+        uri_list = [s.uri for s in self.object.themes.all()]
+        uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Themes", "fr", False)
+        context['themes_labels'] = uris_labels
+        # We get the countries'labels with the Thesaurus repository
+        uri_list = [s.uri for s in self.object.countries.all()]
+        uris_labels = get_labels_for_uris(uri_list, "http://skos.um.es/unescothes/CS000/Countries", "fr", False)
+        context['countries_labels'] = uris_labels
+        # We get the languages'labels with the Languages repository
+        if self.object.language:
+            uri_list = [self.object.language.uri]
+            uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Languages", "fr", False)
+            context['language_label'] = uris_labels[self.object.language.uri]
+        # We get the other languages'labels with the Languages repository
+        uri_list = [s.uri for s in self.object.otherLanguages.all()]
+        uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Languages", "fr", False)
+        context['otherLanguages_labels'] = uris_labels
+        # We get the project'labels with the Projects repository
+        uri_list = [s.uri for s in self.object.projectNames.all()]
+        uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Projects", None, True)
+        context['projects_labels'] = uris_labels
+        # We get the subjectCorporateBodies'labels with the Organizations repository
+        uri_list = [s.uri for s in self.object.subjectCorporateBodies.all()]
+        uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True)
+        context['subjectCorporateBodies_labels'] = uris_labels
+        # We get the corporateAuthors'labels with the Organizations repository
+        uri_list = [s.uri for s in self.object.corporateAuthors.all()]
+        uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/Organizations", None, True)
+        context['corporateAuthors_labels'] = uris_labels
+        # We get the recordType'labels with the DocumentType repository
+        if self.object.recordType:
+            uri_list = [self.object.recordType]
+            uris_labels = get_labels_for_uris(uri_list, "http://www.iiep.unesco.org/plan4learning/scheme/DocumentType", "fr", False)
+            context['recordType_label'] = uris_labels[self.object.recordType]
         
-        return get_object_or_404(Record.objects.select_related("language"), uri=self.request.GET["uri"])
+        return context