|
9
|
1 |
# -*- coding: utf-8 -*- |
|
|
2 |
|
|
|
3 |
from django.conf import settings |
|
|
4 |
from django.core.management import BaseCommand |
|
|
5 |
from p4l.models import Subject |
|
|
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://skos.um.es/unescothes/CS000> . |
|
|
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 |
subjects = Subject.objects.filter(label=None) |
|
|
30 |
total_subjects = len(subjects) |
|
|
31 |
writer = None |
|
|
32 |
i = 0 |
|
|
33 |
found = 0 |
|
|
34 |
for s in subjects: |
|
|
35 |
i += 1 |
|
|
36 |
res = requests.get( |
|
|
37 |
settings.SPARQL_QUERY_ENDPOINT, |
|
|
38 |
params={'query':self.query, 'timeout':10, '$root' : "<"+s.subject+">"}, |
|
|
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 |
s.label = tmp_dict['fr'] |
|
|
59 |
else: |
|
|
60 |
s.label = first_label |
|
|
61 |
l = s.label |
|
|
62 |
s.save() |
|
|
63 |
found += 1 |
|
|
64 |
writer = show_progress(i, total_subjects, l, 50, writer=writer) |
|
|
65 |
print("Processing Subjects Sparql Done. %d found on %d" % (found, total_subjects)) |
|
|
66 |
|
|
|
67 |
def handle(self, *args, **options): |
|
|
68 |
self.fill_label() |
|
|
69 |
|