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