src/p4l/utils.py
changeset 12 57efd01f1715
parent 0 81e7900b06a7
child 17 b31a67614f76
--- a/src/p4l/utils.py	Fri Aug 30 15:59:45 2013 +0200
+++ b/src/p4l/utils.py	Fri Aug 30 17:09:14 2013 +0200
@@ -1,8 +1,9 @@
 # -*- coding: utf-8 -*-
-
+from django.conf import settings
 import sys
 import codecs #@UnresolvedImport
 import math
+import requests #@UnresolvedImport
 
 def show_progress(current_line, total_line, label, width, writer=None):
 
@@ -38,3 +39,58 @@
         lang_uri = lang_uri[len(LANGUAGE_NS):]
 
     return LANGUAGE_URI_MAP.get(lang_uri, None)
+
+
+    
+    
+def fill_label_for_model(model, property_uri, scheme_uri):
+    query = """
+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)
+}
+"""
+    # Loads Models label from sparkl query
+    objs = model.objects.filter(label=None)
+    total_objs = len(objs)
+    writer = None
+    i = 0
+    found = 0
+    for o in objs:
+        i += 1
+        res = requests.get(
+            settings.SPARQL_QUERY_ENDPOINT,
+            params={'query':query % scheme_uri, 'timeout':10, '$root' : "<"+getattr(o, property_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 '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']
+                if 'fr' in tmp_dict or first_label:
+                    if 'fr' in tmp_dict:
+                        o.label = tmp_dict['fr']
+                    else:
+                        o.label = first_label
+                    l = o.label
+                    o.save()
+                    found += 1
+                    writer = show_progress(i, total_objs, l, 50, writer=writer)
+    print("Processing Sparql Done. %d found on %d" % (found, total_objs))
\ No newline at end of file