web/hdalab/views/ajax.py
author veltr
Thu, 04 Oct 2012 12:25:12 +0200
changeset 248 91bc8521e3cb
parent 243 1f2840354865
child 250 7c3f54ce68af
permissions -rw-r--r--
Ajout de la navigation par thesaurus
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
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
     9
from django.db.models import Q, Count, Min
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
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    12
from hdalab.models import HdaSession, Country, TagYears, DatasheetExtras
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
    13
from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    14
from hdalab.models.categories import WpCategory, WpCategoryInclusion, TagWpCategory
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    15
from hdalab.utils import fix_cache_key
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
    16
import copy
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
import django.utils.simplejson as json
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
import hmac
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    19
import itertools
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
import uuid
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    22
def tagtranslation(request):
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    23
    
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
    24
    lang = request.GET.get('lang',request.LANGUAGE_CODE)
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    25
    labels = request.GET.get('labels',None)
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    26
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    27
    if not labels:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    28
        return HttpResponse(content=json.dumps({}), mimetype='application/json')
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    29
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    30
    labelslist = [lbl.strip() for lbl in labels.split(",")]
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    31
    masters = []
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    32
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    33
    for lbl in labelslist:
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
    34
        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
    35
        if len(labelqs) > 0:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    36
            tag = labelqs.get()
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    37
            if tag.dbpedia_fields:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    38
                masters.append(tag.dbpedia_fields)
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    39
    
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
    40
    translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=lang)
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    41
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    42
    translations = dict([(t.master.label, t.label) for t in translationqs])
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    43
    
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    44
    return HttpResponse(content=json.dumps(translations), mimetype='application/json')
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    45
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    46
def subcat(category, globtags, level, max_level ):
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    47
    # recursive function used by cattree
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    48
    catlabel = category.label
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    49
    tags = Tag.objects.filter(wp_categories__wp_category = category).distinct()
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    50
    taglabels = [k for k in dict([(t.label,t.label) for t in tags])]
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    51
    resobj = {
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    52
          'category': category.label,
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    53
          'tags': [],
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    54
          'contents': []
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    55
          }
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    56
    for label in taglabels:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    57
        if label == catlabel:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    58
            globtags[label] = {'level': level, 'access': resobj }
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    59
        else:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    60
            tag_in_list = {'label' : label, 'contents': []}
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    61
            resobj['tags'].append(tag_in_list)
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    62
            globtags[label] = {'level': (level + 1), 'access': tag_in_list }
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    63
            
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    64
    if level < max_level:
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    65
        subcats = WpCategory.objects.filter(parent_categories__parent_category = category)
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    66
        resobj['sub_categories'] = [subcat(subcats[i], globtags, level + 1, max_level ) for i in range(len(subcats))]
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    67
    return resobj
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    68
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    69
def cleantags(category):
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    70
    if category.has_key('contents') and len(category['contents']) == 0:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    71
        del category['contents']
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    72
    if category.has_key('contents'):
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    73
        category['contents'] = sorted(category['contents'], key=lambda content: -content['score'])
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    74
    if category.has_key('tags'):
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    75
        category['tags'] = [cleantags(tag) for tag in category['tags'] if len(tag['contents'])]
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    76
        if len(category['tags']) == 0:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    77
            del category['tags']
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    78
        else:
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    79
            category['tags'] = sorted(category['tags'], key=lambda tag: tag['label'])
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    80
    if category.has_key('sub_categories'):
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    81
        sub_cats = []
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    82
        for sub_cat in category['sub_categories']:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    83
            cat = cleantags(sub_cat)
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    84
            if cat.has_key('tags') or cat.has_key('sub_categories') or cat.has_key('contents'):
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    85
                sub_cats.append(cat)
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    86
        category['sub_categories'] = sorted(sub_cats, key=lambda cat: cat['category'])
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    87
        if len(category['sub_categories']) == 0:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    88
            del category['sub_categories']
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    89
    return category
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    90
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    91
def cattree(request):
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    92
    # Gets the category tree from a label
