3 Created on Jan 31, 2012 |
3 Created on Jan 31, 2012 |
4 |
4 |
5 @author: ymh |
5 @author: ymh |
6 ''' |
6 ''' |
7 from django.conf import settings |
7 from django.conf import settings |
8 from django.db import connection |
8 from django.db.models import Q, Count |
9 from django.db.models import Q, Count, Sum |
|
10 from django.http import HttpResponse |
9 from django.http import HttpResponse |
11 from hdabo.models import Tag, Datasheet, TaggedSheet |
10 from hdabo.models import Tag, Datasheet, TaggedSheet |
12 from hdalab.models import TagLinks, HdaSession, Country, TagYears, DatasheetExtras |
11 from hdalab.models import (TagLinks, HdaSession, Country, TagYears, |
|
12 DatasheetExtras) |
|
13 from hdalab.models.dataviz import DbpediaFieldsTranslation |
13 import django.utils.simplejson as json |
14 import django.utils.simplejson as json |
14 import hmac |
15 import hmac |
|
16 import itertools |
15 import uuid |
17 import uuid |
16 |
18 |
17 def taginfo(request): |
19 def taginfo(request): |
18 label = request.GET.get('label', None) |
20 label = request.GET.get('label', None) |
19 |
21 |
20 resobj = {'requested_label' : label} |
22 resobj = {'requested_label' : label} |
21 |
23 |
22 resobj["content_count"] = Datasheet.objects.filter(taggedsheet__tag__label__iexact = label).distinct().count() |
24 resobj["content_count"] = Datasheet.objects.filter(taggedsheet__tag__label__iexact = label).distinct().count() |
23 |
25 |
24 res = Tag.objects.select_related('dbpedia_fields').filter(label__iexact = label).order_by('-dbpedia_uri')[0:1] |
26 res = Tag.objects.select_related('dbpedia_fields').filter(label__iexact = label).order_by('-dbpedia_uri')[0:1] |
25 if len(res) == 1: |
27 if len(res) == 1: |
26 restag = res.get() |
28 restag = res.get() |
27 resobj["dbpedia_uri"] = restag.dbpedia_uri |
29 resobj["dbpedia_uri"] = restag.dbpedia_uri |
28 if resobj["dbpedia_uri"] is not None and restag.dbpedia_fields is not None: |
30 if resobj["dbpedia_uri"] is not None and restag.dbpedia_fields is not None: |
29 dbfield = restag.dbpedia_fields |
31 dbfield = restag.dbpedia_fields |
30 resobj["abstract"] = dbfield.abstract |
32 resobj["abstract"] = dbfield.abstract |
31 resobj["dbpedia_label"] = dbfield.label |
33 resobj["dbpedia_label"] = dbfield.label |
32 resobj["thumbnail"] = dbfield.thumbnail |
34 resobj["thumbnail"] = dbfield.thumbnail |
33 res = Tag.objects.filter(label__iexact = label).order_by('-wikipedia_url')[0:1] |
35 transqs = DbpediaFieldsTranslation.objects.filter(master=dbfield, language_code=request.LANGUAGE_CODE)[0:1] |
34 if len(res) == 1: |
36 if transqs: |
35 resobj["wikipedia_url"] = res.get().wikipedia_url |
37 trans = transqs.get() |
36 |
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 |
37 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))] |
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))] |
38 |
55 |
39 return HttpResponse(content=json.dumps(resobj), mimetype='application/json') |
56 return HttpResponse(content=json.dumps(resobj), mimetype='application/json') |
|
57 |
|
58 |
|
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 |
40 |
82 |
41 |
83 |
42 def sessioninfo(request): |
84 def sessioninfo(request): |
43 |
85 |
44 data = json.loads(request.GET.get('data', "{}")) |
86 data = json.loads(request.GET.get('data', "{}")) |
98 content_count = request.GET.get('contentcount', 12) |
140 content_count = request.GET.get('contentcount', 12) |
99 tag_count = request.GET.get('tagcount', 30) |
141 tag_count = request.GET.get('tagcount', 30) |
100 |
142 |
101 matchtagids = [] |
143 matchtagids = [] |
102 |
144 |
103 tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']) |
145 tagqs = Tag.objects.exclude(category__label__in = ['Datation', 'Localisation', 'Discipline artistique']).select_related('dbpedia_fields') |
104 countryqs = Country.objects |
146 countryqs = Country.objects |
105 discqs = Tag.objects.filter(category__label = u'Discipline artistique') |
147 discqs = Tag.objects.filter(category__label = u'Discipline artistique').select_related('dbpedia_fields') |
106 yearqs = TagYears.objects |
148 yearqs = TagYears.objects |
107 |
149 |
108 contentqs = Datasheet.objects.filter(validated=True) |
150 contentqs = Datasheet.objects.filter(validated=True) |
|
151 labeltranslations = [] |
109 |
152 |
110 if label or periode or country or contentlist : |
153 if label or periode or country or contentlist : |
111 |
154 |
112 if periode: |
155 if periode: |
113 years = periode.split(",") |
156 years = periode.split(",") |
123 matchtagids += [t.id for t in matchtagqs] |
166 matchtagids += [t.id for t in matchtagqs] |
124 contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, |
167 contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, |
125 taggedsheet__order__lte = max_tag_order) |
168 taggedsheet__order__lte = max_tag_order) |
126 |
169 |
127 if label: |
170 if label: |
|
171 masters = [] |
128 for txtlbl in label.split(","): |
172 for txtlbl in label.split(","): |
129 matchtagqs = Tag.objects.filter(label__iexact = txtlbl) |
173 matchtagqs = Tag.objects.select_related('dbpedia_fields').filter(label__iexact = txtlbl.strip()) |
130 matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids] |
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 |
131 contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, |
180 contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, |
132 taggedsheet__order__lte = max_tag_order) |
181 taggedsheet__order__lte = max_tag_order) |
133 |
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 |
134 if country: |
185 if country: |
135 for country_uri in country.split(","): |
186 for country_uri in country.split(","): |
136 matchtagqs = Tag.objects.filter(locatedin__country__dbpedia_uri = country_uri) |
187 matchtagqs = Tag.objects.filter(locatedin__country__dbpedia_uri = country_uri) |
137 matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids] |
188 matchtagids += [t.id for t in matchtagqs if t.id not in matchtagids] |
138 contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, |
189 contentqs = contentqs.filter(taggedsheet__tag__in = matchtagqs, |
155 |
206 |
156 qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids) |
207 qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids) |
157 for dse in qs: |
208 for dse in qs: |
158 contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude} |
209 contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude} |
159 |
210 |
160 qs = TaggedSheet.objects.select_related('tag').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order') |
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 |
161 for ts in qs: |
216 for ts in qs: |
162 match_tag = ts.tag.id in matchtagids |
217 match_tag = ts.tag.id in matchtagids |
163 contenus[ts.datasheet_id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag }) |
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}) |
164 |
219 |
165 if match_tag: |
220 if match_tag: |
166 contenus[ts.datasheet_id]['score'] += 2*max_tag_order - ts.order |
221 contenus[ts.datasheet_id]['score'] += 2*max_tag_order - ts.order |
167 |
222 |
168 if contentlist is None: |
223 if contentlist is None: |
170 else: |
225 else: |
171 contenus = contenus.values() |
226 contenus = contenus.values() |
172 |
227 |
173 #tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count] |
228 #tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count] |
174 tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label')[:tag_count] |
229 tagqs = tagqs.annotate(nb=Count('datasheet')).order_by('-nb').only('id','label')[:tag_count] |
175 # hack to add only necessary fileds in the group by |
230 # hack to add only necessary fields in the group by |
176 # contournement bug https://code.djangoproject.com/ticket/17144 |
231 # contournement bug https://code.djangoproject.com/ticket/17144 |
177 tagqs.query.clear_select_fields() |
232 tagqs.query.clear_select_fields() |
178 tagqs.query.add_fields(['id','label'], False) |
233 tagqs.query.add_fields(['id','label'], False) |
179 tagqs.query.set_group_by() |
234 tagqs.query.set_group_by() |
180 |
235 |
181 #hack : django bug |
236 tagqslist = list(tagqs) |
182 |
237 |
183 tags = [{'id': tag.id, 'label': tag.label, 'score': tag.nb} for tag in tagqs] |
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] |
184 |
242 |
185 countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet')) |
243 countryqs = countryqs.annotate(nb=Count('includes__tag__taggedsheet')) |
186 countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs]) |
244 countries = dict([(country.dbpedia_uri, country.nb) for country in countryqs]) |
187 |
245 |
188 discqs = discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10] |
246 discqslist = list(discqs.annotate(nb=Count('taggedsheet')).order_by('-nb')[:10]) |
189 disciplines = [{'label':tag.label,'score':tag.nb} for tag in discqs] |
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] |
190 |
253 |
191 years = {} |
254 years = {} |
192 yearqs = yearqs.annotate(nb=Count('tag__taggedsheet')) |
255 yearqs = yearqs.annotate(nb=Count('tag__taggedsheet')) |
193 for ty in yearqs: |
256 for ty in yearqs: |
194 for year in range(ty.start_year, ty.end_year): |
257 for year in range(ty.start_year, ty.end_year): |
201 if (year-1 not in years and score != 0) or (year-1 in years and years[year-1] != score): |
264 if (year-1 not in years and score != 0) or (year-1 in years and years[year-1] != score): |
202 yearchange.append({'year': year, 'score': score}) |
265 yearchange.append({'year': year, 'score': score}) |
203 if year+1 not in years and year != -1 and score != 0: |
266 if year+1 not in years and year != -1 and score != 0: |
204 yearchange.append({'year': year+1, 'score': 0}) |
267 yearchange.append({'year': year+1, 'score': 0}) |
205 |
268 |
206 output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines} |
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} |
207 |
277 |
208 return HttpResponse(content=json.dumps(output), mimetype='application/json') |
278 return HttpResponse(content=json.dumps(output), mimetype='application/json') |