def search(request): return redirect('registration-signup') if request.method == 'GET': input_q = request.GET.get("q","") q = name_seperate(input_q) default_num = 10 #results = personal_ranking(s_qs=SearchQuerySet().auto_query(q).models(*TYPE['all']), user=request.user) # Find location latitude, longitude = get_user_location(request) search_city = request.session.get('search_city', 'Default') spatial_results = spatial_ranking(personal_ranking(SearchQuerySet().models(*TYPE['spatial']).filter(searchable=True), request.user), latitude, longitude).auto_query(q).load_all()[:default_num] spatial_results = get_clear_search_results(spatial_results) results = personal_ranking(SearchQuerySet().models(*TYPE['non_spatial']).filter(searchable=True), request.user).load_all().auto_query(q)[:default_num] results = get_clear_search_results(results) results.extend(spatial_results) rtn = [] for result in results: if result.pk not in request.user.get_profile().blocked_by_user_set: rtn.append(result) return render(request, 'search_result.html', { 'results':rtn, 'search_city':search_city, 'q': input_q, 'default_num': default_num, }) else: return HttpResponseRedirect(slugreverse(request.user, "user-profile", args=[request.user.id]))
def reset_user_locaion(request): city_id = request.GET.get("c",None) city_name = request.GET.get("city_name",None) if city_id: latitude, longitude = get_user_location(request, city_id, reset=True, city_name=city_name) return HttpResponse(json.dumps({'latitude':latitude, 'longitude':longitude, 'status':True}), mimetype="application/json") else: return HttpResponse(json.dumps({'status':False}), mimetype="application/json")
def search_filter(request): """ the filter of search. return json with search result html { status: 1(success) or 0(fail), html: search results, more: 1(has more) or 0 (no more), msg: any msg, } """ if request.is_ajax and request.GET: q = request.GET.get("q", "") model = request.GET.get("m", "") city_id = request.GET.get("c", "") city_name = request.GET.get("name", None) from_index = int(request.GET.get("n", "")) to_index= int(request.GET.get("to_n", "")) if q and model: latitude, longitude = get_user_location(request) if city_id == 'null' else get_user_location(request, city_id, reset=True, city_name=city_name) if model == 'all': spatial_results = spatial_ranking(personal_ranking(SearchQuerySet().models(*TYPE['spatial']).filter(searchable=True), request.user), latitude, longitude).auto_query(q).load_all()[from_index:to_index] spatial_results = get_clear_search_results(spatial_results) results = personal_ranking(SearchQuerySet().models(*TYPE['non_spatial']).filter(searchable=True), request.user).load_all().auto_query(q)[from_index:to_index] results = get_clear_search_results(results) results.extend(spatial_results) elif model not in DJANGO_CT: results = spatial_ranking(personal_ranking(SearchQuerySet().models(*TYPE[model]).filter(searchable=True), request.user), latitude, longitude).auto_query(q).load_all()[from_index:to_index] results = get_clear_search_results(results) else: results = personal_ranking(SearchQuerySet().models(*TYPE[model]).filter(searchable=True), request.user).load_all().auto_query(q)[from_index:to_index] results = get_clear_search_results(results) rtn = [] for result in results: if result.pk not in request.user.get_profile().blocked_by_user_set: rtn.append(result) search_result = render_to_string('inc_search_result.html',{ #'results': results[start:start+num] 'results': rtn }, context_instance = RequestContext(request)) response = { 'status' : 1, 'html' : search_result, 'more' : 1, 'msg' : '', 'search_city' : request.session.get('search_city', 'Default'), } else: response = { 'status' : 0, 'html' : '', 'more' : 0, 'msg' : 'error', } return HttpResponse(json.dumps(response), mimetype="application/json") else: response = { 'status' : 0, 'html' : '', 'more' : 0, 'msg' : 'error', } return HttpResponse(json.dumps(response), mimetype="application/json")
def auto_complete_main(request): """ main search input autocomplete """ q = request.GET.get('q', None) """ query handler """ if q is None or q == '': return HttpResponse('', mimetype="text/plain") query_list = process_query(q) results = None latitude, longitude = get_user_location(request) has_query = False for q_o in query_list: if q_o: if not has_query: has_query = True results = personal_ranking(s_qs=SearchQuerySet().filter(searchable=True).filter(SQ(name=q_o)|SQ(name__startswith=q_o)|SQ(nickname__startswith=q_o)), user=request.user) print results else: results = results.filter(SQ(name=q_o)|SQ(name__startswith=q_o)|SQ(nickname__startswith=q_o)) search_results = {} def p_search(search_set, model_name): if model_name in ['event', 'user']: search_results[model_name]['result'] = get_clear_search_results(search_set.load_all()[:3]) elif model_name in ['place', ]: search_results[model_name]['result'] = get_clear_search_results(spatial_ranking(search_set, latitude, longitude).load_all()[:3]) MODELS_ORDER = ['place', 'user', 'event'] for model in MODELS_ORDER: if has_query: search_set = results.models(*TYPE[model]) search_results[model] = {} search_results[model]['thread'] = threading.Thread(target=p_search, args=(search_set, model)) for model_search in search_results: search_results[model_search]['thread'].start() for model_search in search_results: search_results[model_search]['thread'].join() all_results = [] for model in MODELS_ORDER: print search_results all_results.append(search_results[model]['result']) response = [] for key in range(len(all_results)): tmp_response = [] item = [] num = 0 for result in all_results[key]: if result.pk in request.user.get_profile().blocked_by_user_set: continue num = num+1 if num == 1: response.append('title|%s' % MODELS_ORDER[key]) score_field = 'rank_score' score = result.__dict__[score_field] if score_field in result.__dict__ else 'x' try: if result.model_name == 'user': img_url = result.object.get_profile().get_my_photo(request).image50x50.url else: img_url = result.object.get_display_photo().image50x50.url except: img_url = '' extra = "" if result.model_name == 'user': # name = result.object.username name = result.object.get_profile().get_nickname(request) url = result.object.get_profile().get_absolute_url() elif result.model_name == 'place': extra = result.object.address name = result.object.get_display_name() url = result.object.get_absolute_url() else: name = result.object.get_display_name() url = result.object.get_absolute_url() item = [ result.pk, name, result.model_name, '%s' % score, url, img_url, extra, ] tmp_response.append(item) # tmp_response.sort(key=lambda obj: obj[1].lower().startswith(q),reverse=True) response.extend(["|".join(r) for r in tmp_response]) response_txt = '\n'.join(response) return HttpResponse(response_txt, mimetype="text/plain")
def auto_complete_place(request): """ place autocomplete """ q = request.GET.get('q', None) params = request.GET.get('other_params', None) limit = request.GET.get('limit', 10) latlon = None if params: params = json.loads(params) if 'lat' in params and 'lon' in params: latlon = (float(params['lat']), float(params['lon'])) """ query handler """ if q is None or q == '': return HttpResponse('', mimetype="text/plain") query_list = process_query(q) results = None # handle = open("/tmp/search.log","w") # ISOTIMEFORMAT='%Y-%m-%d %X' if latlon: latitude, longitude = latlon else: latitude, longitude = get_user_location(request) # t = time.strftime( ISOTIMEFORMAT, time.localtime() ) # handle.write('start search:%s\n'%t) has_query = False for q_o in query_list: if q_o: if not has_query: has_query = True results = personal_ranking(s_qs=SearchQuerySet().filter(name__startswith=q_o), user=request.user) else: results = results.filter(name__startswith=q_o) if has_query: results = get_clear_search_results( spatial_ranking(results.models(Place), latitude, longitude, pure_dist=True).load_all()[:limit] ) else: results = [] # t = time.strftime( ISOTIMEFORMAT, time.localtime() ) # handle.write('end search:%s\n'%t) tmp_response = [] for result in results: # t = time.strftime( ISOTIMEFORMAT, time.localtime() ) # handle.write('result:%s\n'%t) score_field = '%s_d'%request.user.id score = result.__dict__[score_field] if score_field in result.__dict__ else 'x' img_url = result.object.get_display_photo().image26x26.url img_url_l = result.object.get_display_photo().image105x105.url url = result.object.get_absolute_url() name = result.object.get_display_name() category = result.object.category if len(name) > 30: name = '%s..' % name[:30] item = [ result.pk, name, result.model_name, '%s' % score, url, img_url, img_url_l, category, ] tmp_response.append(item) # tmp_response.sort(key=lambda obj: obj[1].lower().startswith(q),reverse=True) # t = time.strftime( ISOTIMEFORMAT, time.localtime() ) # handle.write('End of all:%s\n'%t) response = ["|".join(r) for r in tmp_response] response_txt = '\n'.join(response) return HttpResponse(response_txt, mimetype="text/plain")