232
eb02eed5f7d1 UI Improvements (cf. B Sajus .odp)
veltr
parents: 212
diff changeset
    93
    ROOT_MAX_TAG_ORDER = 8
eb02eed5f7d1 UI Improvements (cf. B Sajus .odp)
veltr
parents: 212
diff changeset
    94
    MAX_TAG_ORDER = 8
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    95
    MAX_LEVEL = 3
212
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
    96
    LEVEL_COEFF = 5
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    97
    label = request.GET.get('label', None)
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    98
    lowerlabel = label.lower()
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    99
    globtags = {}
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   100
    resobj = None
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   101
    master_category = WpCategory.objects.filter(label__iexact=label)[0:1]
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   102
    if len(master_category):
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   103
        resobj = subcat(master_category[0], globtags, 1, MAX_LEVEL )
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   104
    
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   105
#    tag_list = [k for k in globtags]
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   106
    
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   107
#    if len(tag_list):
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   108
    contents = []
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   109
#    datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__in = tag_list, taggedsheet__order__lte = MAX_TAG_ORDER).distinct()
232
eb02eed5f7d1 UI Improvements (cf. B Sajus .odp)
veltr
parents: 212
diff changeset
   110
    datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__iexact = label, taggedsheet__order__lte = ROOT_MAX_TAG_ORDER).select_related('organisation').distinct()
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   111
    for datasheet in datasheets:
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   112
        # Calculating where we add the datasheet in the tree
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   113
        maintag = None
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   114
        maintagscore = -5
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   115
        dsscore = 0
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   116
        rootscore = 0
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   117
        for ts in TaggedSheet.objects.select_related('tag','datasheet').filter(datasheet__id=datasheet.id,order__lte=MAX_TAG_ORDER):
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   118
            label = ts.tag.label
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   119
            if globtags.has_key(label):
212
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   120
                score = LEVEL_COEFF * globtags[label]['level'] - ts.order
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   121
                if score > maintagscore:
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   122
                    maintagscore = score
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   123
                    maintag = label
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   124
                    dsscore = (MAX_TAG_ORDER - ts.order)
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   125
                if label.lower() == lowerlabel:
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   126
                    rootscore = (ROOT_MAX_TAG_ORDER - ts.order)
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   127
        if maintag is not None:
