diff -r 3a34fc74e0d2 -r 91bc8521e3cb web/hdalab/views/ajax.py --- a/web/hdalab/views/ajax.py Sun Sep 09 21:56:43 2012 +0200 +++ b/web/hdalab/views/ajax.py Thu Oct 04 12:25:12 2012 +0200 @@ -6,7 +6,7 @@ ''' from django.conf import settings from django.core.cache import cache -from django.db.models import Q, Count +from django.db.models import Q, Count, Min from django.http import HttpResponse from hdabo.models import Tag, Datasheet, TaggedSheet from hdalab.models import HdaSession, Country, TagYears, DatasheetExtras @@ -479,4 +479,37 @@ outputstr = json.dumps(output) cache.set(cache_key, outputstr) - return HttpResponse(content=outputstr, mimetype='application/json') \ No newline at end of file + return HttpResponse(content=outputstr, mimetype='application/json') + +def subtree(tree): + MAX_TAG_ORDER = 8 + label = tree['label'] + sub = tree.get('contents',[]) + + 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() + + 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] + + contents = sorted(contents, key=lambda e: -e['score']) + + res = { 'category': label } + + if len(contents): + res['contents'] = contents + + if len(sub): + subcats = [subtree(st) for st in sub] + subcats = [sc for sc in subcats if len(sc.get('contents',[])) or len(sc.get('sub_categories',[]))] + res['sub_categories'] = subcats + + return res + +def filltree(request): + + tree = request.GET.get('tree','{}') + + treeobj = json.loads(tree) + + res = subtree(treeobj) + + return HttpResponse(content=json.dumps(res), mimetype='application/json')