| author | cavaliet |
| Tue, 02 Sep 2014 12:14:38 +0200 | |
| changeset 324 | 573043a98b44 |
| parent 291 | 44af3e5e4114 |
| child 326 | fc8961398609 |
| 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: |
|
31 |
return HttpResponse(content=json.dumps({}), mimetype='application/json') |
|
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 |
||
47 |
return HttpResponse(content=json.dumps(translations), mimetype='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 = {} |
97 |
resobj = None |
|
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 ) |
|
| 324 | 101 |
|
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 |
}) |
| 208 | 131 |
cleantags(resobj) |
| 204 | 132 |
|
133 |
return HttpResponse(content=json.dumps(resobj), mimetype='application/json') |
|
| 135 | 134 |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
135 |
def sessioninfo(request): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
136 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
137 |
data = json.loads(request.GET.get('data', "{}")) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
138 |
write = False |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
139 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
140 |
if 'sessionid' in request.GET: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
141 |
request.session['sessionid'] = request.GET['sessionid'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
142 |
if 'sessionkey' in request.GET: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
143 |
request.session['sessionkey'] = request.GET['sessionkey'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
144 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
145 |
if 'sessionid' in request.session: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
146 |
sessionid = request.session['sessionid'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
147 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
148 |
if HdaSession.objects.filter(sessionid=sessionid).count() == 1: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
149 |
sessionkey = request.session.get('sessionkey',None) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
150 |
hm = hmac.new(settings.SECRET_KEY, sessionid) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
151 |
if hm.hexdigest() == sessionkey: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
152 |
write = True |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
153 |
else: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
154 |
del request.session['sessionid'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
155 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
156 |
if 'sessionid' not in request.session: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
157 |
sessionid = unicode(uuid.uuid1()) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
158 |
HdaSession.objects.create(sessionid=sessionid, data=json.dumps({})) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
159 |
write = True |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
160 |
request.session['sessionid'] = sessionid |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
161 |
request.session['sessionkey'] = hmac.new(settings.SECRET_KEY, sessionid).hexdigest() |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
162 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
163 |
if write and data: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
164 |
HdaSession.objects.filter(sessionid=sessionid).update(data=json.dumps(data)) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
165 |
else: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
166 |
data = HdaSession.objects.get(sessionid=sessionid).data |
| 290 | 167 |
data = json.loads(data) if data else {} |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
168 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
169 |
resobj = {'data': data, "write_allowed" : write, "sessionid": sessionid } |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
170 |
if write: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
171 |
resobj['sessionkey'] = request.session['sessionkey'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
172 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
173 |
return HttpResponse(content=json.dumps(resobj), mimetype='application/json') |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
174 |
|
|
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 |
def tagsearch(request): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
177 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
178 |
q = request.GET.get('term',None) |
| 238 | 179 |
maxcount = int(request.GET.get('count','40')) |
| 235 | 180 |
lang = request.GET.get('lang',request.LANGUAGE_CODE) |
| 324 | 181 |
count_notices_str = request.REQUEST.get("count_notices") |
182 |
count_notices_bool = True |
|
183 |
if count_notices_str: |
|
184 |
count_notices_bool = {'true': True, 'false': False, "0": False, "1": True}.get(count_notices_str.lower()) |
|
| 235 | 185 |
|
186 |
stemming_langs = [ 'fr', 'en', 'de', 'it' ] |
|
187 |
# For Japanese, there are no word boundaries, we should not use the regexp in that case |
|
188 |
no_translate_langs = [ 'fr' ] |
|
189 |
||
| 150 | 190 |
if q: |
191 |
lq = q.lower() |
|
| 235 | 192 |
qs = Tag.objects.select_related('dbpedia_fields').filter(datasheet__validated=True) |
193 |
qrx = '(\\m|\\b)%s'%q |
|
194 |
if lang in no_translate_langs: |
|
195 |
if lang in stemming_langs: |
|
196 |
qs = qs.filter( label__iregex = qrx ) |
|
197 |
else: |
|
198 |
qs = qs.filter( label__icontains = q ) |
|
199 |
else: |
|
200 |
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
|
201 |
qs = qs.filter(dbpedia_fields__translations__label__iregex=qrx, dbpedia_fields__translations__language_code=lang, dbpedia_fields__translations__is_label_translated = True) |
| 235 | 202 |
else: |
|
243
1f2840354865
correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents:
238
diff
changeset
|
203 |
qs = qs.filter(dbpedia_fields__translations__label__icontains=q, dbpedia_fields__translations__language_code=lang, dbpedia_fields__translations__is_label_translated = True) |
| 235 | 204 |
else: |
205 |
qs = Tag.objects.filter(~Q(dbpedia_uri = None)) |
|
| 324 | 206 |
|
207 |
if count_notices_bool: |
|
208 |
qs = qs.annotate(nb=Count('datasheet',distinct=True)).order_by('-nb')[:maxcount] |
|
209 |
else: |
|
210 |
qs = qs.distinct()[:maxcount] |
|
| 152 | 211 |
|
212 |
qslist = list(qs) |
|
213 |
||
| 235 | 214 |
if lang in no_translate_langs: |
215 |
translations = {} |
|
216 |
else: |
|
|
243
1f2840354865
correct filter on tag completion to avoid tags that are not translated
ymh <ymh.work@gmail.com>
parents:
238
diff
changeset
|
217 |
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
|
218 |
translations = dict([(tr.master.tag_id, {'label':tr.label,'abstract':tr.abstract, 'is_label_translated': tr.is_label_translated}) for tr in transqs]) |
| 152 | 219 |
|
| 150 | 220 |
res = [] |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
221 |
|
| 152 | 222 |
for t in qslist: |
| 285 | 223 |
if hasattr(t, 'dbpedia_fields'): |
224 |
dbfields = t.dbpedia_fields |
|
| 324 | 225 |
resobj = {'original_label':t.label, 'url':t.dbpedia_uri} |
226 |
if count_notices_bool: |
|
227 |
resobj['nb'] = t.nb |
|
| 285 | 228 |
resobj['thumbnail'] = dbfields.thumbnail if dbfields is not None else None |
229 |
||
230 |
if t.id in translations: |
|
231 |
resobj['value'] = translations[t.id]['label'] |
|
232 |
resobj['abstract'] = translations[t.id]['abstract'] |
|
233 |
else: |
|
234 |
resobj['value'] = t.label |
|
235 |
resobj['abstract'] = dbfields.abstract if dbfields is not None else None |
|
236 |
if q is None or resobj['value'].lower().find(lq) != -1: |
|
237 |
res.append(resobj) |
|
| 150 | 238 |
|
239 |
return HttpResponse(content=json.dumps(res), mimetype='application/json') |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
240 |
|
| 205 | 241 |
def catsearch(request): |
242 |
||
243 |
q = request.GET.get('term',None) |
|
244 |
||
| 208 | 245 |
# On ne récupère que les catégories qui sont également des tags |
| 235 | 246 |
qrx = '(\\m|\\b)%s'%q |
| 290 | 247 |
qs = Tag.objects.filter(label__iregex=qrx) |
| 208 | 248 |
|
249 |
labels = [tag.label for tag in qs] |
|
250 |
||
| 235 | 251 |
qs = WpCategory.objects.annotate(nb=Count('child_categories__child_category__tags')).filter(label__in = labels, nb__gt=0) |
| 205 | 252 |
|
253 |
res = [{'value':t.label} for t in qs] |
|
254 |
||
255 |
return HttpResponse(content=json.dumps(res), mimetype='application/json') |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
256 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
257 |
def filter(request): |
| 127 | 258 |
|
| 235 | 259 |
lang = request.GET.get('lang',request.LANGUAGE_CODE) |
| 158 | 260 |
periode = request.GET.get('period',None) |
261 |
label = request.GET.get('label', None) |
|
262 |
country = request.GET.get('country', None) |
|
263 |
contentlist = request.GET.get('contentlist', None) |
|
| 238 | 264 |
max_tag_order = int(request.GET.get('mto', '12')) |
| 158 | 265 |
content_count = request.GET.get('contentcount', 12) |
266 |
tag_count = request.GET.get('tagcount', 30) |
|
| 289 | 267 |
|
268 |
outputstr = filter_generic(lang, periode, label, country, contentlist, max_tag_order, content_count, tag_count) |
|
269 |
||
270 |
return HttpResponse(content=outputstr, mimetype='application/json') |
|
271 |
||
272 |
||
273 |
def filter_generic(lang="fr-fr", periode=None, label=None, country=None, contentlist=None, max_tag_order=12, content_count=12, tag_count=30): |
|
274 |
||
| 260 | 275 |
no_translate_langs = [ 'fr' ] |
| 158 | 276 |
|
| 235 | 277 |
key_parts = ("filter",lang,periode,label,country,contentlist,max_tag_order,content_count,tag_count) |
| 280 | 278 |
key_parts = [unicode(p).encode("ascii", "ignore") for p in key_parts] |
| 163 | 279 |
|
280 |
cache_key = fix_cache_key("-".join(key_parts)) |
|
| 154 | 281 |
|
282 |
outputstr = cache.get(cache_key) |
|
| 127 | 283 |
|
| 154 | 284 |
if outputstr is None: |
285 |
||
286 |
matchtagids = [] |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
287 |
|
| 165 | 288 |
tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']).filter(~Q(dbpedia_uri = None)) |
| 154 | 289 |
countryqs = Country.objects |
| 165 | 290 |
discqs = Tag.objects.filter(~Q(dbpedia_uri = None), category__label = u'Discipline artistique').select_related('dbpedia_fields') |
| 154 | 291 |
yearqs = TagYears.objects |
292 |
||
293 |
contentqs = Datasheet.objects.filter(validated=True) |
|
294 |
labeltranslations = [] |
|
295 |
||
296 |
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
|
297 |
matchtagqslist = [] |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
298 |
|
| 154 | 299 |
if periode: |
300 |
years = periode.split(",") |
|
301 |
start_year = int(years[0]) |
|
302 |
end_year = int(years[0:2][-1]) |
|
303 |
delta = max(1, (end_year-start_year)/2) |
|
304 |
minstart = start_year - delta |
|
305 |
maxend = end_year + delta |
|
| 165 | 306 |
matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None), |
307 |
years__end_year__gte = start_year, |
|
308 |
years__start_year__lte = end_year, |
|
309 |
years__end_year__lte = maxend, |
|
310 |
years__start_year__gte = minstart, |
|
311 |
) |
|
|
172
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
312 |
matchtagqslist.append(matchtagqs) |
| 154 | 313 |
|
314 |
if label: |
|
315 |
for txtlbl in label.split(","): |
|
| 165 | 316 |
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
|
317 |
matchtagqslist.append(matchtagqs) |
| 154 | 318 |
|
319 |
if country: |
|
320 |
for country_uri in country.split(","): |
|
| 165 | 321 |
matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),locatedin__country__dbpedia_uri = country_uri) |
| 154 | 322 |
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
|
323 |
matchtagqslist.append(matchtagqs) |
| 154 | 324 |
if contentlist: |
325 |
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
|
326 |
|
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
327 |
tagcond = None |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
328 |
tagcondid = None |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
329 |
for matchtagqs in matchtagqslist: |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
330 |
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
|
331 |
newcondid = Q(id__in = matchtagqs) |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
332 |
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
|
333 |
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
|
334 |
|
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
335 |
contentqs = contentqs.filter(tagcond).distinct() |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
336 |
matchtagidsqs = list(Tag.objects.select_related("dbpedia_fields").only("id").filter(tagcondid)) |
| 260 | 337 |
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
|
338 |
|
| 260 | 339 |
if lang not in no_translate_langs: |
340 |
masters = [t.dbpedia_fields for t in matchtagidsqs if t.dbpedia_fields is not None] |
|
341 |
||
342 |
translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=lang) |
|
343 |
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
|
344 |
|
| 154 | 345 |
tagqs = tagqs.filter(datasheet__in = contentqs) |
346 |
countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs) |
|
347 |
discqs = discqs.filter(datasheet__in = contentqs) |
|
348 |
yearqs = yearqs.filter(tag__taggedsheet__datasheet__in = contentqs) |
|
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
119
diff
changeset
|
349 |
|
| 154 | 350 |
if contentlist is None: |
351 |
contentqs.order_by('?') |
|
|
172
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
352 |
|
| 154 | 353 |
cont_count = contentqs.count() |
354 |
||
| 253 | 355 |
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 | 356 |
contentids = contenus.keys() |
| 127 | 357 |
|
| 154 | 358 |
qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids) |
359 |
for dse in qs: |
|
360 |
contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude} |
|
361 |
||
362 |
qs = list(TaggedSheet.objects.select_related('tag', 'tag__dbpedia_fields').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order')) |
|
363 |
||
| 260 | 364 |
translations = {} |
365 |
||
366 |
if lang not in no_translate_langs: |
|
|
279
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
367 |
ts_list = [] |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
368 |
for ts in qs: |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
369 |
if hasattr(ts, 'tag') and hasattr(ts.tag, 'dbpedia_fields') : |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
370 |
ts_list.append(ts.tag.dbpedia_fields) |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
371 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = ts_list, language_code = lang) |
| 260 | 372 |
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
|
373 |
|
| 154 | 374 |
for ts in qs: |
|
279
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
375 |
if hasattr(ts, 'tag') and hasattr(ts.tag, 'dbpedia_fields') : |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
376 |
match_tag = ts.tag.id in matchtagids |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
377 |
contenus[ts.datasheet_id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag , 'translated_label': translations.get(ts.tag.dbpedia_fields.id, ts.tag.label) if ts.tag.dbpedia_fields is not None else ts.tag.label}) |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
378 |
|
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
379 |
if match_tag: |
|
177b508612f4
add, configure and correct hdalab to installed apps
cavaliet
parents:
272
diff
changeset
|
380 |
contenus[ts.datasheet_id]['score'] += 2*max_tag_order - ts.order |
| 154 | 381 |
|
382 |
if contentlist is None: |
|
383 |
contenus = sorted(contenus.values(),key=lambda e: -e['score']) |
|
384 |
else: |
|
385 |
contenus = contenus.values() |
|
| 152 | 386 |
|
| 154 | 387 |
#tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count] |
388 |
tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label')[:tag_count] |
|
389 |
#.select_related('dbpedia_fields') |
|
390 |
# hack to add only necessary fields in the group by |
|
391 |
# contournement bug https://code.djangoproject.com/ticket/17144 |
|
392 |
tagqs.query.clear_select_fields() |
|
393 |
tagqs.query.add_fields(['id','label'], False) |
|
394 |
tagqs.query.set_group_by() |
|
395 |
||
396 |
tagqslist = list(tagqs) |
|
| 135 | 397 |
|
| 154 | 398 |
dbpediafields = dict([(df.tag_id, df) for df in DbpediaFields.objects.filter(tag__in = tagqslist)]) |
| 260 | 399 |
|
400 |
if lang not in no_translate_langs: |
|
401 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = dbpediafields.values(), language_code = lang) |
|
402 |
translations = dict([(trans.master_id,trans.label) for trans in transqs]) |
|
| 127 | 403 |
|
| 291 | 404 |
tags = [{'id': tag.id, 'label': tag.label, 'score': tag.nb, 'thumbnail': dbpediafields[tag.id].thumbnail, 'translated_label': translations.get(dbpediafields[tag.id].id, tag.label) if tag.id in dbpediafields else tag.label} for tag in tagqslist] |
| 154 | 405 |
|
406 |
countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet')) |
|
407 |
countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs]) |
|
408 |
||
409 |
discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10]) |
|
| 260 | 410 |
|
411 |
if lang not in no_translate_langs: |
|
412 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in discqslist], language_code = lang) |
|
413 |
translations = dict([(trans.master_id,trans.label) for trans in transqs]) |
|
| 154 | 414 |
|
415 |
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] |
|
416 |
||
417 |
years = {} |
|
418 |
yearqs = yearqs.annotate(nb=Count('tag__taggedsheet')) |
|
419 |
for ty in yearqs: |
|
| 286 | 420 |
for year in range(ty.start_year, ty.end_year): |
421 |
years[year] = ty.nb + (years[year] if year in years else 0) |
|
| 154 | 422 |
|
423 |
yearchange = [] |
|
424 |
for year in sorted(years.keys()): |
|
425 |
score = years[year] |
|
426 |
if year < 2011: |
|
427 |
if (year-1 not in years and score != 0) or (year-1 in years and years[year-1] != score): |
|
428 |
yearchange.append({'year': year, 'score': score}) |
|
429 |
if year+1 not in years and year != -1 and score != 0: |
|
430 |
yearchange.append({'year': year+1, 'score': 0}) |
|
431 |
||
432 |
tag_translations = {} |
|
433 |
for t in itertools.chain(labeltranslations,disciplines,tags): |
|
| 135 | 434 |
tag_translations[t['label']] = t['translated_label'] |
| 154 | 435 |
for c in contenus: |
436 |
for t in c['tags']: |
|
437 |
tag_translations[t['label']] = t['translated_label'] |
|
438 |
||
439 |
output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines, 'tagtranslations': tag_translations} |
|
440 |
outputstr = json.dumps(output) |
|
441 |
cache.set(cache_key, outputstr) |
|
| 289 | 442 |
|
443 |
return outputstr |
|
444 |
||
445 |
||
| 238 | 446 |
|
| 248 | 447 |
def subtree(tree): |
| 250 | 448 |
MAX_TAG_ORDER = 16 |
| 248 | 449 |
label = tree['label'] |
450 |
sub = tree.get('contents',[]) |
|
451 |
||
452 |
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() |
|
453 |
||
| 253 | 454 |
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 | 455 |
|
456 |
contents = sorted(contents, key=lambda e: -e['score']) |
|
457 |
||
| 252 | 458 |
res = { 'label': label } |
| 248 | 459 |
|
460 |
if len(contents): |
|
461 |
res['contents'] = contents |
|
462 |
||
463 |
if len(sub): |
|
464 |
subcats = [subtree(st) for st in sub] |
|
| 252 | 465 |
subcats = [sc for sc in subcats if len(sc.get('contents',[])) or len(sc.get('themes',[]))] |
466 |
res['themes'] = subcats |
|
| 248 | 467 |
|
468 |
return res |
|
469 |
||
470 |
def filltree(request): |
|
471 |
||
472 |
tree = request.GET.get('tree','{}') |
|
473 |
||
474 |
treeobj = json.loads(tree) |
|
475 |
||
476 |
res = subtree(treeobj) |
|
477 |
||
478 |
return HttpResponse(content=json.dumps(res), mimetype='application/json') |