212
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   128
            globtags[maintag]['access']['contents'].append({
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   129
                'id': datasheet.id,
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   130
                'title': datasheet.title,
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   131
                'url': datasheet.url,
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   132
                'description': datasheet.description,
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   133
                'hda_id': datasheet.hda_id,
232
eb02eed5f7d1 UI Improvements (cf. B Sajus .odp)
veltr
parents: 212
diff changeset
   134
                'organization': datasheet.organisation.name,
eb02eed5f7d1 UI Improvements (cf. B Sajus .odp)
veltr
parents: 212
diff changeset
   135
                'score': max(dsscore, rootscore)
212
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   136
            })
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   137
    cleantags(resobj)
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   138
                    
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   139
#        resobj['contents'] = [{'id': d.id, 'title': d.title, 'tags': [t.label for t in d.tags.filter(taggedsheet__order__lte=5)]} for d in datasheets]
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   140
    
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   141
    return HttpResponse(content=json.dumps(resobj), mimetype='application/json')
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   142
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
def sessioninfo(request):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
    data = json.loads(request.GET.get('data', "{}"))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
    write = False
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
    if 'sessionid' in request.GET:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
        request.session['sessionid'] = request.GET['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
    if 'sessionkey' in request.GET:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
        request.session['sessionkey'] = request.GET['sessionkey']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
    if 'sessionid' in request.session:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        sessionid = request.session['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        if HdaSession.objects.filter(sessionid=sessionid).count() == 1:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
            sessionkey = request.session.get('sessionkey',None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
            hm = hmac.new(settings.SECRET_KEY, sessionid)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
            if hm.hexdigest() == sessionkey:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
                write = True            
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
        else:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
            del request.session['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
    if 'sessionid' not in request.session:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
        sessionid = unicode(uuid.uuid1())
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
        HdaSession.objects.create(sessionid=sessionid, data=json.dumps({}))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
        write = True
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
        request.session['sessionid'] = sessionid
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
        request.session['sessionkey'] = hmac.new(settings.SECRET_KEY, sessionid).hexdigest()
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
    if write and data:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
        HdaSession.objects.filter(sessionid=sessionid).update(data=json.dumps(data))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
    else:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
        data = HdaSession.objects.get(sessionid=sessionid).data
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
        data = json.loads(data) if data else {}           
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
         
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
    resobj = {'data': data, "write_allowed" : write, "sessionid": sessionid }
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
    if write:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
        resobj['sessionkey'] = request.session['sessionkey']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
        
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
    return HttpResponse(content=json.dumps(resobj), mimetype='application/json')
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
def tagsearch(request):
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
    q = request.GET.get('term',None)
238
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   187
    maxcount = int(request.GET.get('count','40'))
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   188
    lang = request.GET.get('lang',request.LANGUAGE_CODE)
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   189
    
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   190
    stemming_langs = [ 'fr', 'en', 'de', 'it' ]
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   191
    # For Japanese, there are no word boundaries, we should not use the regexp in that case
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   192
    no_translate_langs = [ 'fr' ]
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   193
    
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   194
    if q:
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   195
        lq = q.lower()
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   196
        qs = Tag.objects.select_related('dbpedia_fields').filter(datasheet__validated=True)
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   197
        qrx = '(\\m|\\b)%s'%q
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   198
        if lang in no_translate_langs:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   199
            if lang in stemming_langs:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   200
                qs = qs.filter( label__iregex = qrx )
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   201
            else:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   202
                qs = qs.filter( label__icontains = q )
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   203
        else:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   204
            if lang in stemming_langs:
243
1f2840354865 correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents: 238
diff changeset
   205
                qs = qs.filter(dbpedia_fields__translations__label__iregex=qrx, dbpedia_fields__translations__language_code=lang, dbpedia_fields__translations__is_label_translated = True)
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   206
            else:
243
1f2840354865 correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents: 238
diff changeset
   207
                qs = qs.filter(dbpedia_fields__translations__label__icontains=q, dbpedia_fields__translations__language_code=lang, dbpedia_fields__translations__is_label_translated = True)
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   208
    else:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   209
        qs = Tag.objects.filter(~Q(dbpedia_uri = None))
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   210
           
238
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   211
    qs = qs.annotate(nb=Count('datasheet',distinct=True)).order_by('-nb')[:maxcount]
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   212
    
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   213
    qslist = list(qs)
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   214
    
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   215
    if lang in no_translate_langs:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   216
        translations = {}
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   217
    else:
243
1f2840354865 correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents: 238
diff changeset
   218
        transqs = DbpediaFieldsTranslation.objects.filter(master__tag__in = qslist, language_code=lang, is_label_translated=True).select_related("master")
1f2840354865 correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents: 238
diff changeset
   219
        translations = dict([(tr.master.tag_id, {'label':tr.label,'abstract':tr.abstract, 'is_label_translated': tr.is_label_translated}) for tr in transqs])
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   220
    
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   221
    res = []
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
    
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   223
    for t in qslist:
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   224
        dbfields = t.dbpedia_fields
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   225
        resobj = {'original_label':t.label,'nb':t.nb}
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   226
        resobj['thumbnail'] = dbfields.thumbnail if dbfields is not None else None
243
1f2840354865 correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents: 238
diff changeset
   227
        
1f2840354865 correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents: 238
diff changeset
   228
#        if t.id in translations and not translations[t.id].get('is_label_translated', True):
1f2840354865 correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents: 238
diff changeset
   229
#            continue
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   230
        if t.id in translations:
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   231
            resobj['value'] = translations[t.id]['label']
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   232
            resobj['abstract'] = translations[t.id]['abstract']
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   233
        else:
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   234
            resobj['value'] = t.label
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   235
            resobj['abstract'] = dbfields.abstract if dbfields is not None else None
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   236
        if q is None or resobj['value'].lower().find(lq) != -1:
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   237
            res.append(resobj)
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   238
    
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   239
    return HttpResponse(content=json.dumps(res), mimetype='application/json')
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
205
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   241
def catsearch(request):
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   242
    
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   243
    q = request.GET.get('term',None)
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   244
    
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   245
    # On ne récupère que les catégories qui sont également des tags
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   246
    qrx = '(\\m|\\b)%s'%q
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   247
    qs = Tag.objects.filter(label__iregex=q)
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   248
    
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   249
    labels = [tag.label for tag in qs]
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   250
    
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   251
    qs = WpCategory.objects.annotate(nb=Count('child_categories__child_category__tags')).filter(label__in = labels, nb__gt=0)
205
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   252
    
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   253
    res = [{'value':t.label} for t in qs]
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   254
    
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   255
    return HttpResponse(content=json.dumps(res), mimetype='application/json')
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
def filter(request):
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   258
    
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   259
    lang = request.GET.get('lang',request.LANGUAGE_CODE)
158
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   260
    periode = request.GET.get('period',None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   261
    label = request.GET.get('label', None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   262
    country = request.GET.get('country', None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   263
    contentlist = request.GET.get('contentlist', None)
238
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   264
    max_tag_order = int(request.GET.get('mto', '12'))
158
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   265
    content_count = request.GET.get('contentcount', 12)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   266
    tag_count = request.GET.get('tagcount', 30)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   267
    
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   268
    key_parts = ("filter",lang,periode,label,country,contentlist,max_tag_order,content_count,tag_count)
163
e4ff12317fa3 correct accent in keys
ymh <ymh.work@gmail.com>
parents: 162
diff changeset
   269
    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
   270
    
e4ff12317fa3 correct accent in keys
ymh <ymh.work@gmail.com>
parents: 162
diff changeset
   271
    cache_key = fix_cache_key("-".join(key_parts))
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   272
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   273
    outputstr = cache.get(cache_key)
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   274
    
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   275
    if outputstr is None:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   276
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   277
        matchtagids = []
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
        
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   279
        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
   280
        countryqs = Country.objects
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   281
        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
   282
        yearqs = TagYears.objects
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   283
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   284
        contentqs = Datasheet.objects.filter(validated=True)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   285
        labeltranslations = []
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
        if label or periode or country or contentlist :
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   288
            matchtagqslist = []
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
            
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   290
            if periode:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   291
                years = periode.split(",")
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   292
                start_year = int(years[0])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   293
                end_year = int(years[0:2][-1])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   294
                delta = max(1, (end_year-start_year)/2)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   295
                minstart = start_year - delta
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   296
                maxend = end_year + delta
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   297
                matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   298
                                                years__end_year__gte = start_year, 
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   299
                                                years__start_year__lte = end_year,
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   300
                                                years__end_year__lte = maxend,
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   301
                                                years__start_year__gte = minstart,
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   302
                                                )
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   303
                matchtagqslist.append(matchtagqs)
154
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
            if label:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   306
                for txtlbl in label.split(","):
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   307
                    matchtagqs = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = txtlbl.strip())
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   308
                    matchtagqslist.append(matchtagqs)
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   309
                
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   310
            if country:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   311
                for country_uri in country.split(","):
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   312
                    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
   313
                    matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids]
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   314
                    matchtagqslist.append(matchtagqs)
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   315
            if contentlist:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   316
                contentqs = contentqs.filter(id__in = contentlist.split(","))
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   317
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   318
            tagcond = None
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   319
            tagcondid = None
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   320
            for matchtagqs in matchtagqslist:
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   321
                newcond = Q(id__in = TaggedSheet.objects.filter(tag__in = copy.deepcopy(matchtagqs), order__lte = max_tag_order).values('datasheet_id'))
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   322
                newcondid = Q(id__in = matchtagqs)
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   323
                tagcond = newcond if tagcond is None else (tagcond & newcond)
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   324
                tagcondid = newcondid if tagcondid is None else (tagcondid | newcondid)
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   325
            
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   326
            contentqs = contentqs.filter(tagcond).distinct()
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   327
            matchtagidsqs = list(Tag.objects.select_related("dbpedia_fields").only("id").filter(tagcondid))
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   328
            matchtagids = [t.id for t in matchtagidsqs]            
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   329
            masters = [t.dbpedia_fields for t in matchtagidsqs if t.dbpedia_fields is not None]
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   330
            
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   331
            translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=lang)    
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   332
            labeltranslations = [{'label':t.master.label, 'translated_label':t.label} for t in translationqs]
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   333
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   334
            
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   335
            tagqs = tagqs.filter(datasheet__in = contentqs)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   336
            countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   337
            discqs = discqs.filter(datasheet__in = contentqs)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   338
            yearqs = yearqs.filter(tag__taggedsheet__datasheet__in = contentqs)
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents: 119
diff changeset
   339
            
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   340
        if contentlist is None:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   341
            contentqs.order_by('?')
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   342
                        
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   343
        cont_count = contentqs.count()
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   344
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   345
        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
   346
        contentids = contenus.keys()
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   347
        
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   348
        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
   349
        for dse in qs:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   350
            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
   351
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   352
        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
   353
        
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   354
        transqs = DbpediaFieldsTranslation.objects.filter(master__in = [ts.tag.dbpedia_fields for ts in qs], language_code = lang)
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   355
        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
   356
        
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   357
        for ts in qs:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   358
            match_tag = ts.tag.id in matchtagids
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   359
            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
   360
            
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   361
            if match_tag:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   362
                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
   363
            
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   364
        if contentlist is None:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   365
            contenus = sorted(contenus.values(),key=lambda e: -e['score'])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   366
        else:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   367
            contenus = contenus.values()
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   368
    
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   369
        #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
   370
        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
   371
        #.select_related('dbpedia_fields')
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   372
        # 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
   373
        # contournement bug https://code.djangoproject.com/ticket/17144
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   374
        tagqs.query.clear_select_fields()
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   375
        tagqs.query.add_fields(['id','label'], False)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   376
        tagqs.query.set_group_by()
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   377
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   378
        tagqslist = list(tagqs)
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   379
        
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   380
        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
   381
    
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   382
        transqs = DbpediaFieldsTranslation.objects.filter(master__in = dbpediafields.values(), language_code = lang)
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   383
        translations = dict([(trans.master_id,trans.label) for trans in transqs])
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   384
    
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   385
        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
   386
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   387
        countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet'))
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   388
        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
   389
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   390
        discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10])
127
8642f1fb6499 Integration scripts django et html
veltr
parents: 122
diff changeset
   391
            
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   392
        transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in discqslist], language_code = lang)
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   393
        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
   394
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   395
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   396
        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
   397
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   398
        years = {}
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   399
        yearqs = yearqs.annotate(nb=Count('tag__taggedsheet'))
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   400
        for ty in yearqs:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   401
            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
   402
                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
   403
                
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   404
        yearchange = []
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   405
        for year in sorted(years.keys()):
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   406
            score = years[year]
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   407
            if year < 2011:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   408
                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
   409
                    yearchange.append({'year': year, 'score': score})
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   410
                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
   411
                    yearchange.append({'year': year+1, 'score': 0})
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   412
    
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   413
        tag_translations = {}
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   414
        for t in itertools.chain(labeltranslations,disciplines,tags):
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   415
            tag_translations[t['label']] = t['translated_label']
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   416
        for c in contenus:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   417
            for t in c['tags']:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   418
                tag_translations[t['label']] = t['translated_label']
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   419
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   420
        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
   421
        outputstr = json.dumps(output)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   422
        cache.set(cache_key, outputstr)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   423
        
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   424
    return HttpResponse(content=outputstr, mimetype='application/json')
