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