web/hdalab/views/ajax.py
author ymh <ymh.work@gmail.com>
Tue, 13 Mar 2012 15:38:57 +0100
changeset 165 b9b93ff09ebe
parent 163 e4ff12317fa3
child 172 8f47c67c6d28
permissions -rw-r--r--
Remove unsemantized tags
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Jan 31, 2012
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from django.conf import settings
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
     8
from django.core.cache import cache
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
     9
from django.db.models import Q, Count
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from django.http import HttpResponse
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from hdabo.models import Tag, Datasheet, TaggedSheet
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    12
from hdalab.models import (TagLinks, HdaSession, Country, TagYears, 
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    13
    DatasheetExtras)
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
    14
from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
import django.utils.simplejson as json
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
import hmac
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    17
import itertools
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
import uuid
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
    19
from hdalab.utils import fix_cache_key
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
def taginfo(request):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    label = request.GET.get('label', None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    resobj = {'requested_label' : label}
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    resobj["content_count"] = Datasheet.objects.filter(taggedsheet__tag__label__iexact = label).distinct().count()
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    27
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
    28
    res = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = label).order_by('-dbpedia_uri')[0:1]
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
    if len(res) == 1:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        restag = res.get()
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        resobj["dbpedia_uri"] = restag.dbpedia_uri
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        if resobj["dbpedia_uri"] is not None and restag.dbpedia_fields is not None:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
            dbfield = restag.dbpedia_fields
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
            resobj["abstract"] = dbfield.abstract
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
            resobj["dbpedia_label"] = dbfield.label
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
            resobj["thumbnail"] = dbfield.thumbnail
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    37
            transqs = DbpediaFieldsTranslation.objects.filter(master=dbfield, language_code=request.LANGUAGE_CODE)[0:1]
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    38
            if transqs:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    39
                trans = transqs.get()
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    40
                resobj['translated_abstract'] = trans.abstract
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    41
                resobj['translated_label'] = trans.label
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    42
            else:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    43
                resobj['translated_abstract'] = dbfield.abstract
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    44
                resobj['translated_label'] = dbfield.label
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    45
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    46
    #res = Tag.objects.filter(label__iexact = label).order_by('-wikipedia_url')[0:1]
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    47
    #if len(res) == 1:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    48
    #    resobj["wikipedia_url"] = res.get().wikipedia_url
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    49
    if 'translated_label' in resobj:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    50
        wikipedia_label = resobj['translated_label']
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    51
    else:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    52
        wikipedia_label = label
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    53
    wikipedia_label = wikipedia_label[0].capitalize() + wikipedia_label[1:]
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    54
    resobj["wikipedia_url"] = "http://%s.wikipedia.org/wiki/%s" % (request.LANGUAGE_CODE,wikipedia_label.replace(' ', '_'))
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
    55
# We don't use links at the moment, so I'll comment this line to speed up requests
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
    56
