src/p4l/management/commands/get_country_label.py
changeset 12 57efd01f1715
parent 10 c4e7d66b7dc2
equal deleted inserted replaced
11:a88010423961 12:57efd01f1715
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 
       
     3 from django.conf import settings
       
     4 from django.core.management import BaseCommand
     2 from django.core.management import BaseCommand
     5 from p4l.models import Country
     3 from p4l.models import Country
     6 from p4l.utils import show_progress
     4 from p4l.utils import fill_label_for_model
     7 import logging
       
     8 import requests
       
     9 
     5 
    10 logger = logging.getLogger(__name__)
       
    11 
     6 
    12 class Command(BaseCommand):
     7 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/Countries> .
       
    22     ?uri skos:prefLabel|skos:label ?label .
       
    23     FILTER (?uri = $root)
       
    24 }
       
    25 """
       
    26     
       
    27     def fill_label(self):
       
    28         # Loads Countries label from sparkl query
       
    29         countries = Country.objects.filter(label=None)
       
    30         total_countries = len(countries)
       
    31         writer = None
       
    32         i = 0
       
    33         found = 0
       
    34         for c in countries:
       
    35             i += 1
       
    36             res = requests.get(
       
    37                 settings.SPARQL_QUERY_ENDPOINT,
       
    38                 params={'query':self.query, 'timeout':10, '$root' : "<"+c.country+">"},
       
    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                             c.label = tmp_dict['fr']
       
    59                         else:
       
    60                             c.label = first_label
       
    61                         l = c.label
       
    62                         c.save()
       
    63                         found += 1
       
    64                         writer = show_progress(i, total_countries, l, 50, writer=writer)
       
    65         print("Processing Subjects Sparql Done. %d found on %d" % (found, total_countries))
       
    66 
     8 
    67     def handle(self, *args, **options):
     9     def handle(self, *args, **options):
    68         self.fill_label()
    10         fill_label_for_model(Country, 'country', 'http://skos.um.es/unescothes/CS000/Countries')
    69 
    11