diff -r 3e5d26b4844b -r e2ceeb903de6 web/hdalab/views/ajax.py --- a/web/hdalab/views/ajax.py Tue Mar 13 11:12:38 2012 +0100 +++ b/web/hdalab/views/ajax.py Tue Mar 13 11:33:26 2012 +0100 @@ -10,7 +10,7 @@ from hdabo.models import Tag, Datasheet, TaggedSheet from hdalab.models import (TagLinks, HdaSession, Country, TagYears, DatasheetExtras) -from hdalab.models.dataviz import DbpediaFieldsTranslation +from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields import django.utils.simplejson as json import hmac import itertools @@ -129,13 +129,19 @@ lq = q.lower() 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() qs = qs.annotate(nb=Count('datasheet')).order_by('-nb')[:20] + + qslist = list(qs) + + transqs = DbpediaFieldsTranslation.objects.filter(master__tag__in = qslist, language_code=request.LANGUAGE_CODE).select_related("master") + + translations = dict([(tr.master.tag_id, tr.label) for tr in transqs]) + res = [] - for t in qs: + for t in qslist: resobj = {'value':t.label,'nb':t.nb} - transqs = DbpediaFieldsTranslation.objects.filter(master__tag = t, language_code=request.LANGUAGE_CODE)[0:1] - if transqs: - resobj['label'] = transqs.get().label + if t.id in translations: + resobj['label'] = translations[t.id] else: resobj['label'] = t.label if q is None or resobj['label'].lower().find(lq) != -1: @@ -155,7 +161,7 @@ matchtagids = [] - tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']).select_related('dbpedia_fields') + tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']) countryqs = Country.objects discqs = Tag.objects.filter(category__label = u'Discipline artistique').select_related('dbpedia_fields') yearqs = TagYears.objects @@ -240,6 +246,7 @@ #tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count] tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label')[:tag_count] + #.select_related('dbpedia_fields') # hack to add only necessary fields in the group by # contournement bug https://code.djangoproject.com/ticket/17144 tagqs.query.clear_select_fields() @@ -247,11 +254,13 @@ tagqs.query.set_group_by() tagqslist = list(tagqs) + + dbpediafields = dict([(df.tag_id, df) for df in DbpediaFields.objects.filter(tag__in = tagqslist)]) - transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in tagqslist], language_code = request.LANGUAGE_CODE) + transqs = DbpediaFieldsTranslation.objects.filter(master__in = dbpediafields.values(), language_code = request.LANGUAGE_CODE) translations = dict([(trans.master_id,trans.label) for trans in transqs]) - 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] + 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] countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet')) countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs])