web/hdalab/views/ajax.py
changeset 172 8f47c67c6d28
parent 165 b9b93ff09ebe
child 175 ebc92daef815
equal deleted inserted replaced
171:0c96f2c5603e 172:8f47c67c6d28
     7 from django.conf import settings
     7 from django.conf import settings
     8 from django.core.cache import cache
     8 from django.core.cache import cache
     9 from django.db.models import Q, Count
     9 from django.db.models import Q, Count
    10 from django.http import HttpResponse
    10 from django.http import HttpResponse
    11 from hdabo.models import Tag, Datasheet, TaggedSheet
    11 from hdabo.models import Tag, Datasheet, TaggedSheet
    12 from hdalab.models import (TagLinks, HdaSession, Country, TagYears, 
    12 from hdalab.models import HdaSession, Country, TagYears, DatasheetExtras
    13     DatasheetExtras)
       
    14 from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields
    13 from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields
       
    14 from hdalab.utils import fix_cache_key
       
    15 import copy
    15 import django.utils.simplejson as json
    16 import django.utils.simplejson as json
    16 import hmac
    17 import hmac
    17 import itertools
    18 import itertools
    18 import uuid
    19 import uuid
    19 from hdalab.utils import fix_cache_key
       
    20 
    20 
    21 def taginfo(request):
    21 def taginfo(request):
    22     label = request.GET.get('label', None)
    22     label = request.GET.get('label', None)
    23     
    23     
    24     resobj = {'requested_label' : label}
    24     resobj = {'requested_label' : label}
   180         
   180         
   181         contentqs = Datasheet.objects.filter(validated=True)
   181         contentqs = Datasheet.objects.filter(validated=True)
   182         labeltranslations = []
   182         labeltranslations = []
   183     
   183     
   184         if label or periode or country or contentlist :
   184         if label or periode or country or contentlist :
       
   185             matchtagqslist = []
   185             
   186             
   186             if periode:
   187             if periode:
   187                 years = periode.split(",")
   188                 years = periode.split(",")
   188                 start_year = int(years[0])
   189                 start_year = int(years[0])
   189                 end_year = int(years[0:2][-1])
   190                 end_year = int(years[0:2][-1])
   194                                                 years__end_year__gte = start_year, 
   195                                                 years__end_year__gte = start_year, 
   195                                                 years__start_year__lte = end_year,
   196                                                 years__start_year__lte = end_year,
   196                                                 years__end_year__lte = maxend,
   197                                                 years__end_year__lte = maxend,
   197                                                 years__start_year__gte = minstart,
   198                                                 years__start_year__gte = minstart,
   198                                                 )
   199                                                 )
   199                 matchtagids += [t.id for t in matchtagqs]
   200                 matchtagqslist.append(matchtagqs)
   200                 contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs,
       
   201                                taggedsheet__order__lte = max_tag_order)
       
   202                 
   201                 
   203             if label:
   202             if label:
   204                 masters = []
       
   205                 for txtlbl in label.split(","):
   203                 for txtlbl in label.split(","):
   206                     matchtagqs = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = txtlbl.strip())
   204                     matchtagqs = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = txtlbl.strip())
   207                     for t in matchtagqs:
   205                     matchtagqslist.append(matchtagqs)
   208                         if t.id not in matchtagids:
       
   209                             matchtagids.append(t.id)
       
   210                         if t.dbpedia_fields:
       
   211                             masters.append(t.dbpedia_fields)
       
   212                         
       
   213                     contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs,
       
   214                                    taggedsheet__order__lte = max_tag_order)
       
   215                 translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE)    
       
   216                 labeltranslations = [{'label':t.master.label, 'translated_label':t.label} for t in translationqs]
       
   217                 
   206                 
   218             if country:
   207             if country:
   219                 for country_uri in country.split(","):
   208                 for country_uri in country.split(","):
   220                     matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),locatedin__country__dbpedia_uri = country_uri)
   209                     matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),locatedin__country__dbpedia_uri = country_uri)
   221                     matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids]
   210                     matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids]
   222                     contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs,
   211                     matchtagqslist.append(matchtagqs)
   223                                    taggedsheet__order__lte = max_tag_order)
       
   224             if contentlist:
   212             if contentlist:
   225                 contentqs = contentqs.filter(id__in = contentlist.split(","))
   213                 contentqs = contentqs.filter(id__in = contentlist.split(","))
   226                 
   214 
       
   215             tagcond = None
       
   216             tagcondid = None
       
   217             for matchtagqs in matchtagqslist:
       
   218                 newcond = Q(id__in = TaggedSheet.objects.filter(tag__in = copy.deepcopy(matchtagqs), order__lte = max_tag_order).values('datasheet_id'))
       
   219                 newcondid = Q(id__in = matchtagqs)
       
   220                 tagcond = newcond if tagcond is None else (tagcond & newcond)
       
   221                 tagcondid = newcondid if tagcondid is None else (tagcondid | newcondid)
       
   222             
       
   223             contentqs = contentqs.filter(tagcond).distinct()
       
   224             matchtagidsqs = list(Tag.objects.select_related("dbpedia_fields").only("id").filter(tagcondid))
       
   225             matchtagids = [t.id for t in matchtagidsqs]            
       
   226             masters = [t.dbpedia_fields for t in matchtagidsqs if t.dbpedia_fields is not None]
       
   227             
       
   228             translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE)    
       
   229             labeltranslations = [{'label':t.master.label, 'translated_label':t.label} for t in translationqs]
       
   230 
       
   231             
   227             tagqs = tagqs.filter(datasheet__in = contentqs)
   232             tagqs = tagqs.filter(datasheet__in = contentqs)
   228             countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs)
   233             countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs)
   229             discqs = discqs.filter(datasheet__in = contentqs)
   234             discqs = discqs.filter(datasheet__in = contentqs)
   230             yearqs = yearqs.filter(tag__taggedsheet__datasheet__in = contentqs)
   235             yearqs = yearqs.filter(tag__taggedsheet__datasheet__in = contentqs)
   231             
   236             
   232         if contentlist is None:
   237         if contentlist is None:
   233             contentqs.order_by('?')
   238             contentqs.order_by('?')
   234                 
   239                         
   235         cont_count = contentqs.count()
   240         cont_count = contentqs.count()
   236         
   241         
   237         contenus = dict([(content.id, {'score' : 0, 'tags' : [], 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url}) for content in contentqs[0:content_count]])
   242         contenus = dict([(content.id, {'score' : 0, 'tags' : [], 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url}) for content in contentqs[0:content_count]])
   238         contentids = contenus.keys()
   243         contentids = contenus.keys()
   239         
   244