| author | ymh <ymh.work@gmail.com> |
| Mon, 19 Mar 2012 16:32:36 +0100 | |
| changeset 172 | 8f47c67c6d28 |
| parent 165 | b9b93ff09ebe |
| child 175 | ebc92daef815 |
| 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 |
| 135 | 9 |
from django.db.models import Q, Count |
|
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 |
|
172
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
14 |
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
|
15 |
import copy |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
import django.utils.simplejson as json |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
import hmac |
| 135 | 18 |
import itertools |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
import uuid |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
def taginfo(request): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
label = request.GET.get('label', None) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
resobj = {'requested_label' : label} |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
resobj["content_count"] = Datasheet.objects.filter(taggedsheet__tag__label__iexact = label).distinct().count() |
| 135 | 27 |
|
| 165 | 28 |
res = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = label).order_by('-dbpedia_uri')[0:1] |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
if len(res) == 1: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
restag = res.get() |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
resobj["dbpedia_uri"] = restag.dbpedia_uri |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
if resobj["dbpedia_uri"] is not None and restag.dbpedia_fields is not None: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
dbfield = restag.dbpedia_fields |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
resobj["abstract"] = dbfield.abstract |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
resobj["dbpedia_label"] = dbfield.label |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
resobj["thumbnail"] = dbfield.thumbnail |
| 135 | 37 |
transqs = DbpediaFieldsTranslation.objects.filter(master=dbfield, language_code=request.LANGUAGE_CODE)[0:1] |
38 |
if transqs: |
|
39 |
trans = transqs.get() |
|
40 |
resobj['translated_abstract'] = trans.abstract |
|
41 |
resobj['translated_label'] = trans.label |
|
42 |
else: |
|
43 |
resobj['translated_abstract'] = dbfield.abstract |
|
44 |
resobj['translated_label'] = dbfield.label |
|
45 |
||
46 |
#res = Tag.objects.filter(label__iexact = label).order_by('-wikipedia_url')[0:1] |
|
47 |
#if len(res) == 1: |
|
48 |
# resobj["wikipedia_url"] = res.get().wikipedia_url |
|
49 |
if 'translated_label' in resobj: |
|
50 |
wikipedia_label = resobj['translated_label'] |
|
51 |
else: |
|
52 |
wikipedia_label = label |
|
53 |
wikipedia_label = wikipedia_label[0].capitalize() + wikipedia_label[1:] |
|
54 |
resobj["wikipedia_url"] = "http://%s.wikipedia.org/wiki/%s" % (request.LANGUAGE_CODE,wikipedia_label.replace(' ', '_')) |
|
| 150 | 55 |
# We don't use links at the moment, so I'll comment this line to speed up requests |
56 |
# resobj["links"] = [{'subject':tl.subject.label, 'object':tl.object.label} for tl in TagLinks.objects.select_related().filter(Q(subject__label__iexact = label) | Q(object__label__iexact = label))] |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
return HttpResponse(content=json.dumps(resobj), mimetype='application/json') |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
|
| 135 | 61 |
def tagtranslation(request): |
62 |
||
63 |
labels = request.GET.get('labels',None) |
|
64 |
||
65 |
if not labels: |
|
66 |
return HttpResponse(content=json.dumps({}), mimetype='application/json') |
|
67 |
||
68 |
labelslist = [lbl.strip() for lbl in labels.split(",")] |
|
69 |
masters = [] |
|
70 |
||
71 |
for lbl in labelslist: |
|
| 165 | 72 |
labelqs = Tag.objects.select_related('dbpedia_fields').filter(~Q(dbpedia_uri = None), label__iexact = lbl)[0:1] |
| 135 | 73 |
if len(labelqs) > 0: |
74 |
tag = labelqs.get() |
|
75 |
if tag.dbpedia_fields: |
|
76 |
masters.append(tag.dbpedia_fields) |
|
77 |
||
78 |
translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE) |
|
79 |
||
80 |
translations = dict([(t.master.label, t.label) for t in translationqs]) |
|
81 |
||
82 |
return HttpResponse(content=json.dumps(translations), mimetype='application/json') |
|
83 |
||
84 |
||
85 |
||
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
def sessioninfo(request): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
data = json.loads(request.GET.get('data', "{}")) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
write = False |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
if 'sessionid' in request.GET: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
request.session['sessionid'] = request.GET['sessionid'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
93 |
if 'sessionkey' in request.GET: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
94 |
request.session['sessionkey'] = request.GET['sessionkey'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
95 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
96 |
if 'sessionid' in request.session: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
97 |
sessionid = request.session['sessionid'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
99 |
if HdaSession.objects.filter(sessionid=sessionid).count() == 1: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
100 |
sessionkey = request.session.get('sessionkey',None) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
101 |
hm = hmac.new(settings.SECRET_KEY, sessionid) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
102 |
if hm.hexdigest() == sessionkey: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
103 |
write = True |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
104 |
else: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
105 |
del request.session['sessionid'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
106 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
107 |
if 'sessionid' not in request.session: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
108 |
sessionid = unicode(uuid.uuid1()) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
109 |
HdaSession.objects.create(sessionid=sessionid, data=json.dumps({})) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
110 |
write = True |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
111 |
request.session['sessionid'] = sessionid |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
112 |
request.session['sessionkey'] = hmac.new(settings.SECRET_KEY, sessionid).hexdigest() |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
113 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
114 |
if write and data: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
115 |
HdaSession.objects.filter(sessionid=sessionid).update(data=json.dumps(data)) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
116 |
else: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
117 |
data = HdaSession.objects.get(sessionid=sessionid).data |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
118 |
data = json.loads(data) if data else {} |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
119 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
120 |
resobj = {'data': data, "write_allowed" : write, "sessionid": sessionid } |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
121 |
if write: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
122 |
resobj['sessionkey'] = request.session['sessionkey'] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
123 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
124 |
return HttpResponse(content=json.dumps(resobj), mimetype='application/json') |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
125 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
126 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
127 |
def tagsearch(request): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
128 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
129 |
q = request.GET.get('term',None) |
| 150 | 130 |
if q: |
131 |
lq = q.lower() |
|
| 165 | 132 |
qs = Tag.objects.filter( Q(label__icontains = q ) | Q(dbpedia_fields__translations__label__icontains = q, dbpedia_fields__translations__language_code=request.LANGUAGE_CODE), ~Q(dbpedia_uri = None)) if q else Tag.objects.filter(~Q(dbpedia_uri = None)) |
| 150 | 133 |
qs = qs.annotate(nb=Count('datasheet')).order_by('-nb')[:20] |
| 152 | 134 |
|
135 |
qslist = list(qs) |
|
136 |
||
137 |
transqs = DbpediaFieldsTranslation.objects.filter(master__tag__in = qslist, language_code=request.LANGUAGE_CODE).select_related("master") |
|
138 |
||
139 |
translations = dict([(tr.master.tag_id, tr.label) for tr in transqs]) |
|
140 |
||
| 150 | 141 |
res = [] |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
142 |
|
| 152 | 143 |
for t in qslist: |
| 150 | 144 |
resobj = {'value':t.label,'nb':t.nb} |
| 152 | 145 |
if t.id in translations: |
146 |
resobj['label'] = translations[t.id] |
|
| 150 | 147 |
else: |
148 |
resobj['label'] = t.label |
|
149 |
if q is None or resobj['label'].lower().find(lq) != -1: |
|
150 |
res.append(resobj) |
|
151 |
||
152 |
return HttpResponse(content=json.dumps(res), mimetype='application/json') |
|
|
119
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 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
155 |
def filter(request): |
| 127 | 156 |
|
| 158 | 157 |
periode = request.GET.get('period',None) |
158 |
label = request.GET.get('label', None) |
|
159 |
country = request.GET.get('country', None) |
|
160 |
contentlist = request.GET.get('contentlist', None) |
|
161 |
max_tag_order = request.GET.get('mto', 12) |
|
162 |
content_count = request.GET.get('contentcount', 12) |
|
163 |
tag_count = request.GET.get('tagcount', 30) |
|
164 |
||
| 163 | 165 |
key_parts = ("filter",request.LANGUAGE_CODE,periode,label,country,contentlist,max_tag_order,content_count,tag_count) |
166 |
key_parts = [unicode(p).encode("utf-8") for p in key_parts] |
|
167 |
||
168 |
cache_key = fix_cache_key("-".join(key_parts)) |
|
| 154 | 169 |
|
170 |
outputstr = cache.get(cache_key) |
|
| 127 | 171 |
|
| 154 | 172 |
if outputstr is None: |
173 |
||
174 |
matchtagids = [] |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
175 |
|
| 165 | 176 |
tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']).filter(~Q(dbpedia_uri = None)) |
| 154 | 177 |
countryqs = Country.objects |
| 165 | 178 |
discqs = Tag.objects.filter(~Q(dbpedia_uri = None), category__label = u'Discipline artistique').select_related('dbpedia_fields') |
| 154 | 179 |
yearqs = TagYears.objects |
180 |
||
181 |
contentqs = Datasheet.objects.filter(validated=True) |
|
182 |
labeltranslations = [] |
|
183 |
||
184 |
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
|
185 |
matchtagqslist = [] |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
186 |
|
| 154 | 187 |
if periode: |
188 |
years = periode.split(",") |
|
189 |
start_year = int(years[0]) |
|
190 |
end_year = int(years[0:2][-1]) |
|
191 |
delta = max(1, (end_year-start_year)/2) |
|
192 |
minstart = start_year - delta |
|
193 |
maxend = end_year + delta |
|
| 165 | 194 |
matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None), |
195 |
years__end_year__gte = start_year, |
|
196 |
years__start_year__lte = end_year, |
|
197 |
years__end_year__lte = maxend, |
|
198 |
years__start_year__gte = minstart, |
|
199 |
) |
|
|
172
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
200 |
matchtagqslist.append(matchtagqs) |
| 154 | 201 |
|
202 |
if label: |
|
203 |
for txtlbl in label.split(","): |
|
| 165 | 204 |
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
|
205 |
matchtagqslist.append(matchtagqs) |
| 154 | 206 |
|
207 |
if country: |
|
208 |
for country_uri in country.split(","): |
|
| 165 | 209 |
matchtagqs = Tag.objects.filter(~Q(dbpedia_uri = None),locatedin__country__dbpedia_uri = country_uri) |
| 154 | 210 |
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
|
211 |
matchtagqslist.append(matchtagqs) |
| 154 | 212 |
if contentlist: |
213 |
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
|
214 |
|
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
215 |
tagcond = None |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
216 |
tagcondid = None |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
217 |
for matchtagqs in matchtagqslist: |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
218 |
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
|
219 |
newcondid = Q(id__in = matchtagqs) |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
220 |
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
|
221 |
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
|
222 |
|
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
223 |
contentqs = contentqs.filter(tagcond).distinct() |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
224 |
matchtagidsqs = list(Tag.objects.select_related("dbpedia_fields").only("id").filter(tagcondid)) |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
225 |
matchtagids = [t.id for t in matchtagidsqs] |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
226 |
masters = [t.dbpedia_fields for t in matchtagidsqs if t.dbpedia_fields is not None] |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
227 |
|
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
228 |
translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE) |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
229 |
labeltranslations = [{'label':t.master.label, 'translated_label':t.label} for t in translationqs] |
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
230 |
|
|
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
231 |
|
| 154 | 232 |
tagqs = tagqs.filter(datasheet__in = contentqs) |
233 |
countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs) |
|
234 |
discqs = discqs.filter(datasheet__in = contentqs) |
|
235 |
yearqs = yearqs.filter(tag__taggedsheet__datasheet__in = contentqs) |
|
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
119
diff
changeset
|
236 |
|
| 154 | 237 |
if contentlist is None: |
238 |
contentqs.order_by('?') |
|
|
172
8f47c67c6d28
Optimize query, allow unlimited facette parameters in query.
ymh <ymh.work@gmail.com>
parents:
165
diff
changeset
|
239 |
|
| 154 | 240 |
cont_count = contentqs.count() |
241 |
||
242 |
contenus = dict([(content.id, {'score' : 0, 'tags' : [], 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url}) for content in contentqs[0:content_count]]) |
|
243 |
contentids = contenus.keys() |
|
| 127 | 244 |
|
| 154 | 245 |
qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids) |
246 |
for dse in qs: |
|
247 |
contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude} |
|
248 |
||
249 |
qs = list(TaggedSheet.objects.select_related('tag', 'tag__dbpedia_fields').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order')) |
|
250 |
||
251 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = [ts.tag.dbpedia_fields for ts in qs], language_code = request.LANGUAGE_CODE) |
|
252 |
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
|
253 |
|
| 154 | 254 |
for ts in qs: |
255 |
match_tag = ts.tag.id in matchtagids |
|
256 |
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}) |
|
257 |
||
258 |
if match_tag: |
|
259 |
contenus[ts.datasheet_id]['score'] += 2*max_tag_order - ts.order |
|
260 |
||
261 |
if contentlist is None: |
|
262 |
contenus = sorted(contenus.values(),key=lambda e: -e['score']) |
|
263 |
else: |
|
264 |
contenus = contenus.values() |
|
| 152 | 265 |
|
| 154 | 266 |
#tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count] |
267 |
tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label')[:tag_count] |
|
268 |
#.select_related('dbpedia_fields') |
|
269 |
# hack to add only necessary fields in the group by |
|
270 |
# contournement bug https://code.djangoproject.com/ticket/17144 |
|
271 |
tagqs.query.clear_select_fields() |
|
272 |
tagqs.query.add_fields(['id','label'], False) |
|
273 |
tagqs.query.set_group_by() |
|
274 |
||
275 |
tagqslist = list(tagqs) |
|
| 135 | 276 |
|
| 154 | 277 |
dbpediafields = dict([(df.tag_id, df) for df in DbpediaFields.objects.filter(tag__in = tagqslist)]) |
| 135 | 278 |
|
| 154 | 279 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = dbpediafields.values(), language_code = request.LANGUAGE_CODE) |
280 |
translations = dict([(trans.master_id,trans.label) for trans in transqs]) |
|
| 127 | 281 |
|
| 154 | 282 |
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] |
283 |
||
284 |
countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet')) |
|
285 |
countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs]) |
|
286 |
||
287 |
discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10]) |
|
| 127 | 288 |
|
| 154 | 289 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in discqslist], language_code = request.LANGUAGE_CODE) |
290 |
translations = dict([(trans.master_id,trans.label) for trans in transqs]) |
|
291 |
||
292 |
||
293 |
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] |
|
294 |
||
295 |
years = {} |
|
296 |
yearqs = yearqs.annotate(nb=Count('tag__taggedsheet')) |
|
297 |
for ty in yearqs: |
|
298 |
for year in range(ty.start_year, ty.end_year): |
|
299 |
years[year] = ty.nb + (years[year] if year in years else 0) |
|
300 |
||
301 |
yearchange = [] |
|
302 |
for year in sorted(years.keys()): |
|
303 |
score = years[year] |
|
304 |
if year < 2011: |
|
305 |
if (year-1 not in years and score != 0) or (year-1 in years and years[year-1] != score): |
|
306 |
yearchange.append({'year': year, 'score': score}) |
|
307 |
if year+1 not in years and year != -1 and score != 0: |
|
308 |
yearchange.append({'year': year+1, 'score': 0}) |
|
309 |
||
310 |
tag_translations = {} |
|
311 |
for t in itertools.chain(labeltranslations,disciplines,tags): |
|
| 135 | 312 |
tag_translations[t['label']] = t['translated_label'] |
| 154 | 313 |
for c in contenus: |
314 |
for t in c['tags']: |
|
315 |
tag_translations[t['label']] = t['translated_label'] |
|
316 |
||
317 |
output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines, 'tagtranslations': tag_translations} |
|
318 |
outputstr = json.dumps(output) |
|
319 |
cache.set(cache_key, outputstr) |
|
320 |
||
321 |
return HttpResponse(content=outputstr, mimetype='application/json') |