src/hdalab/views/ajax.py
changeset 324 573043a98b44
parent 291 44af3e5e4114
child 326 fc8961398609
equal deleted inserted replaced
323:67cff4e39ad8 324:573043a98b44
    96     globtags = {}
    96     globtags = {}
    97     resobj = None
    97     resobj = None
    98     master_category = WpCategory.objects.filter(label__iexact=label)[0:1]
    98     master_category = WpCategory.objects.filter(label__iexact=label)[0:1]
    99     if len(master_category):
    99     if len(master_category):
   100         resobj = subcat(master_category[0], globtags, 1, MAX_LEVEL )
   100         resobj = subcat(master_category[0], globtags, 1, MAX_LEVEL )
   101     
   101        
   102 #    tag_list = [k for k in globtags]
   102     #datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__in = tag_list, taggedsheet__order__lte = MAX_TAG_ORDER).distinct()
   103     
       
   104 #    if len(tag_list):
       
   105     contents = []
       
   106 #    datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__in = tag_list, taggedsheet__order__lte = MAX_TAG_ORDER).distinct()
       
   107     datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__iexact = label, taggedsheet__order__lte = ROOT_MAX_TAG_ORDER).select_related('organisation').distinct()
   103     datasheets = Datasheet.objects.filter(validated = True, taggedsheet__tag__label__iexact = label, taggedsheet__order__lte = ROOT_MAX_TAG_ORDER).select_related('organisation').distinct()
   108     for datasheet in datasheets:
   104     for datasheet in datasheets:
   109         # Calculating where we add the datasheet in the tree
   105         # Calculating where we add the datasheet in the tree
   110         maintag = None
   106         maintag = None
   111         maintagscore = -5
   107         maintagscore = -5
   131                 'organization': datasheet.organisation.name,
   127                 'organization': datasheet.organisation.name,
   132                 'organization_url': datasheet.organisation.website,
   128                 'organization_url': datasheet.organisation.website,
   133                 'score': max(dsscore, rootscore)
   129                 'score': max(dsscore, rootscore)
   134             })
   130             })
   135     cleantags(resobj)
   131     cleantags(resobj)
   136                     
       
   137 #        resobj['contents'] = [{'id': d.id, 'title': d.title, 'tags': [t.label for t in d.tags.filter(taggedsheet__order__lte=5)]} for d in datasheets]
       
   138     
   132     
   139     return HttpResponse(content=json.dumps(resobj), mimetype='application/json')
   133     return HttpResponse(content=json.dumps(resobj), mimetype='application/json')
   140 
   134 
   141 def sessioninfo(request):
   135 def sessioninfo(request):
   142     
   136     
   182 def tagsearch(request):
   176 def tagsearch(request):
   183     
   177     
   184     q = request.GET.get('term',None)
   178     q = request.GET.get('term',None)
   185     maxcount = int(request.GET.get('count','40'))
   179     maxcount = int(request.GET.get('count','40'))
   186     lang = request.GET.get('lang',request.LANGUAGE_CODE)
   180     lang = request.GET.get('lang',request.LANGUAGE_CODE)
       
   181     count_notices_str = request.REQUEST.get("count_notices")
       
   182     count_notices_bool = True
       
   183     if count_notices_str:
       
   184         count_notices_bool = {'true': True, 'false': False, "0": False, "1": True}.get(count_notices_str.lower())
   187     
   185     
   188     stemming_langs = [ 'fr', 'en', 'de', 'it' ]
   186     stemming_langs = [ 'fr', 'en', 'de', 'it' ]
   189     # For Japanese, there are no word boundaries, we should not use the regexp in that case
   187     # For Japanese, there are no word boundaries, we should not use the regexp in that case
   190     no_translate_langs = [ 'fr' ]
   188     no_translate_langs = [ 'fr' ]
   191     
   189     
   203                 qs = qs.filter(dbpedia_fields__translations__label__iregex=qrx, dbpedia_fields__translations__language_code=lang, dbpedia_fields__translations__is_label_translated = True)
   201                 qs = qs.filter(dbpedia_fields__translations__label__iregex=qrx, dbpedia_fields__translations__language_code=lang, dbpedia_fields__translations__is_label_translated = True)
   204             else:
   202             else:
   205                 qs = qs.filter(dbpedia_fields__translations__label__icontains=q, dbpedia_fields__translations__language_code=lang, dbpedia_fields__translations__is_label_translated = True)
   203                 qs = qs.filter(dbpedia_fields__translations__label__icontains=q, dbpedia_fields__translations__language_code=lang, dbpedia_fields__translations__is_label_translated = True)
   206     else:
   204     else:
   207         qs = Tag.objects.filter(~Q(dbpedia_uri = None))
   205         qs = Tag.objects.filter(~Q(dbpedia_uri = None))
   208            
   206     
   209     qs = qs.annotate(nb=Count('datasheet',distinct=True)).order_by('-nb')[:maxcount]
   207     if count_notices_bool:
       
   208         qs = qs.annotate(nb=Count('datasheet',distinct=True)).order_by('-nb')[:maxcount]
       
   209     else:
       
   210         qs = qs.distinct()[:maxcount]
   210     
   211     
   211     qslist = list(qs)
   212     qslist = list(qs)
   212     
   213     
   213     if lang in no_translate_langs:
   214     if lang in no_translate_langs:
   214         translations = {}
   215         translations = {}
   219     res = []
   220     res = []
   220     
   221     
   221     for t in qslist:
   222     for t in qslist:
   222         if hasattr(t, 'dbpedia_fields'):
   223         if hasattr(t, 'dbpedia_fields'):
   223             dbfields = t.dbpedia_fields
   224             dbfields = t.dbpedia_fields
   224             resobj = {'original_label':t.label,'nb':t.nb}
   225             resobj = {'original_label':t.label, 'url':t.dbpedia_uri}
       
   226             if count_notices_bool:
       
   227                 resobj['nb'] = t.nb
   225             resobj['thumbnail'] = dbfields.thumbnail if dbfields is not None else None
   228             resobj['thumbnail'] = dbfields.thumbnail if dbfields is not None else None
   226             
   229             
   227             if t.id in translations:
   230             if t.id in translations:
   228                 resobj['value'] = translations[t.id]['label']
   231                 resobj['value'] = translations[t.id]['label']
   229                 resobj['abstract'] = translations[t.id]['abstract']
   232                 resobj['abstract'] = translations[t.id]['abstract']