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