src/hdalab/views/ajax.py
author ymh <ymh.work@gmail.com>
Wed, 11 Apr 2018 12:19:47 +0200
branchdocumentation
changeset 693 09e00f38d177
parent 620 f45d7494332e
permissions -rw-r--r--
Add hdabo/hdalab documentations
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
290
fb86765b4c54 node placer classes
cavaliet
parents: 289
diff changeset
    14
from hdalab.models.categories import WpCategory
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
288
0bb9c29cd41d renkan first step : link, views and get put for json
cavaliet
parents: 286
diff changeset
    17
import json
119
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
279
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
    22
import logging
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
    23
logger = logging.getLogger(__name__)
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
    24
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    25
def tagtranslation(request):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    26
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    27
    Vue donnant des traductions de label de tag pour une langue.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    28
    la recherche se fait dans les objets :class:`hdalab.models.DbpediaFieldsTranslation`.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    29
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    30
    Paramêtres GET:
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    31
        :var lang: La langue demandée
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    32
        :var labels: Un ou plusieurs labels de tag (séparateur : ",")
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    33
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    34
    Réponse (application/json):
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    35
        Un dictionnaire sous la forme ::
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    36
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    37
            {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    38
                "<label1>": "<translation>",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    39
                "<label2>": "<translation>"
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    40
            }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    41
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    42
    """
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
    43
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
    44
    lang = request.GET.get('lang',request.LANGUAGE_CODE)
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    45
    labels = request.GET.get('labels',None)
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    46
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    47
    if not labels:
545
c752fdee555b Migration to django 1.7
ymh <ymh.work@gmail.com>
parents: 445
diff changeset
    48
        return HttpResponse(content=json.dumps({}), content_type='application/json')
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
    49
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    50
    labelslist = [lbl.strip() for lbl in labels.split(",")]
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    51
    masters = []
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
    52
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    53
    for lbl in labelslist:
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
    54
        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
    55
        if len(labelqs) > 0:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    56
            tag = labelqs.get()
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    57
            if tag.dbpedia_fields:
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    58
                masters.append(tag.dbpedia_fields)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
    59
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
    60
    translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=lang)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
    61
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
    62
    translations = dict([(t.master.label, t.label) for t in translationqs])
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
    63
545
c752fdee555b Migration to django 1.7
ymh <ymh.work@gmail.com>
parents: 445
diff changeset
    64
    return HttpResponse(content=json.dumps(translations), content_type='application/json')
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    65
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    66
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    67
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    68
def subcat(category, globtags, level, max_level):
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    69
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    70
    Méthode récursive utilisée pour reconstruire un arbre de catégories.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    71
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    72
    :param object category: La catégorie racine.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    73
    :param dict globtags: dictionnaire flobal des tags.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    74
    :param int level: Niveau de récursion.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    75
    :param int max_level: Niveau maximum de récursion (level <= max_level)
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    76
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    77
    :returns: L'arbre des catégories.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
    78
    """
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    79
    # recursive function used by cattree
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    80
    catlabel = category.label
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    81
    tags = Tag.objects.filter(wp_categories__wp_category = category).distinct()
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    82
    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
    83
    resobj = {
252
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
    84
          'label': category.label,
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
    85
          'themes': [],
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    86
          'contents': []
252
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
    87
      }
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    88
    for label in taglabels:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    89
        if label == catlabel:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    90
            globtags[label] = {'level': level, 'access': resobj }
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    91
        else:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    92
            tag_in_list = {'label' : label, 'contents': []}
252
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
    93
            resobj['themes'].append(tag_in_list)
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    94
            globtags[label] = {'level': (level + 1), 'access': tag_in_list }
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
    95
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    96
    if level < max_level:
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
    97
        subcats = WpCategory.objects.filter(parent_categories__parent_category = category)
252
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
    98
        resobj['themes'] += [subcat(subcats[i], globtags, level + 1, max_level ) for i in range(len(subcats))]
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
    99
    return resobj
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   100
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   101
def cleantags(category):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   102
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   103
    Methode annexe qui nettoie recursivement un arbre de catégorie. elle effectue les actions suivantes:
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   104
      - retire les clefs correspondant à des listes vides ('contents' et 'themes')
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   105
      - trie les listes 'themes' par label
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   106
      - trie les listes 'contents' par score
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   107
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   108
    :param category: la catégorie racine où commencer le traitement.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   109
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   110
    """
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   111
    if category.has_key('contents') and len(category['contents']) == 0:
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   112
        del category['contents']
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   113
    if category.has_key('contents'):
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   114
        category['contents'] = sorted(category['contents'], key=lambda content: -content['score'])
252
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   115
    if category.has_key('themes'):
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   116
        themes = []
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   117
        for theme in category['themes']:
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   118
            clean_theme = cleantags(theme)
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   119
            if clean_theme.has_key('themes') or clean_theme.has_key('contents'):
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   120
                themes.append(clean_theme)
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   121
        category['themes'] = sorted(themes, key=lambda cat: cat['label'])
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   122
        if len(category['themes']) == 0:
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   123
            del category['themes']
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   124
    return category
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   125
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   126
def cattree(request):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   127
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   128
    Contruit l'arbre de catégorie pour un label.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   129
    Les contenus attachés aux noeud sont des listes de fiches hdalab triées par score.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   130
    Le score pour une fiche est fonction de ses tags, de leur ordre, de leur présence dnas l'arbre des catégories et de leur hauteur dans l'arbre des catégories.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   131
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   132
    Paramêtres GET :
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   133
        :var label: Le label
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   134
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   135
    Réponse (application/json):
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   136
        Un json représentant l'arbre des catégories avec pour chaque noeud une liste ordonnée de fiches liée à la catégorie.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   137
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   138
        exemple ::
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   139
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   140
            {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   141
                "label": "<label1>",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   142
                "themes": [ { "label": "<label1.1>", "themes": [...], "contents": [...]}, ...],
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   143
                "contents": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   144
                    {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   145
                        "organization": "Ciclic",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   146
                        "description": "Ciclic propose...",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   147
                        "score": 7,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   148
                        "title": "Vocabulaire de l'analyse filmique...",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   149
                        "url": "http://upopi.ciclic.fr/vocabulaire/definition/sceance-11",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   150
                        "hda_id": "5879",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   151
                        "organization_url": "http://www.ciclic.fr/",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   152
                        "id": 14852
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   153
                    },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   154
                    ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   155
                ]
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   156
            }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   157
    """
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   158
    # Gets the category tree from a label
