web/hdalab/views/ajax.py
changeset 127 8642f1fb6499
parent 122 fde8335a037c
child 131 9d5d9e6b7fbf
--- a/web/hdalab/views/ajax.py	Fri Feb 24 18:38:00 2012 +0100
+++ b/web/hdalab/views/ajax.py	Mon Feb 27 18:08:42 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, Country, GeoInclusion, TagYears
+from hdalab.models import TagLinks, HdaSession, Country, TagYears, DatasheetExtras
 import django.utils.simplejson as json
 import hmac
 import uuid
@@ -92,141 +92,100 @@
 def filter(request):
     periode = request.GET.get('period',None)
     label = request.GET.get('label', None)
+    country = request.GET.get('country', None)
     contentlist = request.GET.get('contentlist', None)
     max_tag_order = request.GET.get('mto', 12)
     content_count = request.GET.get('contentcount', 12)
     tag_count = request.GET.get('tagcount', 30)
-    cont_count = 0;
-    contenus = {}
-
+    cont_count = 0
+    
+    globalids = []
+    
+    tagqs = Tag.objects.exclude(category__label = u"Localisation").exclude(category__label = u"Datation").exclude(category__label = u"Discipline artistique")
+    countryqs = Country.objects
+    discqs = Tag.objects.filter(category__label = u'Discipline artistique')
+    yearqs = TagYears.objects
+    
+    contentqs = Datasheet.objects.filter(validated=True)
 
-    if label or periode or contentlist :
-
-        qs = Datasheet.objects.filter(validated=True)
-
-        globalids = []
+    if label or periode or country or contentlist :
         
         if periode:
             years = periode.split(",")
             start_year = int(years[0])
             end_year = int(years[0:2][-1])
             delta = max(1, (end_year-start_year)/2)
-            minstart = start_year - delta;
-            maxend = end_year + delta;
-            
+            minstart = start_year - delta
+            maxend = end_year + delta
             tagqs = Tag.objects.filter(years__end_year__gte = start_year, 
                                        years__start_year__lte = end_year,
                                        years__end_year__lte = maxend,
                                        years__start_year__gte = minstart)
-            
             globalids += [t.id for t in tagqs]
-            
-            qs = qs.filter(taggedsheet__tag__in = tagqs)
+            contentqs = contentqs.filter(taggedsheet__tag__in = tagqs,
+                           taggedsheet__order__lte = max_tag_order)
             
         if label:
             for txtlbl in label.split(","):
                 taglblqs = Tag.objects.filter(label__iexact = txtlbl)
                 globalids += [t.id for t in taglblqs if t.id not in globalids]
-                qs = qs.filter(taggedsheet__tag__in = taglblqs)
-
-        if label or periode:
-            qs = qs.filter(taggedsheet__order__lte = max_tag_order)
-
-        if contentlist:
-            qs = qs.filter(id__in = contentlist.split(","))
-        
-#        if contentlist is None:
-#            qs = qs.order_by('?')
-        
-        qs = qs.distinct()
-        
-        contenus = dict([(content.id, {'score' : 0, 'tags' : [], 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url}) for content in qs])
+                contentqs = contentqs.filter(taggedsheet__tag__in = taglblqs,
+                               taggedsheet__order__lte = max_tag_order)
+                    
+        if country:
+            for country_uri in country.split(","):
+                taglblqs = Tag.objects.filter(locatedin__country__dbpedia_uri = country_uri)
+                globalids += [t.id for t in taglblqs if t.id not in globalids]
+                contentqs = contentqs.filter(taggedsheet__tag__in = taglblqs,
+                               taggedsheet__order__lte = max_tag_order)
 
-        #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 = {}
+        tagqs = tagqs.filter(datasheet__in = contentqs)
+        countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs)
+        discqs = discqs.filter(datasheet__in = contentqs)
+        yearqs = yearqs.filter(tag__taggedsheet__datasheet__in = contentqs)
         
-        contentids = contenus.keys()
-        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
-            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
+    if contentlist is not None:
+        contentqs = contentqs.filter(id__in = contentlist.split(","))
+    else:
+        contentqs.order_by('?')
             
-            contenus[ts.datasheet.id]['tags'].append(contenutags)
-                
-            tagscore = 2*max_tag_order - ts.order
-            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
-                
-            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['score'])[0:tag_count]
-        disciplines = sorted(disciplines.values(), key=lambda e: -e['score'])[0:10]
-        years = {}
+    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]])
+    contentids = contenus.keys()
+    
+    qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids)
+    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')
+    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 })
+        
+        if match_tag:
+            contenus[ts.datasheet.id]['score'] += 2*max_tag_order - ts.order
         
-        if 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] = ty.nb + years[year] if year in years else 0
-
+    if contentlist is None:
+        contenus = sorted(contenus.values(),key=lambda e: -e['score'])
     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]
+    tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count]
+    tags = [{'id': tag.id, 'label': tag.label, 'score': tag.nb} for tag in tagqs]
+
+    countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet'))
+    countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs])
 
-        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.annotate(nb=Count('tag__taggedsheet'))
-        for ty in qs:
-            for year in range(ty.start_year, ty.end_year):
-                years[year] = ty.nb + years[year] if year in years else 0
-
+    discqs = discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10]
+    disciplines = [{'label':tag.label,'score':tag.nb} for tag in discqs]
+    
+    years = {}
+    yearqs = yearqs.annotate(nb=Count('tag__taggedsheet'))
+    for ty in yearqs:
+        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]