--- a/web/hdalab/views/ajax.py Fri Feb 17 13:07:49 2012 +0100
+++ b/web/hdalab/views/ajax.py Wed Feb 22 18:55:35 2012 +0100
@@ -9,7 +9,7 @@
from django.db.models import Q, Count, Sum
from django.http import HttpResponse
from hdabo.models import Tag, Datasheet, TaggedSheet
-from hdalab.models import TagLinks, HdaSession, CountryCode, TagYears
+from hdalab.models import TagLinks, HdaSession, Country, GeoInclusion, TagYears
import django.utils.simplejson as json
import hmac
import uuid
@@ -101,13 +101,10 @@
if label or periode or contentlist :
-
+
qs = Datasheet.objects.filter(validated=True)
-
- #$globalsql = "SELECT id, title, description, url FROM hdabo_datasheet E";
globalids = []
- #$globalfilters = array();
if periode:
years = periode.split(",")
@@ -124,7 +121,7 @@
globalids += [t.id for t in tagqs]
- qs = qs.filter(taggedsheet__tag__in = tagqs)
+ qs = qs.filter(taggedsheet__tag__in = tagqs)
if label:
for txtlbl in label.split(","):
@@ -138,95 +135,105 @@
if contentlist:
qs = qs.filter(id__in = contentlist.split(","))
- if contentlist is None:
- qs = qs.order_by('?')
+# if contentlist is None:
+# qs = qs.order_by('?')
+ qs = qs.distinct()
- for content in qs:
- cont_count += 1
- contenus[content.id] = {'score' : 0, 'tags' : [], 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url}
+ contenus = dict([(content.id, {'score' : 0, 'tags' : [], 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url}) for content in qs])
-
- countries = dict([ (cc.label, {'isocode':cc.isocode, 'score':0}) for cc in CountryCode.objects.all() ])
- disciplines = dict([ (d.label, {'label':d.label, 'score':0}) for d in Tag.objects.filter(category__label = "Discipline artistique")])
+ #countries = dict([ (cc.label, {'isocode':cc.isocode, 'score':0}) for cc in CountryCode.objects.all() ])
+ #disciplines = dict([ (d.label, {'label':d.label, 'score':0}) for d in Tag.objects.filter(category__label = "Discipline artistique")])
+
+ countries = {}
+ disciplines = {}
tags = {}
contentids = contenus.keys()
- qs = TaggedSheet.objects.select_related('tag').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order')
+ cont_count = len(contentids)
+
+ qs = TaggedSheet.objects.select_related('tag','tag__category','tag__locatedin__country').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order')
for ts in qs:
match_tag = ts.tag.id in globalids
- contenus[ts.datasheet.id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag})
+ contenutags = {'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag }
+
+# if ts.tag.category is not None:
+# contenutags['category'] = ts.tag.category.label
+
+ contenus[ts.datasheet.id]['tags'].append(contenutags)
+
tagscore = 2*max_tag_order - ts.order
- if ts.tag.id not in tags:
- tags[ts.tag.id] = {'id':ts.tag.id,'label':ts.tag.label, 'score':0}
- tags[ts.tag.id]['score'] += tagscore
+ if ts.tag.category is not None and ts.tag.category.label not in [u'Discipline artistique', u'Datation', u'Localisation']:
+ if ts.tag.id not in tags:
+ tags[ts.tag.id] = {'id':ts.tag.id,'label':ts.tag.label, 'score':0}
+ tags[ts.tag.id]['score'] += 1
+ if match_tag:
+ tags[ts.tag.id]['match'] = True
+
if match_tag:
contenus[ts.datasheet.id]['score'] += tagscore
- tags[ts.tag.id]['match'] = True
- if ts.tag.label in countries:
- countries[ts.tag.label]['score'] += tagscore
- if ts.tag.label in disciplines:
- disciplines[ts.tag.label]['score'] += tagscore
-
- content_count = content_count if not contentlist else len(contenus)
- contenus = sorted(contenus.values(),key=lambda e: e.get('score', 0))[0:content_count]
+
+ if ts.tag.category is not None and ts.tag.category.label == u'Discipline artistique':
+ if ts.tag.label not in disciplines:
+ disciplines[ts.tag.label] = {'label':ts.tag.label, 'score':0}
+ disciplines[ts.tag.label]['score'] += 1
+
+ if ts.tag.locatedin is not None:
+ country_id = ts.tag.locatedin.country.id
+ if country_id not in countries:
+ countries[country_id] = {'id':country_id, 'dbpedia_uri':ts.tag.locatedin.country.dbpedia_uri, 'score': 0}
+ countries[country_id]['score'] += 1
+
+ if contentlist is None:
+ contenus = sorted(contenus.values(),key=lambda e: -e['score'])[0:content_count]
+ contenus = [contenu for contenu in contenus if contenu['score']]
+
countries = countries.values()
- tags = sorted(tags.values(), key=lambda e: e.get('score', 0))[0:tag_count]
- disciplines = sorted(disciplines.values(), key=lambda e: e.get('score', 0))[0:10]
+ tags = sorted(tags.values(), key=lambda e: -e['score'])[0:tag_count]
+ disciplines = sorted(disciplines.values(), key=lambda e: -e['score'])[0:10]
years = {}
+
if contentids:
- qs = TagYears.objects.values('start_year', 'end_year').annotate(order_count=Count('tag__taggedsheet'), order_sum=Sum("tag__taggedsheet__order")).filter(tag__taggedsheet__order__lte=max_tag_order, tag__taggedsheet__datasheet__in = contentids)
+ qs = TagYears.objects.filter(tag__taggedsheet__datasheet__in = contentids).annotate(nb=Count('tag__taggedsheet'))
for ty in qs:
- for year in range(ty['start_year'], ty['end_year']):
- years[year] = (2*max_tag_order*ty['order_count']-ty['order_sum'])/(ty['end_year']-ty['start_year']) + years[year] if year in years else 0
-# $rq = pg_query("SELECT U.start_year, U.end_year, SUM(".(2*$max_tag_order)." - V.order)/(U.end_year + 1 - U.start_year) score FROM hdaviz_years U, hdabo_taggedsheet V WHERE U.tag_id = V.tag_id AND V.order <= $max_tag_order AND V.datasheet_id IN ($contentids) GROUP BY U.start_year, U.end_year");
-# while($ligne = pg_fetch_row($rq)) {
-# foreach(range($ligne[0], $ligne[1]) as $year) {
-# $years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 );
+ for year in range(ty.start_year, ty.end_year):
+ years[year] = ty.nb + years[year] if year in years else 0
else:
for ds in Datasheet.objects.order_by("?")[:content_count]:
contenus[ds.id] = {'id':ds.id, 'title':ds.title, 'description':ds.description, 'url':ds.url, 'tags':[]}
+
cont_count = Datasheet.objects.count()
qs = TaggedSheet.objects.select_related('tag','datasheet').filter(datasheet__id__in = contenus.keys(), order__lte = max_tag_order).order_by("order").only('order','tag__label','tag__id','datasheet__id')
for ts in qs:
contenus[ts.datasheet.id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order})
contenus = contenus.values()
+
+ qs = Tag.objects.exclude(category__label = u"Localisation").exclude(category__label = u"Datation").exclude(category__label = u"Discipline artistice").annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count]
+ tags = [{'id': tag.id, 'label': tag.label, 'score': tag.nb} for tag in qs]
+
+ qs = Country.objects.annotate(nb=Count('includes__tag__taggedsheet')).order_by('-nb')
+ countries = [{'id': country.id, 'dbpedia_uri': country.dbpedia_uri, 'score': country.nb} for country in qs]
- cursor = connection.cursor() #@UndefinedVariable
- try:
- cursor.execute("select t.id, t.label, sum(2*%s-ts.order) as score from hdabo_tag as t join hdabo_taggedsheet as ts on t.id = ts.tag_id where ts.order < %s group by t.id, t.label order by score limit %s",[max_tag_order, max_tag_order, tag_count])
- tags = [{'id': t[0], 'label':t[1], 'score':t[2]} for t in cursor.fetchall()]
- #tags = sorted([{'id':tag.id,'label':tag.label,'score':2*max_tag_order*tag.count_score - tag.sum_score} for tag in qs], key=lambda t:t['score'])[:tag_count]
- finally:
- cursor.close()
- cursor = connection.cursor() #@UndefinedVariable
- try:
- cursor.execute("select c.isocode as isocode, sum(2*%s-ts.order) as score from hdabo_tag as t join hdabo_taggedsheet as ts on t.id = ts.tag_id join hdalab_countrycode as c on t.label = c.label group by c.isocode", [max_tag_order])
- countries = [{'isocode': r[0], 'score':r[1]} for r in cursor.fetchall()]
-
- finally:
- cursor.close()
-
- qs = Tag.objects.annotate(count_score=Count('taggedsheet'),sum_score=Sum('taggedsheet__order')).filter(taggedsheet__order__lte = max_tag_order, category__label__iexact = u"Discipline")
- disciplines = sorted([{'label':tag.label,'score':2*max_tag_order*tag.count_score - tag.sum_score} for tag in qs], key=lambda t:t.score, reverse=True)[:10]
+ qs = Tag.objects.annotate(nb=Count('taggedsheet')).filter(category = 5).order_by('-nb')[:10]
+ disciplines = [{'label':tag.label,'score':tag.nb} for tag in qs]
years = {}
- qs = TagYears.objects.values('start_year', 'end_year').annotate(order_count=Count('tag__taggedsheet'), order_sum=Sum("tag__taggedsheet__order")).filter(tag__taggedsheet__order__lte=max_tag_order)
+ qs = TagYears.objects.annotate(nb=Count('tag__taggedsheet'))
for ty in qs:
- for year in range(ty['start_year'], ty['end_year']):
- years[year] = (2*max_tag_order*ty['order_count']-ty['order_sum'])/(ty['end_year']-ty['start_year']) + years[year] if year in years else 0
+ for year in range(ty.start_year, ty.end_year):
+ years[year] = ty.nb + years[year] if year in years else 0
yearchange = []
for year in sorted(years.keys()):
score = years[year]
if year < 2011:
- if year-1 not in years or years[year-1] != score:
+ if (year-1 not in years and score != 0) or (year-1 in years and years[year-1] != score):
yearchange.append({'year': year, 'score': score})
- if year+1 not in years and year != -1:
+ 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}