232
eb02eed5f7d1 UI Improvements (cf. B Sajus .odp)
veltr
parents: 212
diff changeset
   159
    ROOT_MAX_TAG_ORDER = 8
eb02eed5f7d1 UI Improvements (cf. B Sajus .odp)
veltr
parents: 212
diff changeset
   160
    MAX_TAG_ORDER = 8
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   161
    MAX_LEVEL = 3
212
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   162
    LEVEL_COEFF = 5
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   163
    label = request.GET.get('label', None)
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   164
    lowerlabel = label.lower()
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   165
    globtags = {}
603
979d9263c1c6 correct category display when no results
ymh <ymh.work@gmail.com>
parents: 545
diff changeset
   166
    resobj = {}
204
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   167
    master_category = WpCategory.objects.filter(label__iexact=label)[0:1]
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   168
    if len(master_category):
0a1744477bc1 Added category tree Ajax API + Tables
veltr
parents: 175
diff changeset
   169
        resobj = subcat(master_category[0], globtags, 1, MAX_LEVEL )
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   170
324
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   171
    #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
   172
    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
   173
    for datasheet in datasheets:
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   174
        # Calculating where we add the datasheet in the tree
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   175
        maintag = None
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   176
        maintagscore = -5
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   177
        dsscore = 0
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   178
        rootscore = 0
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   179
        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
   180
            label = ts.tag.label
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   181
            if globtags.has_key(label):
212
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   182
                score = LEVEL_COEFF * globtags[label]['level'] - ts.order
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   183
                if score > maintagscore:
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   184
                    maintagscore = score
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   185
                    maintag = label
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   186
                    dsscore = (MAX_TAG_ORDER - ts.order)
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   187
                if label.lower() == lowerlabel:
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   188
                    rootscore = (ROOT_MAX_TAG_ORDER - ts.order)
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   189
        if maintag is not None:
212
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   190
            globtags[maintag]['access']['contents'].append({
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   191
                'id': datasheet.id,
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   192
                'title': datasheet.title,
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   193
                'url': datasheet.url,
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   194
                'description': datasheet.description,
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   195
                'hda_id': datasheet.hda_id,
232
eb02eed5f7d1 UI Improvements (cf. B Sajus .odp)
veltr
parents: 212
diff changeset
   196
                'organization': datasheet.organisation.name,
253
1113c3874dd6 Added Datasheet View
veltr
parents: 252
diff changeset
   197
                'organization_url': datasheet.organisation.website,
232
eb02eed5f7d1 UI Improvements (cf. B Sajus .odp)
veltr
parents: 212
diff changeset
   198
                'score': max(dsscore, rootscore)
212
3e1193c6dce3 Display improvements on category tree
veltr
parents: 208
diff changeset
   199
            })
603
979d9263c1c6 correct category display when no results
ymh <ymh.work@gmail.com>
parents: 545
diff changeset
   200
    if resobj:
979d9263c1c6 correct category display when no results
ymh <ymh.work@gmail.com>
parents: 545
diff changeset
   201
        cleantags(resobj)
605
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   202
    elif len(master_category):
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   203
        resobj = {
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   204
            'label': master_category[0].label,
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   205
            'themes': [],
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   206
            'contents': []
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   207
        }
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   208
    else:
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   209
        resobj = {
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   210
            'label': label,
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   211
            'themes': [],
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   212
            'contents': []
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   213
        }