238
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   425
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   426
def contentsbytag(request):
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   427
    
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   428
    lang = request.GET.get('lang',request.LANGUAGE_CODE)
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   429
    label = request.GET.get('label', None).strip().lower()
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   430
    max_tag_order = int(request.GET.get('mto', '30'))
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   431
    content_count = int(request.GET.get('contentcount', '30'))
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   432
    
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   433
    cache_key = fix_cache_key("contentsbytag-%s"%unicode(label).encode("utf-8"))
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   434
    
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   435
    outputstr = cache.get(cache_key)
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   436
    
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   437
    if outputstr is None:
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   438
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   439
        matchtagids = []
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   440
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   441
        contentqs = Datasheet.objects.filter(validated=True)
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   442
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   443
        no_translate_langs = [ 'fr' ]
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   444
        if lang in no_translate_langs:
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   445
            tagsqs = Tag.objects.filter( label__iexact = label )
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   446
        else:
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   447
            tagsqs = Tag.objects.select_related('dbpedia_fields').filter(dbpedia_fields__translations__label__iexact = label, dbpedia_fields__translations__language_code=lang)
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   448
            
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   449
        contentqs = contentqs.select_related('taggedsheet__tag').filter(taggedsheet__tag__in=tagsqs).distinct()
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   450
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   451
        cont_count = contentqs.count()
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   452
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   453
        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]])
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   454
        contentids = contenus.keys()
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   455
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   456
        qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids)
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   457
        for dse in qs:
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   458
            contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude}
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   459
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   460
        qs = list(TaggedSheet.objects.select_related('tag', 'tag__dbpedia_fields').filter(datasheet__in = contentids).order_by('order'))
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   461
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   462
        if lang in no_translate_langs:
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   463
            translations = {}
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   464
        else:
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   465
            transqs = DbpediaFieldsTranslation.objects.filter(master__in = [ts.tag.dbpedia_fields for ts in qs], language_code = lang)
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   466
            translations = dict([(trans.master_id,trans.label) for trans in transqs])
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   467
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   468
        for ts in qs:
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   469
            translated_label = translations.get(ts.tag.dbpedia_fields.id, ts.tag.label) if ts.tag.dbpedia_fields is not None else ts.tag.label
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   470
            match_tag = ( label == translated_label.lower() )
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   471
            contenus[ts.datasheet_id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag , 'translated_label': translated_label})
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   472
            
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   473
            if match_tag:
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   474
                contenus[ts.datasheet_id]['score'] += 2*max_tag_order - ts.order
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   475
            
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   476
        contenus = sorted(contenus.values(),key=lambda e: -e['score'])
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   477
        
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   478
        output = {'count': cont_count, 'contents': contenus}
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   479
        outputstr = json.dumps(output)
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   480
        cache.set(cache_key, outputstr)
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   481
        
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   482
    return HttpResponse(content=outputstr, mimetype='application/json')
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   483
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   484
def subtree(tree):
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   485
    MAX_TAG_ORDER = 8
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   486
    label = tree['label']
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   487
    sub = tree.get('contents',[])
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   488
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   489
    datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__iexact = label, taggedsheet__order__lte = MAX_TAG_ORDER).annotate(tagorder=Min('taggedsheet__order')).select_related('organisation').distinct()
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   490
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   491
    contents = [{ 'description': ds.description, 'title': ds.title, 'url': ds.url, 'score': MAX_TAG_ORDER - ds.tagorder, 'id': ds.id, 'hda_id': ds.hda_id, 'organization': ds.organisation.name } for ds in datasheets]
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   492
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   493
    contents = sorted(contents, key=lambda e: -e['score'])
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   494
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   495
    res = { 'category': label }
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   496
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   497
    if len(contents):
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   498
        res['contents'] = contents
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   499
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   500
    if len(sub):
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   501
        subcats = [subtree(st) for st in sub]
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   502
        subcats = [sc for sc in subcats if len(sc.get('contents',[])) or len(sc.get('sub_categories',[]))]
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   503
        res['sub_categories'] = subcats
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   504
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   505
    return res
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   506
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   507
def filltree(request):
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   508
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   509
    tree = request.GET.get('tree','{}')
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   510
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   511
    treeobj = json.loads(tree)
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   512
    
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   513
    res = subtree(treeobj)
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   514
        
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   515
    return HttpResponse(content=json.dumps(res), mimetype='application/json')