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.core.cache import cache |
8 from django.core.cache import cache |
9 from django.db.models import Q, Count |
9 from django.db.models import Q, Count, Min |
10 from django.http import HttpResponse |
10 from django.http import HttpResponse |
11 from hdabo.models import Tag, Datasheet, TaggedSheet |
11 from hdabo.models import Tag, Datasheet, TaggedSheet |
12 from hdalab.models import HdaSession, Country, TagYears, DatasheetExtras |
12 from hdalab.models import HdaSession, Country, TagYears, DatasheetExtras |
13 from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields |
13 from hdalab.models.dataviz import DbpediaFieldsTranslation, DbpediaFields |
14 from hdalab.models.categories import WpCategory, WpCategoryInclusion, TagWpCategory |
14 from hdalab.models.categories import WpCategory, WpCategoryInclusion, TagWpCategory |
478 output = {'count': cont_count, 'contents': contenus} |
478 output = {'count': cont_count, 'contents': contenus} |
479 outputstr = json.dumps(output) |
479 outputstr = json.dumps(output) |
480 cache.set(cache_key, outputstr) |
480 cache.set(cache_key, outputstr) |
481 |
481 |
482 return HttpResponse(content=outputstr, mimetype='application/json') |
482 return HttpResponse(content=outputstr, mimetype='application/json') |
|
483 |
|
484 def subtree(tree): |
|
485 MAX_TAG_ORDER = 8 |
|
486 label = tree['label'] |
|
487 sub = tree.get('contents',[]) |
|
488 |
|
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() |
|
490 |
|
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] |
|
492 |
|
493 contents = sorted(contents, key=lambda e: -e['score']) |
|
494 |
|
495 res = { 'category': label } |
|
496 |
|
497 if len(contents): |
|
498 res['contents'] = contents |
|
499 |
|
500 if len(sub): |
|
501 subcats = [subtree(st) for st in sub] |
|
502 subcats = [sc for sc in subcats if len(sc.get('contents',[])) or len(sc.get('sub_categories',[]))] |
|
503 res['sub_categories'] = subcats |
|
504 |
|
505 return res |
|
506 |
|
507 def filltree(request): |
|
508 |
|
509 tree = request.GET.get('tree','{}') |
|
510 |
|
511 treeobj = json.loads(tree) |
|
512 |
|
513 res = subtree(treeobj) |
|
514 |
|
515 return HttpResponse(content=json.dumps(res), mimetype='application/json') |