40d8113560e4 correct cat tree display when no results
ymh <ymh.work@gmail.com>
parents: 603
diff changeset
   214
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   215
545
c752fdee555b Migration to django 1.7
ymh <ymh.work@gmail.com>
parents: 445
diff changeset
   216
    return HttpResponse(content=json.dumps(resobj), content_type='application/json')
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   217
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   218
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
def sessioninfo(request):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   220
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   221
    Vue gérant les session Hda permettant de sauvegarder un état d'interface.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   222
    Note : Cette vue n'est pas mappée dans le module `hdalab.url`.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   223
    """
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   224
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
    data = json.loads(request.GET.get('data', "{}"))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
    write = False
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   227
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
    if 'sessionid' in request.GET:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
        request.session['sessionid'] = request.GET['sessionid']
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
    if 'sessionkey' in request.GET:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
        request.session['sessionkey'] = request.GET['sessionkey']
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   232
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
    if 'sessionid' in request.session:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
        sessionid = request.session['sessionid']
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   235
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
        if HdaSession.objects.filter(sessionid=sessionid).count() == 1:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
            sessionkey = request.session.get('sessionkey',None)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
            hm = hmac.new(settings.SECRET_KEY, sessionid)
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
            if hm.hexdigest() == sessionkey:
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   240
                write = True
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
        else:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
            del request.session['sessionid']
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   243
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
    if 'sessionid' not in request.session:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
        sessionid = unicode(uuid.uuid1())
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
        HdaSession.objects.create(sessionid=sessionid, data=json.dumps({}))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
        write = True
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
        request.session['sessionid'] = sessionid
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        request.session['sessionkey'] = hmac.new(settings.SECRET_KEY, sessionid).hexdigest()
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   250
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
    if write and data:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
        HdaSession.objects.filter(sessionid=sessionid).update(data=json.dumps(data))
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
    else:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
        data = HdaSession.objects.get(sessionid=sessionid).data
290
fb86765b4c54 node placer classes
cavaliet
parents: 289
diff changeset
   255
        data = json.loads(data) if data else {}
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   256
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
    resobj = {'data': data, "write_allowed" : write, "sessionid": sessionid }
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
    if write:
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
        resobj['sessionkey'] = request.session['sessionkey']
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   260
545
c752fdee555b Migration to django 1.7
ymh <ymh.work@gmail.com>
parents: 445
diff changeset
   261
    return HttpResponse(content=json.dumps(resobj), content_type='application/json')
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
def tagsearch(request):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   265
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   266
    Vue permettant la recherche dans les tag.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   267
    La recherche se fait dans les objets :class:`hdabo.models.Tag`, :class:`hdalab.models.DbpediaFields` et :class:`hdalab.models.DbpediaFieldsTranslation`.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   268
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   269
    Paramêtres GET:
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   270
        :var (str) term: Le terme à rechercher.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   271
        :var (str) lang: La langue dans laquelle il faut faire la recherche.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   272
        :var (int) count: Le nombre maximum de résultat.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   273
        :var (bool) count_notices: Ajoute ou pas le nombre de notices par tag.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   274
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   275
    Réponse (application/json):
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   276
        Une liste comprenant les résultats de la recherche.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   277
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   278
        exemple ::
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   279
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   280
            [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   281
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   282
                    "original_label": "Cathédrale Notre-Dame de Chartres",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   283
                    "url": "http://fr.wikipedia.org/wiki/Cath%C3%A9drale_Notre-Dame_de_Chartres",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   284
                    "abstract": "La cathédrale Notre-Dame de Chartres ...",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   285
                    "value": "Cathédrale Notre-Dame de Chartres",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   286
                    "thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Chartres_Cath+Gare.JPG?width=300",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   287
                    "nb": 7
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   288
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   289
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   290
                    "original_label": "Cathédrale Notre-Dame de Paris",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   291
                    "url": "http://fr.wikipedia.org/wiki/Cath%C3%A9drale_Notre-Dame_de_Paris",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   292
                    "abstract": "La cathédrale Notre-Dame de Paris...",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   293
                    "value": "Cathédrale Notre-Dame de Paris",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   294
                    "thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Notre_Dame_de_Paris_DSC_0846w.jpg?width=300",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   295
                    "nb": 6
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   296
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   297
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   298
            ]
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   299
    """
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   300
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
    q = request.GET.get('term',None)
238
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   302
    maxcount = int(request.GET.get('count','40'))
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   303
    lang = request.GET.get('lang',request.LANGUAGE_CODE)
324
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   304
    count_notices_str = request.REQUEST.get("count_notices")
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   305
    count_notices_bool = True
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   306
    if count_notices_str:
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   307
        count_notices_bool = {'true': True, 'false': False, "0": False, "1": True}.get(count_notices_str.lower())
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   308
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   309
    stemming_langs = [ 'fr', 'en', 'de', 'it' ]
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   310
    # For Japanese, there are no word boundaries, we should not use the regexp in that case
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   311
    no_translate_langs = [ 'fr' ]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   312
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   313
    if q:
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   314
        lq = q.lower()
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   315
        qs = Tag.objects.select_related('dbpedia_fields').filter(datasheet__validated=True)
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   316
        qrx = '(\\m|\\b)%s'%q
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   317
        if lang in no_translate_langs:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   318
            if lang in stemming_langs:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   319
                qs = qs.filter( label__iregex = qrx )
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   320
            else:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   321
                qs = qs.filter( label__icontains = q )
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   322
        else:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   323
            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
   324
                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
   325
            else:
243
1f2840354865 correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents: 238
diff changeset
   326
                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
   327
    else:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   328
        qs = Tag.objects.filter(~Q(dbpedia_uri = None))
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   329
324
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   330
    if count_notices_bool:
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   331
        qs = qs.annotate(nb=Count('datasheet',distinct=True)).order_by('-nb')[:maxcount]
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   332
    else:
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   333
        qs = qs.distinct()[:maxcount]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   334
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   335
    qslist = list(qs)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   336
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   337
    if lang in no_translate_langs:
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   338
        translations = {}
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   339
    else:
