web/hdalab/views/ajax.py
changeset 152 e2ceeb903de6
parent 150 b0a34ce6fdcf
child 154 8527c5a3ddb7
equal deleted inserted replaced
151:3e5d26b4844b 152:e2ceeb903de6
     8 from django.db.models import Q, Count
     8 from django.db.models import Q, Count
     9 from django.http import HttpResponse
     9 from django.http import HttpResponse
    10 from hdabo.models import Tag, Datasheet, TaggedSheet
    10 from hdabo.models import Tag, Datasheet, TaggedSheet
    11 from hdalab.models import (TagLinks, HdaSession, Country, TagYears, 
    11 from hdalab.models import (TagLinks, HdaSession, Country, TagYears, 
    12     DatasheetExtras)
    12     DatasheetExtras)
    13 from hdalab.models.dataviz import DbpediaFieldsTranslation
    13 from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields
    14 import django.utils.simplejson as json
    14 import django.utils.simplejson as json
    15 import hmac
    15 import hmac
    16 import itertools
    16 import itertools
    17 import uuid
    17 import uuid
    18 
    18 
   127     q = request.GET.get('term',None)
   127     q = request.GET.get('term',None)
   128     if q:
   128     if q:
   129         lq = q.lower()
   129         lq = q.lower()
   130     qs = Tag.objects.filter( Q(label__icontains = q ) | Q(dbpedia_fields__translations__label__icontains = q, dbpedia_fields__translations__language_code=request.LANGUAGE_CODE)) if q else Tag.objects.all()        
   130     qs = Tag.objects.filter( Q(label__icontains = q ) | Q(dbpedia_fields__translations__label__icontains = q, dbpedia_fields__translations__language_code=request.LANGUAGE_CODE)) if q else Tag.objects.all()        
   131     qs = qs.annotate(nb=Count('datasheet')).order_by('-nb')[:20]
   131     qs = qs.annotate(nb=Count('datasheet')).order_by('-nb')[:20]
       
   132     
       
   133     qslist = list(qs)
       
   134     
       
   135     transqs = DbpediaFieldsTranslation.objects.filter(master__tag__in = qslist, language_code=request.LANGUAGE_CODE).select_related("master")
       
   136     
       
   137     translations = dict([(tr.master.tag_id, tr.label) for tr in transqs])
       
   138     
   132     res = []
   139     res = []
   133     
   140     
   134     for t in qs:
   141     for t in qslist:
   135         resobj = {'value':t.label,'nb':t.nb}
   142         resobj = {'value':t.label,'nb':t.nb}
   136         transqs = DbpediaFieldsTranslation.objects.filter(master__tag = t, language_code=request.LANGUAGE_CODE)[0:1]
   143         if t.id in translations:
   137         if transqs:
   144             resobj['label'] = translations[t.id]
   138             resobj['label'] = transqs.get().label
       
   139         else:
   145         else:
   140             resobj['label'] = t.label
   146             resobj['label'] = t.label
   141         if q is None or resobj['label'].lower().find(lq) != -1:
   147         if q is None or resobj['label'].lower().find(lq) != -1:
   142             res.append(resobj)
   148             res.append(resobj)
   143     
   149     
   153     content_count = request.GET.get('contentcount', 12)
   159     content_count = request.GET.get('contentcount', 12)
   154     tag_count = request.GET.get('tagcount', 30)
   160     tag_count = request.GET.get('tagcount', 30)
   155     
   161     
   156     matchtagids = []
   162     matchtagids = []
   157     
   163     
   158     tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']).select_related('dbpedia_fields')
   164     tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique'])
   159     countryqs = Country.objects
   165     countryqs = Country.objects
   160     discqs = Tag.objects.filter(category__label = u'Discipline artistique').select_related('dbpedia_fields')
   166     discqs = Tag.objects.filter(category__label = u'Discipline artistique').select_related('dbpedia_fields')
   161     yearqs = TagYears.objects
   167     yearqs = TagYears.objects
   162     
   168     
   163     contentqs = Datasheet.objects.filter(validated=True)
   169     contentqs = Datasheet.objects.filter(validated=True)
   238     else:
   244     else:
   239         contenus = contenus.values()
   245         contenus = contenus.values()
   240 
   246 
   241     #tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count]
   247     #tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count]
   242     tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label')[:tag_count]
   248     tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label')[:tag_count]
       
   249     #.select_related('dbpedia_fields')
   243     # hack to add only necessary fields in the group by
   250     # hack to add only necessary fields in the group by
   244     # contournement bug https://code.djangoproject.com/ticket/17144
   251     # contournement bug https://code.djangoproject.com/ticket/17144
   245     tagqs.query.clear_select_fields()
   252     tagqs.query.clear_select_fields()
   246     tagqs.query.add_fields(['id','label'], False)
   253     tagqs.query.add_fields(['id','label'], False)
   247     tagqs.query.set_group_by()
   254     tagqs.query.set_group_by()
   248 
   255 
   249     tagqslist = list(tagqs)
   256     tagqslist = list(tagqs)
   250 
   257     
   251     transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in tagqslist], language_code = request.LANGUAGE_CODE)
   258     dbpediafields = dict([(df.tag_id, df) for df in DbpediaFields.objects.filter(tag__in = tagqslist)])
       
   259 
       
   260     transqs = DbpediaFieldsTranslation.objects.filter(master__in = dbpediafields.values(), language_code = request.LANGUAGE_CODE)
   252     translations = dict([(trans.master_id,trans.label) for trans in transqs])
   261     translations = dict([(trans.master_id,trans.label) for trans in transqs])
   253 
   262 
   254     tags = [{'id': tag.id, 'label': tag.label, 'score': tag.nb, 'translated_label': translations.get(tag.dbpedia_fields.id, tag.label) if tag.dbpedia_fields is not None else tag.label} for tag in tagqslist]
   263     tags = [{'id': tag.id, 'label': tag.label, 'score': tag.nb, 'translated_label': translations.get(dbpediafields[tag.id].id, tag.label) if tag.id in dbpediafields else tag.label} for tag in tagqslist]
   255 
   264 
   256     countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet'))
   265     countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet'))
   257     countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs])
   266     countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs])
   258 
   267 
   259     discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10])
   268     discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10])