| author | ymh <ymh.work@gmail.com> |
| Sun, 11 Mar 2012 03:27:09 +0100 | |
| changeset 135 | dd6578e36a57 |
| parent 134 | 75f8f05f9a60 |
| child 150 | b0a34ce6fdcf |
| 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) |
|
13 |
from hdalab.models.dataviz import DbpediaFieldsTranslation |
|
|
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(' ', '_')) |
|
53 |
||
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
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))] |
|
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) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
128 |
qs = Tag.objects.filter(label__icontains = q) if q else Tag.objects.all() |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
129 |
res = qs.annotate(nb=Count('datasheet')).order_by('-nb')[:20] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
130 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
131 |
return HttpResponse(content=json.dumps([{'label':t.label,'nb':t.nb} for t in res]), mimetype='application/json') |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
132 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
133 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
134 |
def filter(request): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
135 |
periode = request.GET.get('period',None) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
136 |
label = request.GET.get('label', None) |
| 127 | 137 |
country = request.GET.get('country', None) |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
138 |
contentlist = request.GET.get('contentlist', None) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
139 |
max_tag_order = request.GET.get('mto', 12) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
140 |
content_count = request.GET.get('contentcount', 12) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
141 |
tag_count = request.GET.get('tagcount', 30) |
| 127 | 142 |
|
| 132 | 143 |
matchtagids = [] |
| 127 | 144 |
|
| 135 | 145 |
tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']).select_related('dbpedia_fields') |
| 127 | 146 |
countryqs = Country.objects |
| 135 | 147 |
discqs = Tag.objects.filter(category__label = u'Discipline artistique').select_related('dbpedia_fields') |
| 127 | 148 |
yearqs = TagYears.objects |
149 |
||
150 |
contentqs = Datasheet.objects.filter(validated=True) |
|
| 135 | 151 |
labeltranslations = [] |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
152 |
|
| 127 | 153 |
if label or periode or country or contentlist : |
|
119
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 |
if periode: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
156 |
years = periode.split(",") |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
157 |
start_year = int(years[0]) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
158 |
end_year = int(years[0:2][-1]) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
159 |
delta = max(1, (end_year-start_year)/2) |
| 127 | 160 |
minstart = start_year - delta |
161 |
maxend = end_year + delta |
|
| 132 | 162 |
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
|
163 |
years__start_year__lte = end_year, |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
164 |
years__end_year__lte = maxend, |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
165 |
years__start_year__gte = minstart) |
| 132 | 166 |
matchtagids += [t.id for t in matchtagqs] |
167 |
contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, |
|
| 127 | 168 |
taggedsheet__order__lte = max_tag_order) |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
169 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
170 |
if label: |
| 135 | 171 |
masters = [] |
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
172 |
for txtlbl in label.split(","): |
| 135 | 173 |
matchtagqs = Tag.objects.select_related('dbpedia_fields').filter(label__iexact = txtlbl.strip()) |
174 |
for t in matchtagqs: |
|
175 |
if t.id not in matchtagids: |
|
176 |
matchtagids.append(t.id) |
|
177 |
if t.dbpedia_fields: |
|
178 |
masters.append(t.dbpedia_fields) |
|
179 |
||
| 132 | 180 |
contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, |
| 127 | 181 |
taggedsheet__order__lte = max_tag_order) |
| 135 | 182 |
translationqs = DbpediaFieldsTranslation.objects.select_related("master", "master__tag").filter(master__in = masters, language_code=request.LANGUAGE_CODE) |
183 |
labeltranslations = [{'label':t.master.label, 'translated_label':t.label} for t in translationqs] |
|
184 |
||
| 127 | 185 |
if country: |
186 |
for country_uri in country.split(","): |
|
| 132 | 187 |
matchtagqs = Tag.objects.filter(locatedin__country__dbpedia_uri = country_uri) |
188 |
matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids] |
|
189 |
contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, |
|
| 127 | 190 |
taggedsheet__order__lte = max_tag_order) |
| 131 | 191 |
if contentlist: |
192 |
contentqs = contentqs.filter(id__in = contentlist.split(",")) |
|
193 |
||
| 127 | 194 |
tagqs = tagqs.filter(datasheet__in = contentqs) |
195 |
countryqs = countryqs.filter(includes__tag__taggedsheet__datasheet__in = contentqs) |
|
196 |
discqs = discqs.filter(datasheet__in = contentqs) |
|
197 |
yearqs = yearqs.filter(tag__taggedsheet__datasheet__in = contentqs) |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
198 |
|
| 131 | 199 |
if contentlist is None: |
| 127 | 200 |
contentqs.order_by('?') |
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
119
diff
changeset
|
201 |
|
| 127 | 202 |
cont_count = contentqs.count() |
203 |
||
204 |
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]]) |
|
205 |
contentids = contenus.keys() |
|
206 |
||
207 |
qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids) |
|
208 |
for dse in qs: |
|
|
133
e821a1ae9773
small query optimisation in filter
ymh <ymh.work@gmail.com>
parents:
132
diff
changeset
|
209 |
contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude} |
| 127 | 210 |
|
| 135 | 211 |
qs = list(TaggedSheet.objects.select_related('tag', 'tag__dbpedia_fields').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order')) |
212 |
||
213 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = [ts.tag.dbpedia_fields for ts in qs], language_code = request.LANGUAGE_CODE) |
|
214 |
translations = dict([(trans.master_id,trans.label) for trans in transqs]) |
|
215 |
||
| 127 | 216 |
for ts in qs: |
| 132 | 217 |
match_tag = ts.tag.id in matchtagids |
| 135 | 218 |
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 | 219 |
|
220 |
if match_tag: |
|
|
133
e821a1ae9773
small query optimisation in filter
ymh <ymh.work@gmail.com>
parents:
132
diff
changeset
|
221 |
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
|
222 |
|
| 127 | 223 |
if contentlist is None: |
224 |
contenus = sorted(contenus.values(),key=lambda e: -e['score']) |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
225 |
else: |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
226 |
contenus = contenus.values() |
|
133
e821a1ae9773
small query optimisation in filter
ymh <ymh.work@gmail.com>
parents:
132
diff
changeset
|
227 |
|
| 134 | 228 |
#tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count] |
229 |
tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label')[:tag_count] |
|
| 135 | 230 |
# hack to add only necessary fields in the group by |
| 134 | 231 |
# contournement bug https://code.djangoproject.com/ticket/17144 |
232 |
tagqs.query.clear_select_fields() |
|
233 |
tagqs.query.add_fields(['id','label'], False) |
|
234 |
tagqs.query.set_group_by() |
|
235 |
||
| 135 | 236 |
tagqslist = list(tagqs) |
|
133
e821a1ae9773
small query optimisation in filter
ymh <ymh.work@gmail.com>
parents:
132
diff
changeset
|
237 |
|
| 135 | 238 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in tagqslist], language_code = request.LANGUAGE_CODE) |
239 |
translations = dict([(trans.master_id,trans.label) for trans in transqs]) |
|
240 |
||
241 |
tags = [{'id': tag.id, '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 tagqslist] |
|
| 127 | 242 |
|
243 |
countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet')) |
|
244 |
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
|
245 |
|
| 135 | 246 |
discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10]) |
247 |
||
248 |
transqs = DbpediaFieldsTranslation.objects.filter(master__in = [tag.dbpedia_fields for tag in discqslist], language_code = request.LANGUAGE_CODE) |
|
249 |
translations = dict([(trans.master_id,trans.label) for trans in transqs]) |
|
250 |
||
251 |
||
252 |
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 | 253 |
|
254 |
years = {} |
|
255 |
yearqs = yearqs.annotate(nb=Count('tag__taggedsheet')) |
|
256 |
for ty in yearqs: |
|
257 |
for year in range(ty.start_year, ty.end_year): |
|
| 131 | 258 |
years[year] = ty.nb + (years[year] if year in years else 0) |
| 127 | 259 |
|
|
119
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
260 |
yearchange = [] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
261 |
for year in sorted(years.keys()): |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
262 |
score = years[year] |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
263 |
if year < 2011: |
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
119
diff
changeset
|
264 |
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
|
265 |
yearchange.append({'year': year, 'score': score}) |
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
119
diff
changeset
|
266 |
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
|
267 |
yearchange.append({'year': year+1, 'score': 0}) |
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
268 |
|
| 135 | 269 |
tag_translations = {} |
270 |
for t in itertools.chain(labeltranslations,disciplines,tags): |
|
271 |
tag_translations[t['label']] = t['translated_label'] |
|
272 |
for c in contenus: |
|
273 |
for t in c['tags']: |
|
274 |
tag_translations[t['label']] = t['translated_label'] |
|
275 |
||
276 |
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
|
277 |
|
|
e3ebe3545f72
first implementation of django version.
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
278 |
return HttpResponse(content=json.dumps(output), mimetype='application/json') |