243
1f2840354865 correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents: 238
diff changeset
   340
        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
   341
        translations = dict([(tr.master.tag_id, {'label':tr.label,'abstract':tr.abstract, 'is_label_translated': tr.is_label_translated}) for tr in transqs])
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   342
150
b0a34ce6fdcf Added multilingual possibilities to tagsearch view
veltr
parents: 135
diff changeset
   343
    res = []
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   344
152
e2ceeb903de6 improve requests
ymh <ymh.work@gmail.com>
parents: 150
diff changeset
   345
    for t in qslist:
285
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   346
        if hasattr(t, 'dbpedia_fields'):
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   347
            dbfields = t.dbpedia_fields
445
a74ec9e02042 correct #35 + add tag abstract + improve translations
ymh <ymh.work@gmail.com>
parents: 443
diff changeset
   348
            resobj = {'original_label':t.label, 'url':t.wikipedia_url}
324
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   349
            if count_notices_bool:
573043a98b44 tags search bin
cavaliet
parents: 291
diff changeset
   350
                resobj['nb'] = t.nb
285
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   351
            resobj['thumbnail'] = dbfields.thumbnail if dbfields is not None else None
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   352
285
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   353
            if t.id in translations:
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   354
                resobj['value'] = translations[t.id]['label']
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   355
                resobj['abstract'] = translations[t.id]['abstract']
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   356
            else:
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   357
                resobj['value'] = t.label
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   358
                resobj['abstract'] = dbfields.abstract if dbfields is not None else None
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   359
            if q is None or resobj['value'].lower().find(lq) != -1:
f9a7cc377689 correct ajax request
cavaliet
parents: 283
diff changeset
   360
                res.append(resobj)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   361
545
c752fdee555b Migration to django 1.7
ymh <ymh.work@gmail.com>
parents: 445
diff changeset
   362
    return HttpResponse(content=json.dumps(res), content_type='application/json')
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   364
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   365
205
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   366
def catsearch(request):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   367
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   368
    Vue permettant la recherche de catégorie. On se restreint aux catégories qui sont aussi des tags.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   369
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   370
    Paramêtres GET:
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   371
        :var (str) term: Le terme à rechercher.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   372
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   373
    Réponse (application/json):
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   374
        Une liste comprenant le résultat de la recherche.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   375
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   376
        exemple ::
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   377
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   378
            [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   379
                { "value": "1982 au cinéma" },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   380
                { "value": "Cinéma italien" },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   381
                { "value": "2003 au cinéma" },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   382
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   383
            ]
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   384
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   385
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   386
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   387
205
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   388
    q = request.GET.get('term',None)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   389
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   390
    # On ne récupère que les catégories qui sont également des tags
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   391
    qrx = '(\\m|\\b)%s'%q
290
fb86765b4c54 node placer classes
cavaliet
parents: 289
diff changeset
   392
    qs = Tag.objects.filter(label__iregex=qrx)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   393
208
d4e3ea751966 Wikipedia tree algorithm changes
veltr
parents: 206
diff changeset
   394
    labels = [tag.label for tag in qs]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   395
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   396
    qs = WpCategory.objects.annotate(nb=Count('child_categories__child_category__tags')).filter(label__in = labels, nb__gt=0)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   397
205
8ff4fd1a6e9c Added categories page
veltr
parents: 204
diff changeset
   398
    res = [{'value':t.label} for t in qs]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   399
545
c752fdee555b Migration to django 1.7
ymh <ymh.work@gmail.com>
parents: 445
diff changeset
   400
    return HttpResponse(content=json.dumps(res), content_type='application/json')
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   402
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   403
119
e3ebe3545f72 first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
def filter(request):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   405
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   406
    Vue permettant de filtrer par facette des fiches HDA.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   407
    Cette méthode est en fait un simple wrapper pour la méthode `filter_generic`.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   408
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   409
    Paramêtres GET:
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   410
        :var lang: la langue de recherche (défaut: fr-fr).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   411
        :var period: Période dans laquelle doit se faire la recherche.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   412
        :var label: un mot-clef.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   413
        :var country: Une liste de pays limitant la recherche.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   414
        :var contentlist: Liste de fiches limitant la recherche.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   415
        :var mto: Ordre maximum des tags (défaut: 12).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   416
        :var contentcount: nombre de fiches maximum (défaut : 8).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   417
        :var tagcount: nombre maximum de tag (défaut: 30).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   418
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   419
    Réponse (application/json):
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   420
        Un objet comprenant le résultat de la recherche.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   421
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   422
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   423
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   424
    lang = request.GET.get('lang',request.LANGUAGE_CODE)
