Esempio n. 1
0
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 })
Esempio n. 2
0
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)