--- a/web/hdalab/views/ajax.py Fri Mar 02 23:25:56 2012 +0100
+++ b/web/hdalab/views/ajax.py Sun Mar 11 03:27:09 2012 +0100
@@ -5,13 +5,15 @@
@author: ymh
'''
from django.conf import settings
-from django.db import connection
-from django.db.models import Q, Count, Sum
+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 (TagLinks, HdaSession, Country, TagYears,
+ DatasheetExtras)
+from hdalab.models.dataviz import DbpediaFieldsTranslation
import django.utils.simplejson as json
import hmac
+import itertools
import uuid
def taginfo(request):
@@ -20,7 +22,7 @@
resobj = {'requested_label' : label}
resobj["content_count"] = Datasheet.objects.filter(taggedsheet__tag__label__iexact = label).distinct().count()
-
+
res = Tag.objects.select_related('dbpedia_fields').filter(label__iexact = label).order_by('-dbpedia_uri')[0:1]
if len(res) == 1:
restag = res.get()
@@ -30,15 +32,55 @@
resobj["abstract"] = dbfield.abstract
resobj["dbpedia_label"] = dbfield.label
resobj["thumbnail"] = dbfield.thumbnail
- res = Tag.objects.filter(label__iexact = label).order_by('-wikipedia_url')[0:1]
- if len(res) == 1:
- resobj["wikipedia_url"] = res.get().wikipedia_url
-
+ transqs = DbpediaFieldsTranslation.objects.filter(master=dbfield, language_code=request.LANGUAGE_CODE)[0:1]
+ if transqs:
+ trans = transqs.get()
+ resobj['translated_abstract'] = trans.abstract
+ resobj['translated_label'] = trans.label
+ else:
+ resobj['translated_abstract'] = dbfield.abstract
+ resobj['translated_label'] = dbfield.label
+
+ #res = Tag.objects.filter(label__iexact = label).order_by('-wikipedia_url')[0:1]
+ #if len(res) == 1:
+ # resobj["wikipedia_url"] = res.get().wikipedia_url
+ if 'translated_label' in resobj:
+ wikipedia_label = resobj['translated_label']
+ else:
+ wikipedia_label = label
+ wikipedia_label = wikipedia_label[0].capitalize() + wikipedia_label[1:]
+ resobj["wikipedia_url"] = "http://%s.wikipedia.org/wiki/%s" % (request.LANGUAGE_CODE,wikipedia_label.replace(' ', '_'))
+
resobj["links"] = [{'subject':tl.subject.label, 'object':tl.object.label} for tl in TagLinks.objects.select_related().filter(Q(subject__label__iexact = label) | Q(object__label__iexact = label))]
return HttpResponse(content=json.dumps(resobj), mimetype='application/json')
+def tagtranslation(request):
+
+ labels = request.GET.get('labels',None)
+
+ if not labels:
+ return HttpResponse(content=json.dumps({}), mimetype='application/json')
+
+ labelslist = [lbl.strip() for lbl in labels.split(",")]
+ masters = []
+
+ for lbl in labelslist:
+ labelqs = Tag.objects.select_related('dbpedia_fields').get(label__iexact = lbl)[0:1]
+ if len(labelqs) > 0:
+ tag = labelqs.get()
+ if tag.dbpedia_fields:
+ masters.append(tag.dbpedia_fields)
+
+ translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE)
+
+ translations = dict([(t.master.label, t.label) for t in translationqs])
+
+ return HttpResponse(content=json.dumps(translations), mimetype='application/json')
+
+
+
def sessioninfo(request):
data = json.loads(request.GET.get('data', "{}"))
@@ -100,12 +142,13 @@
matchtagids = []
- tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique'])
+ tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']).select_related('dbpedia_fields')
countryqs = Country.objects
- discqs = Tag.objects.filter(category__label = u'Discipline artistique')
+ discqs = Tag.objects.filter(category__label = u'Discipline artistique').select_related('dbpedia_fields')
yearqs = TagYears.objects
contentqs = Datasheet.objects.filter(validated=True)
+ labeltranslations = []
if label or periode or country or contentlist :
@@ -125,12 +168,20 @@
taggedsheet__order__lte = max_tag_order)
if label:
+ masters = []
for txtlbl in label.split(","):
- matchtagqs = Tag.objects.filter(label__iexact = txtlbl)
- matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids]
+ matchtagqs = Tag.objects.select_related('dbpedia_fields').filter(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]
+
if country:
for country_uri in country.split(","):
matchtagqs = Tag.objects.filter(locatedin__country__dbpedia_uri = country_uri)
@@ -157,10 +208,14 @@
for dse in qs:
contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude}
- qs = TaggedSheet.objects.select_related('tag').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order')
+ qs = list(TaggedSheet.objects.select_related('tag', 'tag__dbpedia_fields').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order'))
+
+ transqs = DbpediaFieldsTranslation.objects.filter(master__in = [ts.tag.dbpedia_fields for ts in qs], language_code = request.LANGUAGE_CODE)
+ translations = dict([(trans.master_id,trans.label) for trans in transqs])
+
for ts in qs:
match_tag = ts.tag.id in matchtagids
- contenus[ts.datasheet_id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag })
+ contenus[ts.datasheet_id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag , 'translated_label': translations.get(ts.tag.dbpedia_fields.id, ts.tag.label) if ts.tag.dbpedia_fields is not None else ts.tag.label})
if match_tag:
contenus[ts.datasheet_id]['score'] += 2*max_tag_order - ts.order
@@ -172,21 +227,29 @@
#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]
- # hack to add only necessary fileds in the group by
+ # hack to add only necessary fields in the group by
# contournement bug https://code.djangoproject.com/ticket/17144
tagqs.query.clear_select_fields()
tagqs.query.add_fields(['id','label'], False)
tagqs.query.set_group_by()
- #hack : django bug
+ tagqslist = list(tagqs)
- tags = [{'id': tag.id, 'label': tag.label, 'score': tag.nb} for tag in tagqs]
+ transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in tagqslist], 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]
countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet'))
countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs])
- discqs = discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10]
- disciplines = [{'label':tag.label,'score':tag.nb} for tag in discqs]
+ discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10])
+
+ transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in discqslist], language_code = request.LANGUAGE_CODE)
+ translations = dict([(trans.master_id,trans.label) for trans in transqs])
+
+
+ disciplines = [{'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 discqslist]
years = {}
yearqs = yearqs.annotate(nb=Count('tag__taggedsheet'))
@@ -203,6 +266,13 @@
if year+1 not in years and year != -1 and score != 0:
yearchange.append({'year': year+1, 'score': 0})
- output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines}
+ tag_translations = {}
+ for t in itertools.chain(labeltranslations,disciplines,tags):
+ tag_translations[t['label']] = t['translated_label']
+ for c in contenus:
+ for t in c['tags']:
+ tag_translations[t['label']] = t['translated_label']
+
+ output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines, 'tagtranslations': tag_translations}
return HttpResponse(content=json.dumps(output), mimetype='application/json')