421 outputstr = json.dumps(output) |
421 outputstr = json.dumps(output) |
422 cache.set(cache_key, outputstr) |
422 cache.set(cache_key, outputstr) |
423 |
423 |
424 return HttpResponse(content=outputstr, mimetype='application/json') |
424 return HttpResponse(content=outputstr, mimetype='application/json') |
425 |
425 |
426 def contentsbytag(request): |
|
427 |
|
428 lang = request.GET.get('lang',request.LANGUAGE_CODE) |
|
429 label = request.GET.get('label', None).strip().lower() |
|
430 max_tag_order = int(request.GET.get('mto', '30')) |
|
431 content_count = int(request.GET.get('contentcount', '30')) |
|
432 |
|
433 cache_key = fix_cache_key("contentsbytag-%s"%unicode(label).encode("utf-8")) |
|
434 |
|
435 outputstr = cache.get(cache_key) |
|
436 |
|
437 if outputstr is None: |
|
438 |
|
439 matchtagids = [] |
|
440 |
|
441 contentqs = Datasheet.objects.filter(validated=True) |
|
442 |
|
443 no_translate_langs = [ 'fr' ] |
|
444 if lang in no_translate_langs: |
|
445 tagsqs = Tag.objects.filter( label__iexact = label ) |
|
446 else: |
|
447 tagsqs = Tag.objects.select_related('dbpedia_fields').filter(dbpedia_fields__translations__label__iexact = label, dbpedia_fields__translations__language_code=lang) |
|
448 |
|
449 contentqs = contentqs.select_related('taggedsheet__tag').filter(taggedsheet__tag__in=tagsqs).distinct() |
|
450 |
|
451 cont_count = contentqs.count() |
|
452 |
|
453 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]]) |
|
454 contentids = contenus.keys() |
|
455 |
|
456 qs = DatasheetExtras.objects.select_related('insee').filter(datasheet__in = contentids) |
|
457 for dse in qs: |
|
458 contenus[dse.datasheet_id]['coords'] = {'city_name': dse.insee.city_name, 'latitude': dse.insee.latitude, 'longitude': dse.insee.longitude} |
|
459 |
|
460 qs = list(TaggedSheet.objects.select_related('tag', 'tag__dbpedia_fields').filter(datasheet__in = contentids).order_by('order')) |
|
461 |
|
462 if lang in no_translate_langs: |
|
463 translations = {} |
|
464 else: |
|
465 transqs = DbpediaFieldsTranslation.objects.filter(master__in = [ts.tag.dbpedia_fields for ts in qs], language_code = lang) |
|
466 translations = dict([(trans.master_id,trans.label) for trans in transqs]) |
|
467 |
|
468 for ts in qs: |
|
469 translated_label = translations.get(ts.tag.dbpedia_fields.id, ts.tag.label) if ts.tag.dbpedia_fields is not None else ts.tag.label |
|
470 match_tag = ( label == translated_label.lower() ) |
|
471 contenus[ts.datasheet_id]['tags'].append({'id': ts.tag.id, 'label':ts.tag.label, 'order':ts.order, 'match': match_tag , 'translated_label': translated_label}) |
|
472 |
|
473 if match_tag: |
|
474 contenus[ts.datasheet_id]['score'] += 2*max_tag_order - ts.order |
|
475 |
|
476 contenus = sorted(contenus.values(),key=lambda e: -e['score']) |
|
477 |
|
478 output = {'count': cont_count, 'contents': contenus} |
|
479 outputstr = json.dumps(output) |
|
480 cache.set(cache_key, outputstr) |
|
481 |
|
482 return HttpResponse(content=outputstr, mimetype='application/json') |
|
483 |
|
484 def subtree(tree): |
426 def subtree(tree): |
485 MAX_TAG_ORDER = 8 |
427 MAX_TAG_ORDER = 16 |
486 label = tree['label'] |
428 label = tree['label'] |
487 sub = tree.get('contents',[]) |
429 sub = tree.get('contents',[]) |
488 |
430 |
489 datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__iexact = label, taggedsheet__order__lte = MAX_TAG_ORDER).annotate(tagorder=Min('taggedsheet__order')).select_related('organisation').distinct() |
431 datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__iexact = label, taggedsheet__order__lte = MAX_TAG_ORDER).annotate(tagorder=Min('taggedsheet__order')).select_related('organisation').distinct() |
490 |
432 |
491 contents = [{ 'description': ds.description, 'title': ds.title, 'url': ds.url, 'score': MAX_TAG_ORDER - ds.tagorder, 'id': ds.id, 'hda_id': ds.hda_id, 'organization': ds.organisation.name } for ds in datasheets] |
433 contents = [{ 'description': ds.description, 'title': ds.title, 'url': ds.url, 'score': int((MAX_TAG_ORDER - ds.tagorder)/2), 'id': ds.id, 'hda_id': ds.hda_id, 'organization': ds.organisation.name } for ds in datasheets] |
492 |
434 |
493 contents = sorted(contents, key=lambda e: -e['score']) |
435 contents = sorted(contents, key=lambda e: -e['score']) |
494 |
436 |
495 res = { 'category': label } |
437 res = { 'category': label } |
496 |
438 |