def datacenter(request, datacenter_id): ''' Rackrow view. It should display all non-authenticated user viewable data @type request: HTTPRequest @param request: Django HTTPRequest object @rtype: HTTPResponse @return: HTTPResponse object rendering corresponding HTML ''' template = 'datacenter.html' datacenter = get_object_or_404(Datacenter, pk=datacenter_id) rackrows = datacenter.rackrow_set.all() for rackrow in rackrows: racks = [str(x).zfill(2) for x in rackrow.rackposition_set.values_list('rack__pk', flat=True)] equipments = functions.search(racks) equipments = equipments.exclude(comments='') if equipments.count() > 0: rackrow.tickets = True return render(request, template, { 'datacenter': datacenter, 'rackrows': rackrows, })
def rack(request, rack_id): ''' Rack view. It should display all non-authenticated user viewable data @type request: HTTPRequest @param request: Django HTTPRequest object @rtype: HTTPResponse @return: HTTPResponse object rendering corresponding HTML ''' template = 'rack.html' rack = get_object_or_404(Rack, pk=rack_id) try: rack.prev = Rack.objects.filter( rackposition__position__lt=rack.rackposition.position, rackposition__rr=rack.rackposition.rr ).order_by('-rackposition__position')[0] except (IndexError, RackPosition.DoesNotExist): rack.prev = None try: rack.next = Rack.objects.filter( rackposition__position__gt=rack.rackposition.position, rackposition__rr=rack.rackposition.rr ).order_by('rackposition__position')[0] except (IndexError, RackPosition.DoesNotExist): rack.next = None equipments = functions.search(str(rack.name)) equipments = functions.populate_tickets(equipments) equipments = functions.populate_hostnames(equipments) equipments = { 'hwdoc': functions.calculate_empty_units(rack, equipments), } return render(request, template, { 'rack': rack, 'equipments': equipments })
def project(request, project_id): ''' Project view. It should display all non-authenticated user viewable data @type request: HTTPRequest @param request: Django HTTPRequest object @rtype: HTTPResponse @return: HTTPResponse object rendering corresponding HTML ''' template = 'project.html' project = get_object_or_404(Project,pk=project_id) equipments = { 'hwdoc': functions.search(project.name) } return render(request, template, { 'project': project, 'equipments': equipments })
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 rack(request, rack_id): ''' Rack view. It should display all non-authenticated user viewable data @type request: HTTPRequest @param request: Django HTTPRequest object @rtype: HTTPResponse @return: HTTPResponse object rendering corresponding HTML ''' template = 'rack.html' rack = get_object_or_404(Rack, pk=rack_id) equipments = functions.search(str(rack.pk)) equipments = functions.populate_tickets(equipments) return render(request, template, { 'rack': rack, 'equipments': equipments })
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')
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 = list(functions.search(key).values_list('serial', flat=True)) # Simple JSON does not handle querysets so we cast to list results = list(results) response = simplejson.dumps([ key, results ]) return HttpResponse(response, mimetype = 'application/x-suggestions+json')
def search(request): ''' Search view. Scans request for q (GET case) or qarea (POST case) and searches for corresponding Equipment instances 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' mimetype = 'text/plain' else: template = 'results.html' mimetype = 'text/html' if u'q' in request.GET: key = request.GET['q'] elif u'qarea' in request.POST: key = functions.get_search_terms(request.POST['qarea']) else: key = None results = functions.search(key).select_related( 'servermanagement', 'rack', 'model', 'model__vendor', 'allocation') results = functions.populate_tickets(results) return render(request, template, { 'results': results, }, mimetype=mimetype)