158
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   425
    periode = request.GET.get('period',None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   426
    label = request.GET.get('label', None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   427
    country = request.GET.get('country', None)
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   428
    contentlist = request.GET.get('contentlist', None)
238
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   429
    max_tag_order = int(request.GET.get('mto', '12'))
419
469ad9ef43e5 close #40 - 8 ressources in facette page
rougeronj
parents: 353
diff changeset
   430
    content_count = request.GET.get('contentcount', 8)
158
f105b62dce5d fix cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
   431
    tag_count = request.GET.get('tagcount', 30)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   432
289
eb479baeb2bf renkan first step with tag and datasheet nodes
cavaliet
parents: 288
diff changeset
   433
    outputstr = filter_generic(lang, periode, label, country, contentlist, max_tag_order, content_count, tag_count)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   434
545
c752fdee555b Migration to django 1.7
ymh <ymh.work@gmail.com>
parents: 445
diff changeset
   435
    return HttpResponse(content=outputstr, content_type='application/json')
289
eb479baeb2bf renkan first step with tag and datasheet nodes
cavaliet
parents: 288
diff changeset
   436
eb479baeb2bf renkan first step with tag and datasheet nodes
cavaliet
parents: 288
diff changeset
   437
419
469ad9ef43e5 close #40 - 8 ressources in facette page
rougeronj
parents: 353
diff changeset
   438
def filter_generic(lang="fr-fr", periode=None, label=None, country=None, contentlist=None, max_tag_order=12, content_count=8, tag_count=30):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   439
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   440
    Méthode de recherche par facette sur les fiches HDA.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   441
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   442
    :param str lang: La langue de recherche (défaut: "fr-fr").
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   443
    :param str period: Période d'année limitant la recherche. Le format est `<année-début>,<année-fin>` (défaut: None).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   444
    :param str label: Limite la recherche à un label de tag (défaut: None).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   445
    :param str country: Liste de pays où limiter la recherche. Le format est `<uri dbpedia pays 1>,<uri dbpedia pays 2>...` (défaut: None).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   446
    :param str contentlist: Liste d'id de fiche HDA (:class:`hdabo.models.Datasheet`) limitant la recherche. Le format est `<id1>,<id2>...` (défaut: None)
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   447
    :param int max_tag_order: Limite le nombre maximum de tag par fiche (défaut: 12).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   448
    :param int content_count: Limite le nombre de fiches résultat (défaut: 8).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   449
    :param int tag_count: Limite le nombre de tag dans le résultat (défaut: 30).
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   450
    :rtype: string
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   451
    :returns: Un objet json sérialisé comprenant les résultats de la recherche pour les différentes facettes.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   452
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   453
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   454
    Clefs de l'objet:
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   455
        - :count: Nombre total de fiches.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   456
        - :disciplines: Liste de disciplines artistiques en relation avec les reesultats, triée par score.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   457
        - :countries: Objet dont les clef sont des uri dbpedia de pays et les valeurs sont le nombre de fiches.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   458
        - :tags: Liste de tag triés par score.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   459
        - :sparkline: Liste d'année avec un score, triés par année. Le score est lié au nombre de fiche dont le contenu couvre l'année en question.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   460
        - :contents: Liste de fiches HDA répondant à la recherche, classée par score. Chaque fiche comporte une liste de tag.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   461
        - :tagtranslations: Objet donnant les traductions de label de tag rencontrés dans les résultats.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   462
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   463
    exemple::
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   464
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   465
        {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   466
            "count": 936,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   467
            "disciplines": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   468
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   469
                "translated_label": "Peinture",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   470
                "score": 936,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   471
                "label": "Peinture"
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   472
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   473
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   474
                "translated_label": "Sculpture",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   475
                "score": 88,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   476
                "label": "Sculpture"
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   477
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   478
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   479
            ],
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   480
            "countries": {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   481
                "http://fr.dbpedia.org/resource/Iran": 1,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   482
                "http://fr.dbpedia.org/resource/Espagne": 16,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   483
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   484
            },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   485
            "tags": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   486
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   487
                "url": "http://fr.dbpedia.org/resource/Portrait",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   488
                "id": 63452,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   489
                "translated_label": "Portrait",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   490
                "score": 179,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   491
                "wkpd_url": "http://fr.wikipedia.org/wiki/Portrait",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   492
                "label": "Portrait",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   493
                "thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Fayum02.jpg?width=300"
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   494
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   495
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   496
            ],
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   497
            "sparkline": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   498
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   499
                "score": 2,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   500
                "year": -600
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   501
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   502
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   503
                "score": 4,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   504
                "year": -500
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   505
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   506
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   507
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   508
                "score": 18,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   509
                "year": 2001
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   510
                }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   511
            ],
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   512
            "contents": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   513
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   514
                "description": "Le palais Fesch, ...",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   515
                "title": "Histoires bibliques",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   516
                "url": "http://www.musee-fesch.com/index.php/musee_fesch/content/view/ef_catalogue_sommaire/1513/%28node_id_theme%29/33459",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   517
                "tags": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   518
                    {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   519
                    "url": "http://fr.dbpedia.org/resource/Peinture",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   520
                    "id": 54648,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   521
                    "translated_label": "Peinture",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   522
                    "wkpd_url": "http://fr.wikipedia.org/wiki/Peinture",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   523
                    "label": "Peinture",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   524
                    "order": 1,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   525
                    "match": true
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   526
                    },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   527
                    ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   528
                ],
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   529
                "score": 23,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   530
                "coords": {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   531
                    "city_name": "Ajaccio",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   532
                    "latitude": 41.916667,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   533
                    "longitude": 8.733333
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   534
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   535
                "hda_id": "4448",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   536
                "id": 13855
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   537
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   538
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   539
                "description": "...",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   540
                "title": "Le XIXe siècle",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   541
                "url": "http://www.grandpalais.fr/fr/article/le-xixe-siecle",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   542
                "tags": [ ... ],
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   543
                "score": 22,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   544
                "hda_id": "5217",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   545
                "id": 13582
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   546
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   547
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   548
            ],
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   549
            "tagtranslations": {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   550
                "Paul Cézanne": "Paul Cézanne",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   551
                "Politique": "Politique",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   552
                "Poésie": "Poésie",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   553
                "Religion": "Religion",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   554
                "Empereur": "Empereur",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   555
                "Saint": "Saint",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   556
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   557
            }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   558
        }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   559
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   560
    """
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   561
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   562
    no_translate_langs = [ 'fr' ]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   563
235
b41f0b200940 Added autocomplete
veltr
parents: 232
diff changeset
   564
    key_parts = ("filter",lang,periode,label,country,contentlist,max_tag_order,content_count,tag_count)
280
56d84b0c267f update and correct different files
cavaliet
parents: 279
diff changeset
   565
    key_parts = [unicode(p).encode("ascii", "ignore") for p in key_parts]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   566
163
e4ff12317fa3 correct accent in keys
ymh <ymh.work@gmail.com>
parents: 162
diff changeset
   567
    cache_key = fix_cache_key("-".join(key_parts))
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   568
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   569
    outputstr = cache.get(cache_key)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   570
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   571
    if outputstr is None:
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   572
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   573
        matchtagids = []
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   574
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   575
        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
   576
        countryqs = Country.objects
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   577
        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
   578
        yearqs = TagYears.objects
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   579
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   580
        contentqs = Datasheet.objects.filter(validated=True)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   581
        labeltranslations = []
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   582
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   583
        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
   584
            matchtagqslist = []
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   585
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   586
            if periode:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   587
                years = periode.split(",")
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   588
                start_year = int(years[0])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   589
                end_year = int(years[0:2][-1])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   590
                delta = max(1, (end_year-start_year)/2)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   591
                minstart = start_year - delta
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   592
                maxend = end_year + delta
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   593
                matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   594
                                                years__end_year__gte = start_year,
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   595
                                                years__start_year__lte = end_year,
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   596
                                                years__end_year__lte = maxend,
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   597
                                                years__start_year__gte = minstart,
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   598
                                                )
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   599
                matchtagqslist.append(matchtagqs)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   600
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   601
            if label:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   602
                for txtlbl in label.split(","):
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   603
                    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
   604
                    matchtagqslist.append(matchtagqs)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   605
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   606
            if country:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   607
                for country_uri in country.split(","):
165
b9b93ff09ebe Remove unsemantized tags
ymh <ymh.work@gmail.com>
parents: 163
diff changeset
   608
                    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
   609
                    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
   610
                    matchtagqslist.append(matchtagqs)
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   611
            if contentlist:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   612
                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
   613
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   614
            tagcond = None
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   615
            tagcondid = None
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   616
            for matchtagqs in matchtagqslist:
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   617
                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
   618
                newcondid = Q(id__in = matchtagqs)
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   619
                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
   620
                tagcondid = newcondid if tagcondid is None else (tagcondid | newcondid)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   621
172
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   622
            contentqs = contentqs.filter(tagcond).distinct()
8f47c67c6d28 Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents: 165
diff changeset
   623
            matchtagidsqs = list(Tag.objects.select_related("dbpedia_fields").only("id").filter(tagcondid))
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   624
            matchtagids = [t.id for t in matchtagidsqs]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   625
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   626
            if lang not in no_translate_langs:
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   627
                masters = [t.dbpedia_fields for t in matchtagidsqs if t.dbpedia_fields is not None]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   628
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   629
                translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=lang)
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   630
                labeltranslations = [{'label':t.master.label, 'translated_label':t.label} for t in translationqs]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   631
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   632
            tagqs = tagqs.filter(datasheet__in = contentqs)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   633
            countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   634
            discqs = discqs.filter(datasheet__in = contentqs)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   635
            yearqs = yearqs.filter(tag__taggedsheet__datasheet__in = contentqs)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   636
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   637
        if contentlist is None:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   638
            contentqs.order_by('?')
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   639
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   640
        cont_count = contentqs.count()
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   641
443
27f71b0a772d next version of import_rdf
ymh <ymh.work@gmail.com>
parents: 419
diff changeset
   642
        logger.debug("ajax filter SQL for contentqs %s", contentqs.query)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   643
253
1113c3874dd6 Added Datasheet View
veltr
parents: 252
diff changeset
   644
        contenus = dict([(content.id, {'score' : 0, 'tags' : [], 'hda_id': content.hda_id, 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url}) for content in contentqs[0:content_count]])
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   645
        contentids = contenus.keys()
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   646
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   647
        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
   648
        for dse in qs:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   649
            contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude}
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   650
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   651
        qs = list(TaggedSheet.objects.select_related('tag', 'tag__dbpedia_fields').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order'))
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   652
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   653
        translations = {}
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   654
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   655
        if lang not in no_translate_langs:
279
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
   656
            ts_list = []
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
   657
            for ts in qs:
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
   658
                if hasattr(ts, 'tag') and hasattr(ts.tag, 'dbpedia_fields') :
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
   659
                    ts_list.append(ts.tag.dbpedia_fields)
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
   660
            transqs = DbpediaFieldsTranslation.objects.filter(master__in = ts_list, language_code = lang)
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   661
            translations = dict([(trans.master_id,trans.label) for trans in transqs])
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   662
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   663
        for ts in qs:
279
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
   664
            if hasattr(ts, 'tag') and hasattr(ts.tag, 'dbpedia_fields') :
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
   665
                match_tag = ts.tag.id in matchtagids
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   666
                contenus[ts.datasheet_id]['tags'].append({'id': ts.tag.id,
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   667
                                                          'label': ts.tag.label,
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   668
                                                          'order': ts.order,
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   669
                                                          'match': match_tag,
326
fc8961398609 enhance uri for notices and tags
cavaliet
parents: 324
diff changeset
   670
                                                          'translated_label': translations.get(ts.tag.dbpedia_fields.id, ts.tag.label) if ts.tag.dbpedia_fields is not None else ts.tag.label,
445
a74ec9e02042 correct #35 + add tag abstract + improve translations
ymh <ymh.work@gmail.com>
parents: 443
diff changeset
   671
                                                          'url': ts.tag.dbpedia_uri,
a74ec9e02042 correct #35 + add tag abstract + improve translations
ymh <ymh.work@gmail.com>
parents: 443
diff changeset
   672
                                                          'wkpd_url': ts.tag.wikipedia_url})
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   673
279
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
   674
                if match_tag:
177b508612f4 add, configure and correct hdalab to installed apps
cavaliet
parents: 272
diff changeset
   675
                    contenus[ts.datasheet_id]['score'] += 2*max_tag_order - ts.order
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   676
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   677
        if contentlist is None:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   678
            contenus = sorted(contenus.values(),key=lambda e: -e['score'])
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   679
        else:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   680
            contenus = contenus.values()
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   681
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   682
        #tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   683
        tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label', 'dbpedia_uri', 'wikipedia_url')[:tag_count]
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   684
        #.select_related('dbpedia_fields')
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   685
        # 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
   686
        # contournement bug https://code.djangoproject.com/ticket/17144
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   687
        #tagqs.query.clear_select_fields()
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   688
        #tagqs.query.add_fields(['id','label'], False)
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   689
        #tagqs.query.set_group_by()
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   690
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   691
        tagqslist = list(tagqs)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   692
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   693
        dbpediafields = dict([(df.tag_id, df) for df in DbpediaFields.objects.filter(tag__in = tagqslist)])
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   694
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   695
        if lang not in no_translate_langs:
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   696
            transqs = DbpediaFieldsTranslation.objects.filter(master__in = dbpediafields.values(), language_code = lang)
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   697
            translations = dict([(trans.master_id,trans.label) for trans in transqs])
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   698
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   699
        tags = [{'id': tag.id,
353
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents: 326
diff changeset
   700
                 'label': tag.label,
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   701
                 'score': tag.nb,
353
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents: 326
diff changeset
   702
                 'thumbnail': dbpediafields[tag.id].thumbnail if tag.id in dbpediafields else None,
326
fc8961398609 enhance uri for notices and tags
cavaliet
parents: 324
diff changeset
   703
                 'translated_label': translations.get(dbpediafields[tag.id].id, tag.label) if tag.id in dbpediafields else tag.label,
445
a74ec9e02042 correct #35 + add tag abstract + improve translations
ymh <ymh.work@gmail.com>
parents: 443
diff changeset
   704
                 'url': tag.dbpedia_uri,
a74ec9e02042 correct #35 + add tag abstract + improve translations
ymh <ymh.work@gmail.com>
parents: 443
diff changeset
   705
                 'wkpd_url': tag.wikipedia_url} for tag in tagqslist]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   706
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   707
        countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet'))
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   708
        countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs])
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   709
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   710
        discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10])
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   711
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   712
        if lang not in no_translate_langs:
353
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents: 326
diff changeset
   713
            list_dbpediafields = [tag.dbpedia_fields for tag in discqslist if tag.dbpedia_fields is not None]
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents: 326
diff changeset
   714
            transqs = DbpediaFieldsTranslation.objects.filter(master__in = list_dbpediafields, language_code = lang)
260
a15e8cb98525 Remove tag translations for french language
veltr
parents: 253
diff changeset
   715
            translations = dict([(trans.master_id,trans.label) for trans in transqs])
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   716
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   717
        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]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   718
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   719
        years = {}
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   720
        yearqs = yearqs.annotate(nb=Count('tag__taggedsheet'))
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   721
        for ty in yearqs:
286
5e98984bbfc9 correct date management (finally fully reworks !)
cavaliet
parents: 285
diff changeset
   722
            for year in range(ty.start_year, ty.end_year):
5e98984bbfc9 correct date management (finally fully reworks !)
cavaliet
parents: 285
diff changeset
   723
                years[year] = ty.nb + (years[year] if year in years else 0)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   724
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   725
        yearchange = []
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   726
        for year in sorted(years.keys()):
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   727
            score = years[year]
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   728
            if year < 2011:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   729
                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
   730
                    yearchange.append({'year': year, 'score': score})
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   731
                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
   732
                    yearchange.append({'year': year+1, 'score': 0})
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   733
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   734
        tag_translations = {}
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   735
        for t in itertools.chain(labeltranslations,disciplines,tags):
135
dd6578e36a57 translate interface
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   736
            tag_translations[t['label']] = t['translated_label']
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   737
        for c in contenus:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   738
            for t in c['tags']:
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   739
                tag_translations[t['label']] = t['translated_label']
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   740
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   741
        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
   742
        outputstr = json.dumps(output)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents: 152
diff changeset
   743
        cache.set(cache_key, outputstr)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   744
289
eb479baeb2bf renkan first step with tag and datasheet nodes
cavaliet
parents: 288
diff changeset
   745
    return outputstr
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   746
289
eb479baeb2bf renkan first step with tag and datasheet nodes
cavaliet
parents: 288
diff changeset
   747
238
f818b9430585 Corrections in Completion
veltr
parents: 235
diff changeset
   748
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   749
def subtree(tree):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   750
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   751
    Methode récursive permettant de remplir un arbre ce catégories avec les fiches HDA correspondantes.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   752
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   753
    :param tree: L'arbre de catégorie
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   754
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   755
    :returns:Un arbre de catégorie rempli de fiches HDA.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   756
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   757
    format du paramêtre `tree` ::
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   758
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   759
        {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   760
            "label": "secteur urbain",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   761
            "contents": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   762
                { "label": "banlieue",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   763
                    "contents": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   764
                        { "label": "faubourg" }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   765
                    ] },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   766
                { "label": "îlot" },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   767
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   768
            ]
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   769
        }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   770
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   771
    Exemple de retour ::
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   772
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   773
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   774
        {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   775
            "label": "secteur urbain",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   776
            "contents": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   777
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   778
                    "score": 6,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   779
                    "organization": "Institut national de l'audiovisuel ( INA )",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   780
                    "description": "Pour faire face à la ...",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   781
                    "title": "La construction des grands ensembles de banlieue : l'exemple de Sarcelles",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   782
                    "url": "http://fresques.ina.fr/jalons/fiche-media/InaEdu01075/la-construction-des-grands-ensembles-de-banlieue--l-exemple-de-sarcelles",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   783
                    "hda_id": "2090",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   784
                    "organization_url": "http://www.ina.fr",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   785
                    "id": 12360
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   786
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   787
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   788
                    "score": 6,
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   789
                    "organization": "Maison de banlieue et d'architecture",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   790
                    "description": "La Maison de banlieue et d'architecture...",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   791
                    "title": "Des ensembles assez grands. Mémoire et projets en Essonne",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   792
                    "url": "http://maisondebanlieue.fr/wp-content/uploads/2011/05/Cahier11_grands_ensembles.pdf",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   793
                    "hda_id": "5893",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   794
                    "organization_url": "http://www.maisondebanlieue.fr/",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   795
                    "id": 14821
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   796
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   797
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   798
            ]
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   799
            "themes": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   800
                {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   801
                    "label": "faubourg",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   802
                    "content":  [...],
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   803
                    "themes": [...]
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   804
                },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   805
                ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   806
            ]
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   807
        }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   808
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   809
    """
