|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 from django.conf import settings |
|
4 from django.core.management import BaseCommand |
|
5 from p4l.models import Theme |
|
6 from p4l.utils import show_progress |
|
7 import logging |
|
8 import requests |
|
9 |
|
10 logger = logging.getLogger(__name__) |
|
11 |
|
12 class Command(BaseCommand): |
|
13 |
|
14 query = """ |
|
15 PREFIX skos:<http://www.w3.org/2004/02/skos/core#> |
|
16 PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> |
|
17 PREFIX owl:<http://www.w3.org/2002/07/owl#> |
|
18 PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> |
|
19 SELECT ?uri ?label |
|
20 WHERE { |
|
21 ?uri skos:inScheme <http://www.iiep.unesco.org/plan4learning/scheme/Themes> . |
|
22 ?uri skos:prefLabel|skos:label ?label . |
|
23 FILTER (?uri = $root) |
|
24 } |
|
25 """ |
|
26 |
|
27 def fill_label(self): |
|
28 # Loads Themes label from sparkl query |
|
29 themes = Theme.objects.filter(label=None) |
|
30 total_themes = len(themes) |
|
31 writer = None |
|
32 i = 0 |
|
33 found = 0 |
|
34 for t in themes: |
|
35 i += 1 |
|
36 res = requests.get( |
|
37 settings.SPARQL_QUERY_ENDPOINT, |
|
38 params={'query':self.query, 'timeout':10, '$root' : "<"+t.theme+">"}, |
|
39 headers={'accept':'application/sparql-results+json'}, |
|
40 ) |
|
41 if not res.ok: |
|
42 continue |
|
43 elif res.text: |
|
44 json_res = res.json() |
|
45 if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0: |
|
46 # json_res['results']['bindings'] has several languages. If we find french, we save the french label. |
|
47 # If not, we save the first one. |
|
48 tmp_dict = {} |
|
49 first_label = None |
|
50 # We create a temporary dict with the lang code and the label |
|
51 for b in json_res['results']['bindings']: |
|
52 if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']: |
|
53 tmp_dict[b['label']['xml:lang']] = b['label']['value'] |
|
54 if not first_label: |
|
55 first_label = b['label']['value'] |
|
56 if 'fr' in tmp_dict or first_label: |
|
57 if 'fr' in tmp_dict: |
|
58 t.label = tmp_dict['fr'] |
|
59 else: |
|
60 t.label = first_label |
|
61 l = t.label |
|
62 t.save() |
|
63 found += 1 |
|
64 writer = show_progress(i, total_themes, l, 50, writer=writer) |
|
65 print("Processing Themes Sparql Done. %d found on %d" % (found, total_themes)) |
|
66 |
|
67 def handle(self, *args, **options): |
|
68 self.fill_label() |
|
69 |