Ejemplo n.º 1
0
def get_terms(request):
    """Given an ajax request, return, in a json, the index terms"""
    context = request.REQUEST.get('context')
    lang = request.REQUEST.get('lang', 'en')
    service = request.REQUEST.get('service')    
    service = service if service in WEB_SERVICES.keys() or not service else 'yahoo'
    use_service = bool(service)    
    
    if context:        
        terms = build_query(context, language=lang, use_web_service=use_service, web_service=service)
    else:
        terms = ""
        
    if request.is_ajax():
        return HttpResponse(json.dumps({'terms': terms}, ensure_ascii=False), mimetype="application/json")
    else:
        return HttpResponse(terms, mimetype="text/plain")
Ejemplo n.º 2
0
def get_recommendations(request):    
    #get the terms
    context = request.REQUEST['context']
    lang = request.REQUEST.get('lang', 'en')
    #default to a supported language:
    lang = lang if lang in [e[0] for e in settings.LANGUAGES] else 'en'
    service = request.REQUEST.get('service', '')    
    service = service if service in WEB_SERVICES.keys() or (not service and lang == 'en') else "yahoo"#'tagthe'
    use_service = bool(service)    
    
    try:
        terms = build_query(context, language=lang, use_web_service=use_service, web_service=service, fail_silently=False)
    except:
        return HttpResponseServerError("Error extracting terms")
     
    #do the search
    results = do_search(terms, lang=lang)
    #re-rank
    if hasattr(request, 'profile'):
        results=re_rank(request.profile, results)
    else:
        return HttpResponseBadRequest("No profile found")
    #return    
    return {'results': results, 'terms': unicode(terms)}