+def search(request):
+ tags = request.GET.get('tags', '')
+ prefix = request.GET.get('q', '')
+
+ try:
+ tag_list = models.Tag.get_tag_list(tags)
+ except:
+ tag_list = []
+
+ try:
+ result = find_best_matches(prefix, request.user)
+ except ValueError:
+ return render_to_response('catalogue/search_too_short.html', {'tags':tag_list, 'prefix':prefix},
+ context_instance=RequestContext(request))
+
+ if len(result) == 1:
+ return HttpResponseRedirect(_get_result_link(result[0], tag_list))
+ elif len(result) > 1:
+ return render_to_response('catalogue/search_multiple_hits.html',
+ {'tags':tag_list, 'prefix':prefix, 'results':((x, _get_result_link(x, tag_list), _get_result_type(x)) for x in result)},
+ context_instance=RequestContext(request))
+ else:
+ form = PublishingSuggestForm(initial={"books": prefix + ", "})
+ return render_to_response('catalogue/search_no_hits.html',
+ {'tags':tag_list, 'prefix':prefix, "pubsuggest_form": form},
+ context_instance=RequestContext(request))
+
+
+def tags_starting_with(request):
+ prefix = request.GET.get('q', '')
+ # Prefix must have at least 2 characters
+ if len(prefix) < 2:
+ return HttpResponse('')
+ tags_list = []
+ result = ""
+ for tag in _tags_starting_with(prefix, request.user):
+ if not tag.name in tags_list:
+ result += "\n" + tag.name
+ tags_list.append(tag.name)
+ return HttpResponse(result)
+
+def json_tags_starting_with(request, callback=None):
+ # Callback for JSONP
+ prefix = request.GET.get('q', '')
+ callback = request.GET.get('callback', '')
+ # Prefix must have at least 2 characters
+ if len(prefix) < 2:
+ return HttpResponse('')
+ tags_list = []
+ for tag in _tags_starting_with(prefix, request.user):
+ if not tag.name in tags_list:
+ tags_list.append(tag.name)
+ if request.GET.get('mozhint', ''):
+ result = [prefix, tags_list]
+ else:
+ result = {"matches": tags_list}
+ return JSONResponse(result, callback)