#    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))]
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
    return HttpResponse(content=json.dumps(resobj), mimetype='application/json')
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    61
def tagtranslation(request):
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    62
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    63
    labels = request.GET.get('labels',None)
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    64
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    65
    if not labels:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    66
        return HttpResponse(content=json.dumps({}), mimetype='application/json')
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    67
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    68
    labelslist = [lbl.strip() for lbl in labels.split(",")]
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    69
    masters = []
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    70
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    71
    for lbl in labelslist:
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
    72
        labelqs = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = lbl)[0:1]
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    73
        if len(labelqs) > 0:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    74
            tag = labelqs.get()
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    75
            if tag.dbpedia_fields:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    76
                masters.append(tag.dbpedia_fields)
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    77
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    78
    translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE)
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    79
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    80
    translations = dict([(t.master.label, t.label) for t in translationqs])
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    81
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    82
    return HttpResponse(content=json.dumps(translations), mimetype='application/json')
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    83
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    84
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    85
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
def sessioninfo(request):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    data = json.loads(request.GET.get('data', "{}"))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    write = False
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
    if 'sessionid' in request.GET:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        request.session['sessionid'] = request.GET['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
    if 'sessionkey' in request.GET:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        request.session['sessionkey'] = request.GET['sessionkey']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
    if 'sessionid' in request.session:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        sessionid = request.session['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        if HdaSession.objects.filter(sessionid=sessionid).count() == 1:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
            sessionkey = request.session.get('sessionkey',None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
            hm = hmac.new(settings.SECRET_KEY, sessionid)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
            if hm.hexdigest() == sessionkey:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
                write = True            
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        else:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
            del request.session['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    if 'sessionid' not in request.session:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        sessionid = unicode(uuid.uuid1())
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        HdaSession.objects.create(sessionid=sessionid, data=json.dumps({}))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        write = True
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        request.session['sessionid'] = sessionid
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        request.session['sessionkey'] = hmac.new(settings.SECRET_KEY, sessionid).hexdigest()
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
    if write and data:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
        HdaSession.objects.filter(sessionid=sessionid).update(data=json.dumps(data))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
    else:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        data = HdaSession.objects.get(sessionid=sessionid).data
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
        data = json.loads(data) if data else {}           
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
         
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
    resobj = {'data': data, "write_allowed" : write, "sessionid": sessionid }
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
    if write:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
        resobj['sessionkey'] = request.session['sessionkey']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
    return HttpResponse(content=json.dumps(resobj), mimetype='application/json')
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
def tagsearch(request):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
    q = request.GET.get('term',None)
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   130
    if q:
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   131
        lq = q.lower()
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   132
    qs = Tag.objects.filter( Q(label__icontains = q ) | Q(dbpedia_fields__translations__label__icontains = q, dbpedia_fields__translations__language_code=request.LANGUAGE_CODE), ~Q(dbpedia_uri = None)) if q else Tag.objects.filter(~Q(dbpedia_uri = None))       
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   133
    qs = qs.annotate(nb=Count('datasheet')).order_by('-nb')[:20]
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   134
    
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   135
    qslist = list(qs)
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   136
    
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   137
    transqs = DbpediaFieldsTranslation.objects.filter(master__tag__in = qslist, language_code=request.LANGUAGE_CODE).select_related("master")
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   138
    
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   139
    translations = dict([(tr.master.tag_id, tr.label) for tr in transqs])
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   140
    
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   141
    res = []
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
    
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   143
    for t in qslist:
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   144
        resobj = {'value':t.label,'nb':t.nb}
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   145
        if t.id in translations:
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   146
            resobj['label'] = translations[t.id]
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   147
        else:
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   148
            resobj['label'] = t.label
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   149
        if q is None or resobj['label'].lower().find(lq) != -1:
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   150
            res.append(resobj)
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   151
    
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   152
    return HttpResponse(content=json.dumps(res), mimetype='application/json')
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
def filter(request):
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   156
    
158
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   157
    periode = request.GET.get('period',None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   158
    label = request.GET.get('label', None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   159
    country = request.GET.get('country', None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   160
    contentlist = request.GET.get('contentlist', None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   161
    max_tag_order = request.GET.get('mto', 12)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   162
    content_count = request.GET.get('contentcount', 12)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   163
    tag_count = request.GET.get('tagcount', 30)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   164
    
163
e4ff12317fa3 correct accent in keys
ymh <ymh.work@gmail.com>
parents: 162
diff changeset
   165
    key_parts = ("filter",request.LANGUAGE_CODE,periode,label,country,contentlist,max_tag_order,content_count,tag_count)
e4ff12317fa3 correct accent in keys
ymh <ymh.work@gmail.com>
parents: 162
diff changeset
   166
    key_parts = [unicode(p).encode("utf-8") for p in key_parts]
e4ff12317fa3 correct accent in keys
ymh <ymh.work@gmail.com>
parents: 162
diff changeset
   167
    
e4ff12317fa3 correct accent in keys
ymh <ymh.work@gmail.com>
parents: 162
diff changeset
   168
    cache_key = fix_cache_key("-".join(key_parts))
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   169
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   170
    outputstr = cache.get(cache_key)
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   171
    
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   172
    if outputstr is None:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   173
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   174
        matchtagids = []
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
        
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   176
        tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']).filter(~Q(dbpedia_uri = None))
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   177
        countryqs = Country.objects
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   178
        discqs = Tag.objects.filter(~Q(dbpedia_uri = None), category__label = u'Discipline artistique').select_related('dbpedia_fields')
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   179
        yearqs = TagYears.objects
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   180
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   181
        contentqs = Datasheet.objects.filter(validated=True)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   182
        labeltranslations = []
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   183
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   184
        if label or periode or country or contentlist :
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
            
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   186
            if periode:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   187
                years = periode.split(",")
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   188
                start_year = int(years[0])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   189
                end_year = int(years[0:2][-1])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   190
                delta = max(1, (end_year-start_year)/2)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   191
                minstart = start_year - delta
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   192
                maxend = end_year + delta
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   193
                matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   194
                                                years__end_year__gte = start_year, 
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   195
                                                years__start_year__lte = end_year,
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   196
                                                years__end_year__lte = maxend,
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   197
                                                years__start_year__gte = minstart,
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   198
                                                )
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   199
                matchtagids += [t.id for t in matchtagqs]
132
e6483309fa52 style changes and minor corrections
veltr
parents: 131
diff changeset
   200
                contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs,
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   201
                               taggedsheet__order__lte = max_tag_order)
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   202
                
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   203
            if label:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   204
                masters = []
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   205
                for txtlbl in label.split(","):
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   206
                    matchtagqs = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = txtlbl.strip())
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   207
                    for t in matchtagqs:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   208
                        if t.id not in matchtagids:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   209
                            matchtagids.append(t.id)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   210
                        if t.dbpedia_fields:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   211
                            masters.append(t.dbpedia_fields)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   212
                        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   213
                    contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs,
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   214
                                   taggedsheet__order__lte = max_tag_order)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   215
                translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE)    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   216
                labeltranslations = [{'label':t.master.label, 'translated_label':t.label} for t in translationqs]
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   217
                
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   218
            if country:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   219
                for country_uri in country.split(","):
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   220
                    matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),locatedin__country__dbpedia_uri = country_uri)
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   221
                    matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids]
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   222
                    contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs,
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   223
                                   taggedsheet__order__lte = max_tag_order)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   224
            if contentlist:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   225
                contentqs = contentqs.filter(id__in = contentlist.split(","))
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   226
                
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   227
            tagqs = tagqs.filter(datasheet__in = contentqs)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   228
            countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   229
            discqs = discqs.filter(datasheet__in = contentqs)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   230
            yearqs = yearqs.filter(tag__taggedsheet__datasheet__in = contentqs)
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   231
            
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   232
        if contentlist is None:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   233
            contentqs.order_by('?')
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   234
                
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   235
        cont_count = contentqs.count()
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   236
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   237
        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]])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   238
        contentids = contenus.keys()
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   239
        
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   240
        qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   241
        for dse in qs:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   242
            contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude}
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   243
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   244
        qs = list(TaggedSheet.objects.select_related('tag', 'tag__dbpedia_fields').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order'))
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   245
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   246
        transqs = DbpediaFieldsTranslation.objects.filter(master__in = [ts.tag.dbpedia_fields for ts in qs], language_code = request.LANGUAGE_CODE)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   247
        translations = dict([(trans.master_id,trans.label) for trans in transqs])
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   248
        
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   249
        for ts in qs:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   250
            match_tag = ts.tag.id in matchtagids
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   251
            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})
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   252
            
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   253
            if match_tag:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   254
                contenus[ts.datasheet_id]['score'] += 2*max_tag_order - ts.order
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   255
            
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   256
        if contentlist is None:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   257
            contenus = sorted(contenus.values(),key=lambda e: -e['score'])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   258
        else:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   259
            contenus = contenus.values()
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   260
    
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   261
        #tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count]
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   262
        tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label')[:tag_count]
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   263
        #.select_related('dbpedia_fields')
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   264
        # hack to add only necessary fields in the group by
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   265
        # contournement bug https://code.djangoproject.com/ticket/17144
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   266
        tagqs.query.clear_select_fields()
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   267
        tagqs.query.add_fields(['id','label'], False)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   268
        tagqs.query.set_group_by()
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   269
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   270
        tagqslist = list(tagqs)
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   271
        
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   272
        dbpediafields = dict([(df.tag_id, df) for df in DbpediaFields.objects.filter(tag__in = tagqslist)])
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   273
    
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   274
        transqs = DbpediaFieldsTranslation.objects.filter(master__in = dbpediafields.values(), language_code = request.LANGUAGE_CODE)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   275
        translations = dict([(trans.master_id,trans.label) for trans in transqs])
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   276
    
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   277
        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]
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   278
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   279
        countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet'))
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   280
        countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   281
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   282
        discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10])
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   283
            
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   284
        transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in discqslist], language_code = request.LANGUAGE_CODE)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   285
        translations = dict([(trans.master_id,trans.label) for trans in transqs])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   286
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   287
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   288
        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]
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   289
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   290
        years = {}
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   291
        yearqs = yearqs.annotate(nb=Count('tag__taggedsheet'))
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   292
        for ty in yearqs:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   293
            for year in range(ty.start_year, ty.end_year):
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   294
                years[year] = ty.nb + (years[year] if year in years else 0)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   295
                
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   296
        yearchange = []
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   297
        for year in sorted(years.keys()):
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   298
            score = years[year]
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   299
            if year < 2011:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   300
                if (year-1 not in years and score != 0) or (year-1 in years and years[year-1] != score):
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   301
                    yearchange.append({'year': year, 'score': score})
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   302
                if year+1 not in years and year != -1 and score != 0:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   303
                    yearchange.append({'year': year+1, 'score': 0})
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   304
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   305
        tag_translations = {}
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   306
        for t in itertools.chain(labeltranslations,disciplines,tags):
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   307
            tag_translations[t['label']] = t['translated_label']
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   308
        for c in contenus:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   309
            for t in c['tags']:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   310
                tag_translations[t['label']] = t['translated_label']
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   311
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   312
        output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines, 'tagtranslations': tag_translations}
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   313
        outputstr = json.dumps(output)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   314
        cache.set(cache_key, outputstr)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   315
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   316
    return HttpResponse(content=outputstr, mimetype='application/json')