# -*- coding: utf-8 -*- vim:encoding=utf-8:
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab

# Copyright © 2010-2012 Greek Research and Technology Network (GRNET S.A.)
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
# TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THIS SOFTWARE.

from servermon.hwdoc.models import Equipment, ServerManagement
from django.db.models import Q
from django.template import RequestContext
from django.shortcuts import render_to_response

def __search(key):
    if key is None:
        return None
    if key.upper().replace('R','').replace('U','').isdigit():
        rackunit = key.upper().replace('R','').replace('U','')
        if len(rackunit) < 3:
            result=Equipment.objects.filter(
                                        Q(rack=rackunit)|
                                        Q(unit=rackunit)
                                        )
        else:
            result=Equipment.objects.filter(rack=rackunit[0:2], unit=rackunit[2:4])
    else:
        result=Equipment.objects.filter(
                                        Q(serial=key)|
                                        Q(servermanagement__mac__icontains=key)|
                                        Q(servermanagement__hostname__icontains=key)
                                        )
    return result

def search(request):
    if u'key' in request.GET:
        key = request.GET['key']
    else:
        key = None

    return render_to_response('search.txt',
            { 'results': __search(key), },
            mimetype='text/plain',
            context_instance=RequestContext(request))

# -*- coding: utf-8 -*- vim:encoding=utf-8:
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab

# Copyright © 2010-2012 Greek Research and Technology Network (GRNET S.A.)
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
# TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
# USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THIS SOFTWARE.

from hwdoc.models import Equipment, ServerManagement
from django.db.models import Q
from hwdoc import functions
from django.template import RequestContext
from django.shortcuts import render_to_response

def search(request):
    if u'key' in request.GET:
        key = request.GET['key']
    else:
        key = None

    return render_to_response('search.txt',
            { 'results': functions.search(key), },
            mimetype='text/plain',
            context_instance=RequestContext(request))

Use search function in hwdoc/views