# HG changeset patch # User cavaliet # Date 1377859201 -7200 # Node ID 3bc55f57b2b1ba0435e25fffc5fe504dd8cd5407 # Parent d10cdb768a03d24303952fc1a40d1255d2a88c31 add sparql request for subjects and themes labels diff -r d10cdb768a03 -r 3bc55f57b2b1 src/p4l/management/commands/get_subject_label.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/p4l/management/commands/get_subject_label.py Fri Aug 30 12:40:01 2013 +0200 @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- + +from django.conf import settings +from django.core.management import BaseCommand +from p4l.models import Subject +from p4l.utils import show_progress +import logging +import requests + +logger = logging.getLogger(__name__) + +class Command(BaseCommand): + + query = """ +PREFIX skos: +PREFIX rdf: +PREFIX owl: +PREFIX rdfs: +SELECT ?uri ?label +WHERE { + ?uri skos:inScheme . + ?uri skos:prefLabel|skos:label ?label . + FILTER (?uri = $root) +} +""" + + def fill_label(self): + # Loads Subjects label from sparkl query + subjects = Subject.objects.filter(label=None) + total_subjects = len(subjects) + writer = None + i = 0 + found = 0 + for s in subjects: + i += 1 + res = requests.get( + settings.SPARQL_QUERY_ENDPOINT, + params={'query':self.query, 'timeout':10, '$root' : "<"+s.subject+">"}, + 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: + s.label = tmp_dict['fr'] + else: + s.label = first_label + l = s.label + s.save() + found += 1 + writer = show_progress(i, total_subjects, l, 50, writer=writer) + print("Processing Subjects Sparql Done. %d found on %d" % (found, total_subjects)) + + def handle(self, *args, **options): + self.fill_label() + diff -r d10cdb768a03 -r 3bc55f57b2b1 src/p4l/management/commands/get_theme_label.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/p4l/management/commands/get_theme_label.py Fri Aug 30 12:40:01 2013 +0200 @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- + +from django.conf import settings +from django.core.management import BaseCommand +from p4l.models import Theme +from p4l.utils import show_progress +import logging +import requests + +logger = logging.getLogger(__name__) + +class Command(BaseCommand): + + query = """ +PREFIX skos: +PREFIX rdf: +PREFIX owl: +PREFIX rdfs: +SELECT ?uri ?label +WHERE { + ?uri skos:inScheme . + ?uri skos:prefLabel|skos:label ?label . + FILTER (?uri = $root) +} +""" + + def fill_label(self): + # Loads Themes label from sparkl query + themes = Theme.objects.filter(label=None) + total_themes = len(themes) + writer = None + i = 0 + found = 0 + for t in themes: + i += 1 + res = requests.get( + settings.SPARQL_QUERY_ENDPOINT, + params={'query':self.query, 'timeout':10, '$root' : "<"+t.theme+">"}, + 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: + t.label = tmp_dict['fr'] + else: + t.label = first_label + l = t.label + t.save() + found += 1 + writer = show_progress(i, total_themes, l, 50, writer=writer) + print("Processing Themes Sparql Done. %d found on %d" % (found, total_themes)) + + def handle(self, *args, **options): + self.fill_label() + diff -r d10cdb768a03 -r 3bc55f57b2b1 src/p4l/migrations/0001_initial.py --- a/src/p4l/migrations/0001_initial.py Thu Aug 29 16:12:25 2013 +0200 +++ b/src/p4l/migrations/0001_initial.py Fri Aug 30 12:40:01 2013 +0200 @@ -78,6 +78,7 @@ db.create_table(u'p4l_subject', ( (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('subject', self.gf('django.db.models.fields.URLField')(unique=True, max_length=2048, db_index=True)), + ('label', self.gf('django.db.models.fields.CharField')(db_index=True, max_length=2048, null=True, blank=True)), )) db.send_create_signal('p4l', ['Subject']) @@ -85,6 +86,7 @@ db.create_table(u'p4l_theme', ( (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('theme', self.gf('django.db.models.fields.URLField')(unique=True, max_length=2048, db_index=True)), + ('label', self.gf('django.db.models.fields.CharField')(db_index=True, max_length=2048, null=True, blank=True)), )) db.send_create_signal('p4l', ['Theme']) diff -r d10cdb768a03 -r 3bc55f57b2b1 src/p4l/models/data.py --- a/src/p4l/models/data.py Thu Aug 29 16:12:25 2013 +0200 +++ b/src/p4l/models/data.py Fri Aug 30 12:40:01 2013 +0200 @@ -48,9 +48,11 @@ class Subject(P4lModel): subject = models.URLField(max_length=2048, unique=True, db_index=True) + label = models.CharField(max_length=2048, blank=True, null=True, db_index=True) # filled from sparql request after import class Theme(P4lModel): theme = models.URLField(max_length=2048, unique=True, db_index=True) + label = models.CharField(max_length=2048, blank=True, null=True, db_index=True) # filled from sparql request after import class Country(P4lModel): diff -r d10cdb768a03 -r 3bc55f57b2b1 src/p4l/settings.py --- a/src/p4l/settings.py Thu Aug 29 16:12:25 2013 +0200 +++ b/src/p4l/settings.py Fri Aug 30 12:40:01 2013 +0200 @@ -164,6 +164,7 @@ } NB_RECORDS_BY_PAGE = 20 +SPARQL_QUERY_ENDPOINT = "http://localhost:8080/openrdf-sesame/repositories/plan4learning" from config import * # @UnusedWildImport diff -r d10cdb768a03 -r 3bc55f57b2b1 src/p4l/templates/p4l/p4l_record_view.html --- a/src/p4l/templates/p4l/p4l_record_view.html Thu Aug 29 16:12:25 2013 +0200 +++ b/src/p4l/templates/p4l/p4l_record_view.html Fri Aug 30 12:40:01 2013 +0200 @@ -17,11 +17,11 @@ {% trans 'subjects' %} -
    {% for i in record.subjects.all %}
  • {{ i.subject|default:'' }}
  • {% endfor %}
+
    {% for i in record.subjects.all %}
  • {{ i.label|default:'' }} ({{ i.subject|default:'' }})
  • {% endfor %}
{% trans 'themes' %} -
    {% for i in record.themes.all %}
  • {{ i.theme|default:'' }}
  • {% endfor %}
+
    {% for i in record.themes.all %}
  • {{ i.label|default:'' }} ({{ i.theme|default:'' }})
  • {% endfor %}
{% trans 'countries' %}