136 qs = qs.filter(taggedsheet__order__lte = max_tag_order) |
133 qs = qs.filter(taggedsheet__order__lte = max_tag_order) |
137 |
134 |
138 if contentlist: |
135 if contentlist: |
139 qs = qs.filter(id__in = contentlist.split(",")) |
136 qs = qs.filter(id__in = contentlist.split(",")) |
140 |
137 |
141 if contentlist is None: |
138 # if contentlist is None: |
142 qs = qs.order_by('?') |
139 # qs = qs.order_by('?') |
143 |
140 |
144 |
141 qs = qs.distinct() |
145 for content in qs: |
142 |
146 cont_count += 1 |
143 contenus = dict([(content.id, {'score' : 0, 'tags' : [], 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url}) for content in qs]) |
147 contenus[content.id] = {'score' : 0, 'tags' : [], 'id':content.id, 'title': content.title, 'description': content.description, 'url': content.url} |
144 |
148 |
145 #countries = dict([ (cc.label, {'isocode':cc.isocode, 'score':0}) for cc in CountryCode.objects.all() ]) |
149 |
146 #disciplines = dict([ (d.label, {'label':d.label, 'score':0}) for d in Tag.objects.filter(category__label = "Discipline artistique")]) |
150 countries = dict([ (cc.label, {'isocode':cc.isocode, 'score':0}) for cc in CountryCode.objects.all() ]) |
147 |
151 disciplines = dict([ (d.label, {'label':d.label, 'score':0}) for d in Tag.objects.filter(category__label = "Discipline artistique")]) |
148 countries = {} |
|
149 disciplines = {} |
152 |
150 |
153 tags = {} |
151 tags = {} |
154 |
152 |
155 contentids = contenus.keys() |
153 contentids = contenus.keys() |
156 qs = TaggedSheet.objects.select_related('tag').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order') |
154 cont_count = len(contentids) |
|
155 |
|
156 qs = TaggedSheet.objects.select_related('tag','tag__category','tag__locatedin__country').filter(datasheet__in = contentids, order__lte = max_tag_order).order_by('order') |
157 for ts in qs: |
157 for ts in qs: |
158 match_tag = ts.tag.id in globalids |
158 match_tag = ts.tag.id in globalids |
159 contenus[ts.datasheet.id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag}) |
159 contenutags = {'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag } |
|
160 |
|
161 # if ts.tag.category is not None: |
|
162 # contenutags['category'] = ts.tag.category.label |
|
163 |
|
164 contenus[ts.datasheet.id]['tags'].append(contenutags) |
|
165 |
160 tagscore = 2*max_tag_order - ts.order |
166 tagscore = 2*max_tag_order - ts.order |
161 if ts.tag.id not in tags: |
167 if ts.tag.category is not None and ts.tag.category.label not in [u'Discipline artistique', u'Datation', u'Localisation']: |
162 tags[ts.tag.id] = {'id':ts.tag.id,'label':ts.tag.label, 'score':0} |
168 if ts.tag.id not in tags: |
163 tags[ts.tag.id]['score'] += tagscore |
169 tags[ts.tag.id] = {'id':ts.tag.id,'label':ts.tag.label, 'score':0} |
|
170 tags[ts.tag.id]['score'] += 1 |
|
171 if match_tag: |
|
172 tags[ts.tag.id]['match'] = True |
|
173 |
164 if match_tag: |
174 if match_tag: |
165 contenus[ts.datasheet.id]['score'] += tagscore |
175 contenus[ts.datasheet.id]['score'] += tagscore |
166 tags[ts.tag.id]['match'] = True |
176 |
167 if ts.tag.label in countries: |
177 if ts.tag.category is not None and ts.tag.category.label == u'Discipline artistique': |
168 countries[ts.tag.label]['score'] += tagscore |
178 if ts.tag.label not in disciplines: |
169 if ts.tag.label in disciplines: |
179 disciplines[ts.tag.label] = {'label':ts.tag.label, 'score':0} |
170 disciplines[ts.tag.label]['score'] += tagscore |
180 disciplines[ts.tag.label]['score'] += 1 |
171 |
181 |
172 content_count = content_count if not contentlist else len(contenus) |
182 if ts.tag.locatedin is not None: |
173 contenus = sorted(contenus.values(),key=lambda e: e.get('score', 0))[0:content_count] |
183 country_id = ts.tag.locatedin.country.id |
|
184 if country_id not in countries: |
|
185 countries[country_id] = {'id':country_id, 'dbpedia_uri':ts.tag.locatedin.country.dbpedia_uri, 'score': 0} |
|
186 countries[country_id]['score'] += 1 |
|
187 |
|
188 if contentlist is None: |
|
189 contenus = sorted(contenus.values(),key=lambda e: -e['score'])[0:content_count] |
|
190 contenus = [contenu for contenu in contenus if contenu['score']] |
|
191 |
174 countries = countries.values() |
192 countries = countries.values() |
175 tags = sorted(tags.values(), key=lambda e: e.get('score', 0))[0:tag_count] |
193 tags = sorted(tags.values(), key=lambda e: -e['score'])[0:tag_count] |
176 disciplines = sorted(disciplines.values(), key=lambda e: e.get('score', 0))[0:10] |
194 disciplines = sorted(disciplines.values(), key=lambda e: -e['score'])[0:10] |
177 years = {} |
195 years = {} |
|
196 |
178 if contentids: |
197 if contentids: |
179 qs = TagYears.objects.values('start_year', 'end_year').annotate(order_count=Count('tag__taggedsheet'), order_sum=Sum("tag__taggedsheet__order")).filter(tag__taggedsheet__order__lte=max_tag_order, tag__taggedsheet__datasheet__in = contentids) |
198 qs = TagYears.objects.filter(tag__taggedsheet__datasheet__in = contentids).annotate(nb=Count('tag__taggedsheet')) |
180 for ty in qs: |
199 for ty in qs: |
181 for year in range(ty['start_year'], ty['end_year']): |
200 for year in range(ty.start_year, ty.end_year): |
182 years[year] = (2*max_tag_order*ty['order_count']-ty['order_sum'])/(ty['end_year']-ty['start_year']) + years[year] if year in years else 0 |
201 years[year] = ty.nb + years[year] if year in years else 0 |
183 # $rq = pg_query("SELECT U.start_year, U.end_year, SUM(".(2*$max_tag_order)." - V.order)/(U.end_year + 1 - U.start_year) score FROM hdaviz_years U, hdabo_taggedsheet V WHERE U.tag_id = V.tag_id AND V.order <= $max_tag_order AND V.datasheet_id IN ($contentids) GROUP BY U.start_year, U.end_year"); |
|
184 # while($ligne = pg_fetch_row($rq)) { |
|
185 # foreach(range($ligne[0], $ligne[1]) as $year) { |
|
186 # $years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 ); |
|
187 |
202 |
188 else: |
203 else: |
189 |
204 |
190 for ds in Datasheet.objects.order_by("?")[:content_count]: |
205 for ds in Datasheet.objects.order_by("?")[:content_count]: |
191 contenus[ds.id] = {'id':ds.id, 'title':ds.title, 'description':ds.description, 'url':ds.url, 'tags':[]} |
206 contenus[ds.id] = {'id':ds.id, 'title':ds.title, 'description':ds.description, 'url':ds.url, 'tags':[]} |
|
207 |
192 cont_count = Datasheet.objects.count() |
208 cont_count = Datasheet.objects.count() |
193 |
209 |
194 qs = TaggedSheet.objects.select_related('tag','datasheet').filter(datasheet__id__in = contenus.keys(), order__lte = max_tag_order).order_by("order").only('order','tag__label','tag__id','datasheet__id') |
210 qs = TaggedSheet.objects.select_related('tag','datasheet').filter(datasheet__id__in = contenus.keys(), order__lte = max_tag_order).order_by("order").only('order','tag__label','tag__id','datasheet__id') |
195 for ts in qs: |
211 for ts in qs: |
196 contenus[ts.datasheet.id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order}) |
212 contenus[ts.datasheet.id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order}) |
197 contenus = contenus.values() |
213 contenus = contenus.values() |
198 |
214 |
199 cursor = connection.cursor() #@UndefinedVariable |
215 qs = Tag.objects.exclude(category__label = u"Localisation").exclude(category__label = u"Datation").exclude(category__label = u"Discipline artistice").annotate(nb=Count('datasheet')).order_by('-nb')[:tag_count] |
200 try: |
216 tags = [{'id': tag.id, 'label': tag.label, 'score': tag.nb} for tag in qs] |
201 cursor.execute("select t.id, t.label, sum(2*%s-ts.order) as score from hdabo_tag as t join hdabo_taggedsheet as ts on t.id = ts.tag_id where ts.order < %s group by t.id, t.label order by score limit %s",[max_tag_order, max_tag_order, tag_count]) |
217 |
202 tags = [{'id': t[0], 'label':t[1], 'score':t[2]} for t in cursor.fetchall()] |
218 qs = Country.objects.annotate(nb=Count('includes__tag__taggedsheet')).order_by('-nb') |
203 #tags = sorted([{'id':tag.id,'label':tag.label,'score':2*max_tag_order*tag.count_score - tag.sum_score} for tag in qs], key=lambda t:t['score'])[:tag_count] |
219 countries = [{'id': country.id, 'dbpedia_uri': country.dbpedia_uri, 'score': country.nb} for country in qs] |
204 finally: |
220 |
205 cursor.close() |
221 qs = Tag.objects.annotate(nb=Count('taggedsheet')).filter(category = 5).order_by('-nb')[:10] |
206 cursor = connection.cursor() #@UndefinedVariable |
222 disciplines = [{'label':tag.label,'score':tag.nb} for tag in qs] |
207 try: |
|
208 cursor.execute("select c.isocode as isocode, sum(2*%s-ts.order) as score from hdabo_tag as t join hdabo_taggedsheet as ts on t.id = ts.tag_id join hdalab_countrycode as c on t.label = c.label group by c.isocode", [max_tag_order]) |
|
209 countries = [{'isocode': r[0], 'score':r[1]} for r in cursor.fetchall()] |
|
210 |
|
211 finally: |
|
212 cursor.close() |
|
213 |
|
214 qs = Tag.objects.annotate(count_score=Count('taggedsheet'),sum_score=Sum('taggedsheet__order')).filter(taggedsheet__order__lte = max_tag_order, category__label__iexact = u"Discipline") |
|
215 disciplines = sorted([{'label':tag.label,'score':2*max_tag_order*tag.count_score - tag.sum_score} for tag in qs], key=lambda t:t.score, reverse=True)[:10] |
|
216 |
223 |
217 years = {} |
224 years = {} |
218 qs = TagYears.objects.values('start_year', 'end_year').annotate(order_count=Count('tag__taggedsheet'), order_sum=Sum("tag__taggedsheet__order")).filter(tag__taggedsheet__order__lte=max_tag_order) |
225 qs = TagYears.objects.annotate(nb=Count('tag__taggedsheet')) |
219 for ty in qs: |
226 for ty in qs: |
220 for year in range(ty['start_year'], ty['end_year']): |
227 for year in range(ty.start_year, ty.end_year): |
221 years[year] = (2*max_tag_order*ty['order_count']-ty['order_sum'])/(ty['end_year']-ty['start_year']) + years[year] if year in years else 0 |
228 years[year] = ty.nb + years[year] if year in years else 0 |
222 |
229 |
223 yearchange = [] |
230 yearchange = [] |
224 for year in sorted(years.keys()): |
231 for year in sorted(years.keys()): |
225 score = years[year] |
232 score = years[year] |
226 if year < 2011: |
233 if year < 2011: |
227 if year-1 not in years or years[year-1] != score: |
234 if (year-1 not in years and score != 0) or (year-1 in years and years[year-1] != score): |
228 yearchange.append({'year': year, 'score': score}) |
235 yearchange.append({'year': year, 'score': score}) |
229 if year+1 not in years and year != -1: |
236 if year+1 not in years and year != -1 and score != 0: |
230 yearchange.append({'year': year+1, 'score': 0}) |
237 yearchange.append({'year': year+1, 'score': 0}) |
231 |
238 |
232 output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines} |
239 output = {'count': cont_count, 'contents': contenus, 'tags':tags, 'sparkline':yearchange, 'countries':countries, 'disciplines':disciplines} |
233 |
240 |
234 return HttpResponse(content=json.dumps(output), mimetype='application/json') |
241 return HttpResponse(content=json.dumps(output), mimetype='application/json') |