250
7c3f54ce68af Merged Completion List back into Facettes
veltr
parents: 248
diff changeset
   810
    MAX_TAG_ORDER = 16
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   811
    label = tree['label']
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   812
    sub = tree.get('contents',[])
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   813
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   814
    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()
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   815
253
1113c3874dd6 Added Datasheet View
veltr
parents: 252
diff changeset
   816
    contents = [{ 'description': ds.description, 'title': ds.title, 'url': ds.url, 'score': int((MAX_TAG_ORDER - ds.tagorder)/2), 'id': ds.id, 'hda_id': ds.hda_id, 'organization': ds.organisation.name, 'organization_url': ds.organisation.website } for ds in datasheets]
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   817
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   818
    contents = sorted(contents, key=lambda e: -e['score'])
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   819
252
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   820
    res = { 'label': label }
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   821
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   822
    if len(contents):
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   823
        res['contents'] = contents
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   824
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   825
    if len(sub):
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   826
        subcats = [subtree(st) for st in sub]
252
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   827
        subcats = [sc for sc in subcats if len(sc.get('contents',[])) or len(sc.get('themes',[]))]
a46461f8a74f Separated Thesaurus and WP Categories
veltr
parents: 250
diff changeset
   828
        res['themes'] = subcats
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   829
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   830
    return res
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   831
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   832
def filltree(request):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   833
    """
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   834
    Vue permettant d'ajouter des fiches à un arbre de catégories.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   835
    Cette méthode est en fait un simple wrapper pour la méthode :meth:`subtree`.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   836
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   837
    Paramêtres GET:
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   838
        :var tree: Serialisation json d'un arbre de catégories à remplir. exemple:
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   839
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   840
        ::
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   841
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   842
            {
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   843
                "label": "secteur urbain",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   844
                "contents": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   845
                    { "label": "banlieue",
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   846
                      "contents": [
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   847
                          { "label": "faubourg" }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   848
                      ] },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   849
                    { "label": "îlot" },
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   850
                    ...
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   851
                ]
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   852
            }
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   853
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   854
    Réponse (application/json):
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   855
        L'arbre de catégories remplis de fiches HDA (c.f. retour methode :meth:`subtree`)
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   856
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 620
diff changeset
   857
    """
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   858
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   859
    tree = request.GET.get('tree','{}')
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   860
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   861
    treeobj = json.loads(tree)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   862
248
91bc8521e3cb Ajout de la navigation par thesaurus
veltr
parents: 243
diff changeset
   863
    res = subtree(treeobj)
620
f45d7494332e upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents: 605
diff changeset
   864
545
c752fdee555b Migration to django 1.7
ymh <ymh.work@gmail.com>
parents: 445
diff changeset
   865
    return HttpResponse(content=json.dumps(res), content_type='application/json')