diff -r 0c96f2c5603e -r 8f47c67c6d28 web/hdalab/views/ajax.py --- a/web/hdalab/views/ajax.py Thu Mar 15 16:37:28 2012 +0100 +++ b/web/hdalab/views/ajax.py Mon Mar 19 16:32:36 2012 +0100 @@ -9,14 +9,14 @@ from django.db.models import Q, Count from django.http import HttpResponse from hdabo.models import Tag, Datasheet, TaggedSheet -from hdalab.models import (TagLinks, HdaSession, Country, TagYears, - DatasheetExtras) +from hdalab.models import HdaSession, Country, TagYears, DatasheetExtras from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields +from hdalab.utils import fix_cache_key +import copy import django.utils.simplejson as json import hmac import itertools import uuid -from hdalab.utils import fix_cache_key def taginfo(request): label = request.GET.get('label', None) @@ -182,6 +182,7 @@ labeltranslations = [] if label or periode or country or contentlist : + matchtagqslist = [] if periode: years = periode.split(",") @@ -196,34 +197,38 @@ years__end_year__lte = maxend, years__start_year__gte = minstart, ) - matchtagids += [t.id for t in matchtagqs] - contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, - taggedsheet__order__lte = max_tag_order) + matchtagqslist.append(matchtagqs) if label: - masters = [] for txtlbl in label.split(","): matchtagqs = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = txtlbl.strip()) - for t in matchtagqs: - if t.id not in matchtagids: - matchtagids.append(t.id) - if t.dbpedia_fields: - masters.append(t.dbpedia_fields) - - contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, - taggedsheet__order__lte = max_tag_order) - translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE) - labeltranslations = [{'label':t.master.label, 'translated_label':t.label} for t in translationqs] + matchtagqslist.append(matchtagqs) if country: for country_uri in country.split(","): matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),locatedin__country__dbpedia_uri = country_uri) matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids] - contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, - taggedsheet__order__lte = max_tag_order) + matchtagqslist.append(matchtagqs) if contentlist: contentqs = contentqs.filter(id__in = contentlist.split(",")) - + + tagcond = None + tagcondid = None + for matchtagqs in matchtagqslist: + newcond = Q(id__in = TaggedSheet.objects.filter(tag__in = copy.deepcopy(matchtagqs), order__lte = max_tag_order).values('datasheet_id')) + newcondid = Q(id__in = matchtagqs) + tagcond = newcond if tagcond is None else (tagcond & newcond) + tagcondid = newcondid if tagcondid is None else (tagcondid | newcondid) + + contentqs = contentqs.filter(tagcond).distinct() + matchtagidsqs = list(Tag.objects.select_related("dbpedia_fields").only("id").filter(tagcondid)) + matchtagids = [t.id for t in matchtagidsqs] + masters = [t.dbpedia_fields for t in matchtagidsqs if t.dbpedia_fields is not None] + + translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE) + labeltranslations = [{'label':t.master.label, 'translated_label':t.label} for t in translationqs] + + tagqs = tagqs.filter(datasheet__in = contentqs) countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs) discqs = discqs.filter(datasheet__in = contentqs) @@ -231,7 +236,7 @@ if contentlist is None: contentqs.order_by('?') - + cont_count = contentqs.count() 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]])