| author | ymh <ymh.work@gmail.com> |
| Wed, 11 Apr 2018 12:19:47 +0200 | |
| branch | documentation |
| changeset 693 | 09e00f38d177 |
| parent 620 | f45d7494332e |
| permissions | -rw-r--r-- |
|
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 | 8 |
from django.core.cache import cache |
| 248 | 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 | 13 |
from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields |
| 290 | 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 | 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 | 25 |
def tagtranslation(request): |
| 693 | 26 |
""" |
27 |
Vue donnant des traductions de label de tag pour une langue. |
|
28 |
la recherche se fait dans les objets :class:`hdalab.models.DbpediaFieldsTranslation`. |
|
29 |
||
30 |
Paramêtres GET: |
|
31 |
:var lang: La langue demandée |
|
32 |
:var labels: Un ou plusieurs labels de tag (séparateur : ",") |
|
33 |
||
34 |
Réponse (application/json): |
|
35 |
Un dictionnaire sous la forme :: |
|
36 |
||
37 |
{ |
|
38 |
"<label1>": "<translation>", |
|
39 |
"<label2>": "<translation>" |
|
40 |
} |
|
41 |
||
42 |
""" |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
43 |
|
| 235 | 44 |
lang = request.GET.get('lang',request.LANGUAGE_CODE) |
| 135 | 45 |
labels = request.GET.get('labels',None) |
46 |
||
47 |
if not labels: |
|
| 545 | 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 | 50 |
labelslist = [lbl.strip() for lbl in labels.split(",")] |
51 |
masters = [] |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
52 |
|
| 135 | 53 |
for lbl in labelslist: |
| 165 | 54 |
labelqs = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = lbl)[0:1] |
| 135 | 55 |
if len(labelqs) > 0: |
56 |
tag = labelqs.get() |
|
57 |
if tag.dbpedia_fields: |
|
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 | 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 | 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 | 64 |
return HttpResponse(content=json.dumps(translations), content_type='application/json') |
| 204 | 65 |
|
| 693 | 66 |
|
67 |
||
68 |
def subcat(category, globtags, level, max_level): |
|
69 |
""" |
|
70 |
Méthode récursive utilisée pour reconstruire un arbre de catégories. |
|
71 |
||
72 |
:param object category: La catégorie racine. |
|
73 |
:param dict globtags: dictionnaire flobal des tags. |
|
74 |
:param int level: Niveau de récursion. |
|
75 |
:param int max_level: Niveau maximum de récursion (level <= max_level) |
|
76 |
||
77 |
:returns: L'arbre des catégories. |
|
78 |
""" |
|
| 204 | 79 |
# recursive function used by cattree |
| 208 | 80 |
catlabel = category.label |
81 |
tags = Tag.objects.filter(wp_categories__wp_category = category).distinct() |
|
| 204 | 82 |
taglabels = [k for k in dict([(t.label,t.label) for t in tags])] |
83 |
resobj = { |
|
| 252 | 84 |
'label': category.label, |
85 |
'themes': [], |
|
| 204 | 86 |
'contents': [] |
| 252 | 87 |
} |
| 204 | 88 |
for label in taglabels: |
89 |
if label == catlabel: |
|
90 |
globtags[label] = {'level': level, 'access': resobj } |
|
91 |
else: |
|
92 |
tag_in_list = {'label' : label, 'contents': []} |
|
| 252 | 93 |
resobj['themes'].append(tag_in_list) |
| 204 | 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 | 96 |
if level < max_level: |
| 208 | 97 |
subcats = WpCategory.objects.filter(parent_categories__parent_category = category) |
| 252 | 98 |
resobj['themes'] += [subcat(subcats[i], globtags, level + 1, max_level ) for i in range(len(subcats))] |
| 204 | 99 |
return resobj |
100 |
||
101 |
def cleantags(category): |
|
| 693 | 102 |
""" |
103 |
Methode annexe qui nettoie recursivement un arbre de catégorie. elle effectue les actions suivantes: |
|
104 |
- retire les clefs correspondant à des listes vides ('contents' et 'themes') |
|
105 |
- trie les listes 'themes' par label |
|
106 |
- trie les listes 'contents' par score |
|
107 |
||
108 |
:param category: la catégorie racine où commencer le traitement. |
|
109 |
||
110 |
""" |
|
| 204 | 111 |
if category.has_key('contents') and len(category['contents']) == 0: |
112 |
del category['contents'] |
|
| 208 | 113 |
if category.has_key('contents'): |
114 |
category['contents'] = sorted(category['contents'], key=lambda content: -content['score']) |
|
| 252 | 115 |
if category.has_key('themes'): |
116 |
themes = [] |
|
117 |
for theme in category['themes']: |
|
118 |
clean_theme = cleantags(theme) |
|
119 |
if clean_theme.has_key('themes') or clean_theme.has_key('contents'): |
|
120 |
themes.append(clean_theme) |
|
121 |
category['themes'] = sorted(themes, key=lambda cat: cat['label']) |
|
122 |
if len(category['themes']) == 0: |
|
123 |
del category['themes'] |
|
| 204 | 124 |
return category |
125 |
||
126 |
def cattree(request): |
|
| 693 | 127 |
""" |
128 |
Contruit l'arbre de catégorie pour un label. |
|
129 |
Les contenus attachés aux noeud sont des listes de fiches hdalab triées par score. |
|
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. |
|
131 |
||
132 |
Paramêtres GET : |
|
133 |
:var label: Le label |
|
134 |
||
135 |
Réponse (application/json): |
|
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. |
|
137 |
||
138 |
exemple :: |
|
139 |
||
140 |
{ |
|
141 |
"label": "<label1>", |
|
142 |
"themes": [ { "label": "<label1.1>", "themes": [...], "contents": [...]}, ...], |
|
143 |
"contents": [ |
|
144 |
{ |
|
145 |
"organization": "Ciclic", |
|
146 |
"description": "Ciclic propose...", |
|
147 |
"score": 7, |
|
148 |
"title": "Vocabulaire de l'analyse filmique...", |
|
149 |
"url": "http://upopi.ciclic.fr/vocabulaire/definition/sceance-11", |
|
150 |
"hda_id": "5879", |
|
151 |
"organization_url": "http://www.ciclic.fr/", |
|
152 |
"id": 14852 |
|
153 |
}, |
|
154 |
... |
|
155 |
] |
|
156 |
} |
|
157 |
""" |
|
| 204 | 158 |
# Gets the category tree from a label |
| 232 | 159 |
ROOT_MAX_TAG_ORDER = 8 |
160 |
MAX_TAG_ORDER = 8 |
|
| 204 | 161 |
MAX_LEVEL = 3 |
| 212 | 162 |
LEVEL_COEFF = 5 |
| 204 | 163 |
label = request.GET.get('label', None) |
| 208 | 164 |
lowerlabel = label.lower() |
| 204 | 165 |
globtags = {} |
|
603
979d9263c1c6
correct category display when no results
ymh <ymh.work@gmail.com>
parents:
545
diff
changeset
|
166 |
resobj = {} |
| 204 | 167 |
master_category = WpCategory.objects.filter(label__iexact=label)[0:1] |
168 |
if len(master_category): |
|
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 | 171 |
#datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__in = tag_list, taggedsheet__order__lte = MAX_TAG_ORDER).distinct() |
| 232 | 172 |
datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__iexact = label, taggedsheet__order__lte = ROOT_MAX_TAG_ORDER).select_related('organisation').distinct() |
| 208 | 173 |
for datasheet in datasheets: |
174 |
# Calculating where we add the datasheet in the tree |
|
175 |
maintag = None |
|
176 |
maintagscore = -5 |
|
177 |
dsscore = 0 |
|
178 |
rootscore = 0 |
|
179 |
for ts in TaggedSheet.objects.select_related('tag','datasheet').filter(datasheet__id=datasheet.id,order__lte=MAX_TAG_ORDER): |
|
180 |
label = ts.tag.label |
|
181 |
if globtags.has_key(label): |
|
| 212 | 182 |
score = LEVEL_COEFF * globtags[label]['level'] - ts.order |
| 208 | 183 |
if score > maintagscore: |
184 |
maintagscore = score |
|
185 |
maintag = label |
|
186 |
dsscore = (MAX_TAG_ORDER - ts.order) |
|
187 |
if label.lower() == lowerlabel: |
|
188 |
rootscore = (ROOT_MAX_TAG_ORDER - ts.order) |
|
189 |
if maintag is not None: |
|
| 212 | 190 |
globtags[maintag]['access']['contents'].append({ |
191 |
'id': datasheet.id, |
|
192 |
'title': datasheet.title, |
|
193 |
'url': datasheet.url, |
|
194 |
'description': datasheet.description, |
|
195 |
'hda_id': datasheet.hda_id, |
|
| 232 | 196 |
'organization': datasheet.organisation.name, |
| 253 | 197 |
'organization_url': datasheet.organisation.website, |
| 232 | 198 |
'score': max(dsscore, rootscore) |
| 212 | 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 | 216 |
return HttpResponse(content=json.dumps(resobj), content_type='application/json') |
| 135 | 217 |
|
| 693 | 218 |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
219 |
def sessioninfo(request): |
| 693 | 220 |
""" |
221 |
Vue gérant les session Hda permettant de sauvegarder un état d'interface. |
|
222 |
Note : Cette vue n'est pas mappée dans le module `hdalab.url`. |
|
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 | 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 | 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 | 265 |
""" |
266 |
Vue permettant la recherche dans les tag. |
|
267 |
La recherche se fait dans les objets :class:`hdabo.models.Tag`, :class:`hdalab.models.DbpediaFields` et :class:`hdalab.models.DbpediaFieldsTranslation`. |
|
268 |
||
269 |
Paramêtres GET: |
|
270 |
:var (str) term: Le terme à rechercher. |
|
271 |
:var (str) lang: La langue dans laquelle il faut faire la recherche. |
|
272 |
:var (int) count: Le nombre maximum de résultat. |
|
273 |
:var (bool) count_notices: Ajoute ou pas le nombre de notices par tag. |
|
274 |
||
275 |
Réponse (application/json): |
|
276 |
Une liste comprenant les résultats de la recherche. |
|
277 |
||
278 |
exemple :: |
|
279 |
||
280 |
[ |
|
281 |
{ |
|
282 |
"original_label": "Cathédrale Notre-Dame de Chartres", |
|
283 |
"url": "http://fr.wikipedia.org/wiki/Cath%C3%A9drale_Notre-Dame_de_Chartres", |
|
284 |
"abstract": "La cathédrale Notre-Dame de Chartres ...", |
|
285 |
"value": "Cathédrale Notre-Dame de Chartres", |
|
286 |
"thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Chartres_Cath+Gare.JPG?width=300", |
|
287 |
"nb": 7 |
|
288 |
}, |
|
289 |
{ |
|
290 |
"original_label": "Cathédrale Notre-Dame de Paris", |
|
291 |
"url": "http://fr.wikipedia.org/wiki/Cath%C3%A9drale_Notre-Dame_de_Paris", |
|
292 |
"abstract": "La cathédrale Notre-Dame de Paris...", |
|
293 |
"value": "Cathédrale Notre-Dame de Paris", |
|
294 |
"thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Notre_Dame_de_Paris_DSC_0846w.jpg?width=300", |
|
295 |
"nb": 6 |
|
296 |
}, |
|
297 |
... |
|
298 |
] |
|
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 | 302 |
maxcount = int(request.GET.get('count','40')) |
| 235 | 303 |
lang = request.GET.get('lang',request.LANGUAGE_CODE) |
| 324 | 304 |
count_notices_str = request.REQUEST.get("count_notices") |
305 |
count_notices_bool = True |
|
306 |
if count_notices_str: |
|
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 | 309 |
stemming_langs = [ 'fr', 'en', 'de', 'it' ] |
310 |
# For Japanese, there are no word boundaries, we should not use the regexp in that case |
|
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 | 313 |
if q: |
314 |
lq = q.lower() |
|
| 235 | 315 |
qs = Tag.objects.select_related('dbpedia_fields').filter(datasheet__validated=True) |
316 |
qrx = '(\\m|\\b)%s'%q |
|
317 |
if lang in no_translate_langs: |
|
318 |
if lang in stemming_langs: |
|
319 |
qs = qs.filter( label__iregex = qrx ) |
|
320 |
else: |
|
321 |
qs = qs.filter( label__icontains = q ) |
|
322 |
else: |
|
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 | 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 | 327 |
else: |
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 | 330 |
if count_notices_bool: |
331 |
qs = qs.annotate(nb=Count('datasheet',distinct=True)).order_by('-nb')[:maxcount] |
|
332 |
else: |
|
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 | 335 |
qslist = list(qs) |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
336 |
|
| 235 | 337 |
if lang in no_translate_langs: |
338 |
translations = {} |
|
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 | 343 |
res = [] |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
344 |
|
| 152 | 345 |
for t in qslist: |
| 285 | 346 |
if hasattr(t, 'dbpedia_fields'): |
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 | 349 |
if count_notices_bool: |
350 |
resobj['nb'] = t.nb |
|
| 285 | 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 | 353 |
if t.id in translations: |
354 |
resobj['value'] = translations[t.id]['label'] |
|
355 |
resobj['abstract'] = translations[t.id]['abstract'] |
|
356 |
else: |
|
357 |
resobj['value'] = t.label |
|
358 |
resobj['abstract'] = dbfields.abstract if dbfields is not None else None |
|
359 |
if q is None or resobj['value'].lower().find(lq) != -1: |
|
360 |
res.append(resobj) |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
361 |
|
| 545 | 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 | 364 |
|
365 |
||
| 205 | 366 |
def catsearch(request): |
| 693 | 367 |
""" |
368 |
Vue permettant la recherche de catégorie. On se restreint aux catégories qui sont aussi des tags. |
|
369 |
||
370 |
Paramêtres GET: |
|
371 |
:var (str) term: Le terme à rechercher. |
|
372 |
||
373 |
Réponse (application/json): |
|
374 |
Une liste comprenant le résultat de la recherche. |
|
375 |
||
376 |
exemple :: |
|
377 |
||
378 |
[ |
|
379 |
{ "value": "1982 au cinéma" }, |
|
380 |
{ "value": "Cinéma italien" }, |
|
381 |
{ "value": "2003 au cinéma" }, |
|
382 |
... |
|
383 |
] |
|
384 |
||
385 |
""" |
|
386 |
||
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
387 |
|
| 205 | 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 | 390 |
# On ne récupère que les catégories qui sont également des tags |
| 235 | 391 |
qrx = '(\\m|\\b)%s'%q |
| 290 | 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 | 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 | 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 | 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 | 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 | 402 |
|
403 |
||
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
404 |
def filter(request): |
| 693 | 405 |
""" |
406 |
Vue permettant de filtrer par facette des fiches HDA. |
|
407 |
Cette méthode est en fait un simple wrapper pour la méthode `filter_generic`. |
|
408 |
||
409 |
Paramêtres GET: |
|
410 |
:var lang: la langue de recherche (défaut: fr-fr). |
|
411 |
:var period: Période dans laquelle doit se faire la recherche. |
|
412 |
:var label: un mot-clef. |
|
413 |
:var country: Une liste de pays limitant la recherche. |
|
414 |
:var contentlist: Liste de fiches limitant la recherche. |
|
415 |
:var mto: Ordre maximum des tags (défaut: 12). |
|
416 |
:var contentcount: nombre de fiches maximum (défaut : 8). |
|
417 |
:var tagcount: nombre maximum de tag (défaut: 30). |
|
418 |
||
419 |
Réponse (application/json): |
|
420 |
Un objet comprenant le résultat de la recherche. |
|
421 |
""" |
|
422 |
||
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
423 |
|
| 235 | 424 |
lang = request.GET.get('lang',request.LANGUAGE_CODE) |
| 158 | 425 |
periode = request.GET.get('period',None) |
426 |
label = request.GET.get('label', None) |
|
427 |
country = request.GET.get('country', None) |
|
428 |
contentlist = request.GET.get('contentlist', None) |
|
| 238 | 429 |
max_tag_order = int(request.GET.get('mto', '12')) |
| 419 | 430 |
content_count = request.GET.get('contentcount', 8) |
| 158 | 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 | 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 | 435 |
return HttpResponse(content=outputstr, content_type='application/json') |
| 289 | 436 |
|
437 |
||
| 419 | 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 | 439 |
""" |
440 |
Méthode de recherche par facette sur les fiches HDA. |
|
441 |
||
442 |
:param str lang: La langue de recherche (défaut: "fr-fr"). |
|
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). |
|
444 |
:param str label: Limite la recherche à un label de tag (défaut: None). |
|
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). |
|
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) |
|
447 |
:param int max_tag_order: Limite le nombre maximum de tag par fiche (défaut: 12). |
|
448 |
:param int content_count: Limite le nombre de fiches résultat (défaut: 8). |
|
449 |
:param int tag_count: Limite le nombre de tag dans le résultat (défaut: 30). |
|
450 |
:rtype: string |
|
451 |
:returns: Un objet json sérialisé comprenant les résultats de la recherche pour les différentes facettes. |
|
452 |
||
453 |
||
454 |
Clefs de l'objet: |
|
455 |
- :count: Nombre total de fiches. |
|
456 |
- :disciplines: Liste de disciplines artistiques en relation avec les reesultats, triée par score. |
|
457 |
- :countries: Objet dont les clef sont des uri dbpedia de pays et les valeurs sont le nombre de fiches. |
|
458 |
- :tags: Liste de tag triés par score. |
|
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. |
|
460 |
- :contents: Liste de fiches HDA répondant à la recherche, classée par score. Chaque fiche comporte une liste de tag. |
|
461 |
- :tagtranslations: Objet donnant les traductions de label de tag rencontrés dans les résultats. |
|
462 |
||
463 |
exemple:: |
|
464 |
||
465 |
{ |
|
466 |
"count": 936, |
|
467 |
"disciplines": [ |
|
468 |
{ |
|
469 |
"translated_label": "Peinture", |
|
470 |
"score": 936, |
|
471 |
"label": "Peinture" |
|
472 |
}, |
|
473 |
{ |
|
474 |
"translated_label": "Sculpture", |
|
475 |
"score": 88, |
|
476 |
"label": "Sculpture" |
|
477 |
}, |
|
478 |
... |
|
479 |
], |
|
480 |
"countries": { |
|
481 |
"http://fr.dbpedia.org/resource/Iran": 1, |
|
482 |
"http://fr.dbpedia.org/resource/Espagne": 16, |
|
483 |
... |
|
484 |
}, |
|
485 |
"tags": [ |
|
486 |
{ |
|
487 |
"url": "http://fr.dbpedia.org/resource/Portrait", |
|
488 |
"id": 63452, |
|
489 |
"translated_label": "Portrait", |
|
490 |
"score": 179, |
|
491 |
"wkpd_url": "http://fr.wikipedia.org/wiki/Portrait", |
|
492 |
"label": "Portrait", |
|
493 |
"thumbnail": "http://commons.wikimedia.org/wiki/Special:FilePath/Fayum02.jpg?width=300" |
|
494 |
}, |
|
495 |
... |
|
496 |
], |
|
497 |
"sparkline": [ |
|
498 |
{ |
|
499 |
"score": 2, |
|
500 |
"year": -600 |
|
501 |
}, |
|
502 |
{ |
|
503 |
"score": 4, |
|
504 |
"year": -500 |
|
505 |
}, |
|
506 |
... |
|
507 |
{ |
|
508 |
"score": 18, |
|
509 |
"year": 2001 |
|
510 |
} |
|
511 |
], |
|
512 |
"contents": [ |
|
513 |
{ |
|
514 |
"description": "Le palais Fesch, ...", |
|
515 |
"title": "Histoires bibliques", |
|
516 |
"url": "http://www.musee-fesch.com/index.php/musee_fesch/content/view/ef_catalogue_sommaire/1513/%28node_id_theme%29/33459", |
|
517 |
"tags": [ |
|
518 |
{ |
|
519 |
"url": "http://fr.dbpedia.org/resource/Peinture", |
|
520 |
"id": 54648, |
|
521 |
"translated_label": "Peinture", |
|
522 |
"wkpd_url": "http://fr.wikipedia.org/wiki/Peinture", |
|
523 |
"label": "Peinture", |
|
524 |
"order": 1, |
|
525 |
"match": true |
|
526 |
}, |
|
527 |
... |
|
528 |
], |
|
529 |
"score": 23, |
|
530 |
"coords": { |
|
531 |
"city_name": "Ajaccio", |
|
532 |
"latitude": 41.916667, |
|
533 |
"longitude": 8.733333 |
|
534 |
}, |
|
535 |
"hda_id": "4448", |
|
536 |
"id": 13855 |
|
537 |
}, |
|
538 |
{ |
|
539 |
"description": "...", |
|
540 |
"title": "Le XIXe siècle", |
|
541 |
"url": "http://www.grandpalais.fr/fr/article/le-xixe-siecle", |
|
542 |
"tags": [ ... ], |
|
543 |
"score": 22, |
|
544 |
"hda_id": "5217", |
|
545 |
"id": 13582 |
|
546 |
}, |
|
547 |
... |
|
548 |
], |
|
549 |
"tagtranslations": { |
|
550 |
"Paul Cézanne": "Paul Cézanne", |
|
551 |
"Politique": "Politique", |
|
552 |
"Poésie": "Poésie", |
|
553 |
"Religion": "Religion", |
|
554 |
"Empereur": "Empereur", |
|
555 |
"Saint": "Saint", |
|
556 |
... |
|
557 |
} |
|
558 |
} |
|
559 |
||
560 |
""" |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
561 |
|
| 260 | 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 | 564 |
key_parts = ("filter",lang,periode,label,country,contentlist,max_tag_order,content_count,tag_count) |
| 280 | 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 | 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 | 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 | 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 | 573 |
matchtagids = [] |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
574 |
|
| 165 | 575 |
tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']).filter(~Q(dbpedia_uri = None)) |
| 154 | 576 |
countryqs = Country.objects |
| 165 | 577 |
discqs = Tag.objects.filter(~Q(dbpedia_uri = None), category__label = u'Discipline artistique').select_related('dbpedia_fields') |
| 154 | 578 |
yearqs = TagYears.objects |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
579 |
|
| 154 | 580 |
contentqs = Datasheet.objects.filter(validated=True) |
581 |
labeltranslations = [] |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
582 |
|
| 154 | 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 | 586 |
if periode: |
587 |
years = periode.split(",") |
|
588 |
start_year = int(years[0]) |
|
589 |
end_year = int(years[0:2][-1]) |
|
590 |
delta = max(1, (end_year-start_year)/2) |
|
591 |
minstart = start_year - delta |
|
592 |
maxend = end_year + delta |
|
| 165 | 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 | 595 |
years__start_year__lte = end_year, |
596 |
years__end_year__lte = maxend, |
|
597 |
years__start_year__gte = minstart, |
|
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 | 601 |
if label: |
602 |
for txtlbl in label.split(","): |
|
| 165 | 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 | 606 |
if country: |
607 |
for country_uri in country.split(","): |
|
| 165 | 608 |
matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),locatedin__country__dbpedia_uri = country_uri) |
| 154 | 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 | 611 |
if contentlist: |
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 | 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 | 626 |
if lang not in no_translate_langs: |
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 | 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 | 632 |
tagqs = tagqs.filter(datasheet__in = contentqs) |
633 |
countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs) |
|
634 |
discqs = discqs.filter(datasheet__in = contentqs) |
|
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 | 637 |
if contentlist is None: |
638 |
contentqs.order_by('?') |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
639 |
|
| 154 | 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 | 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 | 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 | 645 |
contentids = contenus.keys() |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
646 |
|
| 154 | 647 |
qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids) |
648 |
for dse in qs: |
|
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 | 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 | 653 |
translations = {} |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
654 |
|
| 260 | 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 | 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 | 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 | 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 | 677 |
if contentlist is None: |
678 |
contenus = sorted(contenus.values(),key=lambda e: -e['score']) |
|
679 |
else: |
|
680 |
contenus = contenus.values() |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
681 |
|
| 154 | 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 | 684 |
#.select_related('dbpedia_fields') |
685 |
# hack to add only necessary fields in the group by |
|
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 | 691 |
tagqslist = list(tagqs) |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
692 |
|
| 154 | 693 |
dbpediafields = dict([(df.tag_id, df) for df in DbpediaFields.objects.filter(tag__in = tagqslist)]) |
| 260 | 694 |
|
695 |
if lang not in no_translate_langs: |
|
696 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = dbpediafields.values(), language_code = lang) |
|
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 | 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 | 707 |
countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet')) |
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 | 710 |
discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10]) |
| 260 | 711 |
|
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 | 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 | 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 | 719 |
years = {} |
720 |
yearqs = yearqs.annotate(nb=Count('tag__taggedsheet')) |
|
721 |
for ty in yearqs: |
|
| 286 | 722 |
for year in range(ty.start_year, ty.end_year): |
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 | 725 |
yearchange = [] |
726 |
for year in sorted(years.keys()): |
|
727 |
score = years[year] |
|
728 |
if year < 2011: |
|
729 |
if (year-1 not in years and score != 0) or (year-1 in years and years[year-1] != score): |
|
730 |
yearchange.append({'year': year, 'score': score}) |
|
731 |
if year+1 not in years and year != -1 and score != 0: |
|
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 | 734 |
tag_translations = {} |
735 |
for t in itertools.chain(labeltranslations,disciplines,tags): |
|
| 135 | 736 |
tag_translations[t['label']] = t['translated_label'] |
| 154 | 737 |
for c in contenus: |
738 |
for t in c['tags']: |
|
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 | 741 |
output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines, 'tagtranslations': tag_translations} |
742 |
outputstr = json.dumps(output) |
|
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 | 745 |
return outputstr |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
746 |
|
| 289 | 747 |
|
| 238 | 748 |
|
| 248 | 749 |
def subtree(tree): |
| 693 | 750 |
""" |
751 |
Methode récursive permettant de remplir un arbre ce catégories avec les fiches HDA correspondantes. |
|
752 |
||
753 |
:param tree: L'arbre de catégorie |
|
754 |
||
755 |
:returns:Un arbre de catégorie rempli de fiches HDA. |
|
756 |
||
757 |
format du paramêtre `tree` :: |
|
758 |
||
759 |
{ |
|
760 |
"label": "secteur urbain", |
|
761 |
"contents": [ |
|
762 |
{ "label": "banlieue", |
|
763 |
"contents": [ |
|
764 |
{ "label": "faubourg" } |
|
765 |
] }, |
|
766 |
{ "label": "îlot" }, |
|
767 |
... |
|
768 |
] |
|
769 |
} |
|
770 |
||
771 |
Exemple de retour :: |
|
772 |
||
773 |
||
774 |
{ |
|
775 |
"label": "secteur urbain", |
|
776 |
"contents": [ |
|
777 |
{ |
|
778 |
"score": 6, |
|
779 |
"organization": "Institut national de l'audiovisuel ( INA )", |
|
780 |
"description": "Pour faire face à la ...", |
|
781 |
"title": "La construction des grands ensembles de banlieue : l'exemple de Sarcelles", |
|
782 |
"url": "http://fresques.ina.fr/jalons/fiche-media/InaEdu01075/la-construction-des-grands-ensembles-de-banlieue--l-exemple-de-sarcelles", |
|
783 |
"hda_id": "2090", |
|
784 |
"organization_url": "http://www.ina.fr", |
|
785 |
"id": 12360 |
|
786 |
}, |
|
787 |
{ |
|
788 |
"score": 6, |
|
789 |
"organization": "Maison de banlieue et d'architecture", |
|
790 |
"description": "La Maison de banlieue et d'architecture...", |
|
791 |
"title": "Des ensembles assez grands. Mémoire et projets en Essonne", |
|
792 |
"url": "http://maisondebanlieue.fr/wp-content/uploads/2011/05/Cahier11_grands_ensembles.pdf", |
|
793 |
"hda_id": "5893", |
|
794 |
"organization_url": "http://www.maisondebanlieue.fr/", |
|
795 |
"id": 14821 |
|
796 |
}, |
|
797 |
... |
|
798 |
] |
|
799 |
"themes": [ |
|
800 |
{ |
|
801 |
"label": "faubourg", |
|
802 |
"content": [...], |
|
803 |
"themes": [...] |
|
804 |
}, |
|
805 |
... |
|
806 |
] |
|
807 |
} |
|
808 |
||
809 |
""" |
|
| 250 | 810 |
MAX_TAG_ORDER = 16 |
| 248 | 811 |
label = tree['label'] |
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 | 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 | 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 | 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 | 820 |
res = { 'label': label } |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
821 |
|
| 248 | 822 |
if len(contents): |
823 |
res['contents'] = contents |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
824 |
|
| 248 | 825 |
if len(sub): |
826 |
subcats = [subtree(st) for st in sub] |
|
| 252 | 827 |
subcats = [sc for sc in subcats if len(sc.get('contents',[])) or len(sc.get('themes',[]))] |
828 |
res['themes'] = subcats |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
829 |
|
| 248 | 830 |
return res |
831 |
||
832 |
def filltree(request): |
|
| 693 | 833 |
""" |
834 |
Vue permettant d'ajouter des fiches à un arbre de catégories. |
|
835 |
Cette méthode est en fait un simple wrapper pour la méthode :meth:`subtree`. |
|
836 |
||
837 |
Paramêtres GET: |
|
838 |
:var tree: Serialisation json d'un arbre de catégories à remplir. exemple: |
|
839 |
||
840 |
:: |
|
841 |
||
842 |
{ |
|
843 |
"label": "secteur urbain", |
|
844 |
"contents": [ |
|
845 |
{ "label": "banlieue", |
|
846 |
"contents": [ |
|
847 |
{ "label": "faubourg" } |
|
848 |
] }, |
|
849 |
{ "label": "îlot" }, |
|
850 |
... |
|
851 |
] |
|
852 |
} |
|
853 |
||
854 |
Réponse (application/json): |
|
855 |
L'arbre de catégories remplis de fiches HDA (c.f. retour methode :meth:`subtree`) |
|
856 |
||
857 |
""" |
|
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
858 |
|
| 248 | 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 | 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 | 863 |
res = subtree(treeobj) |
|
620
f45d7494332e
upgrade renkan, correct tagcloud, migrate to lodash
ymh <ymh.work@gmail.com>
parents:
605
diff
changeset
|
864 |
|
| 545 | 865 |
return HttpResponse(content=json.dumps(res), content_type='application/json') |