def search(request): ''' Search view. Scans request for q (GET case) or qarea (POST case) and searches for corresponding instances in all subapps matching the query If txt is send in a GET it will display results in txt and not in html format @type request: HTTPRequest @param request: Django HTTPRequest object @rtype: HTTPResponse @return: HTTPResponse object rendering corresponding HTML ''' if u'txt' in request.GET: template = 'results.txt' content_type = 'text/plain' else: template = 'results.html' content_type = 'text/html' if u'q' in request.GET: key = request.GET['q'] elif u'qarea' in request.POST: key = projectwide_functions.get_search_terms(request.POST['qarea']) else: key = None results = { 'hwdoc': None, 'puppet': None, 'updates': None, } results['puppet'] = puppet_functions.search(key).select_related() results['hwdoc'] = hwdoc_functions.search(key).select_related( 'servermanagement', 'rack', 'model', 'model__vendor', 'allocation') results['hwdoc'] = hwdoc_functions.populate_tickets(results['hwdoc']) results['hwdoc'] = hwdoc_functions.populate_hostnames(results['hwdoc']) try: return render(request, template, { 'results': results, }, content_type=content_type) except TemplateSyntaxError as e: if re.search('too many SQL variables', e.message): return render(request, 'error.html', content_type=content_type)
def suggest(request): ''' opensearch suggestions view. Returns JSON @type request: HTTPRequest @param request: Django HTTPRequest object @rtype: HTTPResponse @return: HTTPResponse object rendering corresponding JSON ''' if u'q' in request.GET: key = request.GET['q'] else: key = None results = { 'hwdoc': dict(), 'puppet': dict(), } k = { 'hwdoc': 'serial', 'puppet': 'value', } puppet = puppet_functions.search(key).annotate(Count('value')) hwdoc = hwdoc_functions.search(key).annotate(Count('serial')) for i,v in k.items(): try: results[i]['results'] = locals()[i].values_list('%s' % v, flat=True) results[i]['count'] = locals()[i].values_list('%s__count' % v, flat=True) except (DatabaseError, FieldError): results[i]['results'] = list() results[i]['count'] = list() resp = [key, [], []] for i in results.keys(): resp[1] = resp[1] + list(results[i]['results']) resp[2] = resp[2] + list(results[i]['count']) response = simplejson.dumps(resp) return HttpResponse(response, mimetype = 'application/x-suggestions+json')