Ejemplo n.º 1
0
def handle(self, *args, **options):
    """
    Handle command
    """

    if args is None or len(args) != 1:
        raise CommandError(_("You must supply a key"))
    key = args[0]

    es = search(key)
    if es.count() == 0:
        print _("No Equipment found")
        return

    for e in es:
        try:
            e.servermanagement
        except ServerManagement.DoesNotExist:
            continue
        if int(options["verbosity"]) > 0:
            print e
        opts = options.copy()
        for term in ["username", "password", "command"]:
            if term in opts:
                opts.pop(term)
        command = getattr(e.servermanagement, options["command"])
        result = command(options["username"], options["password"], **opts)
        # TODO: Figure out what to do with this
        if int(options["verbosity"]) > 1:
            print result
    return
Ejemplo n.º 2
0
def handle(self, *args, **options):
    '''
    Handle command
    '''

    if args is None or len(args) != 1:
        raise CommandError(_('You must supply a key'))
    key = args[0]

    es = search(key)
    if es.count() == 0:
        print _('No Equipment found')
        return

    for e in es:
        try:
            e.servermanagement
        except ServerManagement.DoesNotExist:
            continue
        if int(options['verbosity']) > 0:
            print e
        opts = options.copy()
        for term in ['username', 'password', 'command']:
            if term in opts:
                opts.pop(term)
        command = getattr(e.servermanagement, options['command'])
        result = command(options['username'], options['password'], **opts)
        # TODO: Figure out what to do with this
        if int(options['verbosity']) > 1:
            print result
    return
Ejemplo n.º 3
0
    def test_free_text_search(self):
        text=u'''
        This is a text that is not going to make any sense apart from containing
        a hostname for a server (aka example.com) and a rackunit aka R10U22
        '''

        tokens = get_search_terms(text)
        self.assertNotEqual(search(tokens).count(), 0)
Ejemplo n.º 4
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 sent in a GET it will display results in txt and not in html
    format
    If csv is sent in a GET it will display results in text format separated by comma
    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'
    elif u'csv' in request.GET:
        template = 'results.csv'
        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_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)
Ejemplo n.º 5
0
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})
Ejemplo n.º 6
0
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})
Ejemplo n.º 7
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 sent in a GET it will display results in txt and not in html
    format
    If csv is sent in a GET it will display results in text format separated by comma
    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'
    elif u'csv' in request.GET:
        template = 'results.csv'
        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',  # noqa
                        'model__vendor', 'allocation')

    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)
Ejemplo n.º 8
0
    def handle(self, *args, **options):
        '''
        Handle command
        '''

        if args is None or len(args) != 1:
            raise CommandError(_('You must supply a key'))

        try:
            key = args[0]
        except IndexError:
            print(_('Error in usage. See help'))
            sys.exit(1)

        es = search(key)
        populate_tickets(es, options['closed'])
Ejemplo n.º 9
0
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
    })
Ejemplo n.º 10
0
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')
Ejemplo n.º 11
0
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 = json.dumps(resp)

    return HttpResponse(response, content_type='application/x-suggestions+json')
Ejemplo n.º 12
0
 def test_search_serial(self):
     self.assertEqual(search(self.server1.serial)[0].serial, self.server1.serial)
     self.assertEqual(search(self.server2.serial)[0].serial, self.server2.serial)
Ejemplo n.º 13
0
 def test_search_empty(self):
     self.assertFalse(search(''))
Ejemplo n.º 14
0
 def test_search_rack_heuristic(self):
     self.assertEqual(search(str(self.server3.rack.name + self.server3.unit)).count(), 1)
Ejemplo n.º 15
0
 def test_search_rack_heuristic(self):
     self.assertEqual(
         search('%sU%s' %
                (self.server3.rack.name, self.server3.unit)).count(), 1)
Ejemplo n.º 16
0
 def test_search_serial(self):
     self.assertEqual(
         search(self.server1.serial)[0].serial, self.server1.serial)
     self.assertEqual(
         search(self.server2.serial)[0].serial, self.server2.serial)
Ejemplo n.º 17
0
 def test_search_rack(self):
     self.assertEqual(search(str(self.server1.rack.name)).count(), 2)
Ejemplo n.º 18
0
 def test_search_all(self):
     self.assertEqual(search('ALL_EQS').count(), 3)
Ejemplo n.º 19
0
 def test_search_empty(self):
     self.assertFalse(search(''))
Ejemplo n.º 20
0
 def test_search_rack(self):
     self.assertEqual(search(str(self.server1.rack.pk)).count(), 2)
Ejemplo n.º 21
0
 def test_populate_tickets(self):
     self.assertEqual(populate_tickets(search(str(self.server2.rack.pk))).count(), 2)
Ejemplo n.º 22
0
 def test_search_all(self):
     self.assertEqual(search('ALL_EQS').count(), 3)
Ejemplo n.º 23
0
 def test_search_rack_heuristic(self):
     self.assertEqual(search('%sU%s' % (self.server3.rack.name, self.server3.unit)).count(), 1)