|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 from django.conf import settings |
|
4 from django.core.management import BaseCommand |
|
5 from p4l.models import Language |
|
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/Languages> . |
|
22 ?uri skos:prefLabel|skos:label ?label . |
|
23 FILTER (?uri = $root) |
|
24 } |
|
25 """ |
|
26 |
|
27 def fill_label(self): |
|
28 # Loads Subjects label from sparkl query |
|
29 langs = Language.objects.filter(label=None) |
|
30 total_langs = len(langs) |
|
31 writer = None |
|
32 i = 0 |
|
33 found = 0 |
|
34 for l in langs: |
|
35 i += 1 |
|
36 logger.debug("1") |
|
37 logger.debug(l) |
|
38 logger.debug("2") |
|
39 logger.debug(l.language) |
|
40 res = requests.get( |
|
41 settings.SPARQL_QUERY_ENDPOINT, |
|
42 params={'query':self.query, 'timeout':10, '$root' : "<"+l.language+">"}, |
|
43 headers={'accept':'application/sparql-results+json'}, |
|
44 ) |
|
45 if not res.ok: |
|
46 continue |
|
47 elif res.text: |
|
48 json_res = res.json() |
|
49 if 'results' in json_res and 'bindings' in json_res['results'] and len(json_res['results']['bindings'])>0: |
|
50 # json_res['results']['bindings'] has several languages. If we find french, we save the french label. |
|
51 # If not, we save the first one. |
|
52 tmp_dict = {} |
|
53 first_label = None |
|
54 # We create a temporary dict with the lang code and the label |
|
55 for b in json_res['results']['bindings']: |
|
56 if 'label' in b and 'value' in b['label'] and 'xml:lang' in b['label']: |
|
57 tmp_dict[b['label']['xml:lang']] = b['label']['value'] |
|
58 if not first_label: |
|
59 first_label = b['label']['value'] |
|
60 if 'fr' in tmp_dict or first_label: |
|
61 if 'fr' in tmp_dict: |
|
62 l.label = tmp_dict['fr'] |
|
63 else: |
|
64 l.label = first_label |
|
65 lab = l.label |
|
66 l.save() |
|
67 found += 1 |
|
68 writer = show_progress(i, total_langs, lab, 50, writer=writer) |
|
69 print("Processing Subjects Sparql Done. %d found on %d" % (found, total_langs)) |
|
70 |
|
71 def handle(self, *args, **options): |
|
72 self.fill_label() |
|
73 |