<commit_before>import csv

from django.core.exceptions import ValidationError
from django.core.paginator import Paginator, InvalidPage, EmptyPage

from django.db.models import Q
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import  redirect, get_object_or_404
from django.template import RequestContext
from django.template.loader import render_to_string
try:
    import json
except:
    from django.utils import simplejson as json
import _mysql_exceptions

import models
from middleware.restrict_to_remote import allow_anyone,sysadmin_only, LdapGroupRequired

import re
from django.test.client import Client
from jinja2.filters import contextfilter
from django.utils import translation
from libs.jinja import render_to_response as render_to_response
from jingo import render
from django.views.decorators.csrf import csrf_exempt
from Rack import Rack
from MozInvAuthorization.KeyValueACL import KeyValueACL 
# Source: http://nedbatchelder.com/blog/200712/human_sorting.html
# Author: <NAME>
def tryint(s):
    try:
        return int(s)
    except:
        return s

def alphanum_key(s):
    """ Turn a string into a list of string and number chunks.
        "z23a" -> ["z", 23, "a"]
    """
    return [ tryint(c) for c in re.split('([0-9]+)', s) ]

def sort_nicely(l):
    """ Sort the given list in the way that humans expect.
    """
    l.sort(key=alphanum_key)
def parse_title_num(title):
   val = 0
   try:
      val = int(title.rsplit('#')[-1])
   except ValueError:
      pass
   return val

def check_dupe_nic(request,system_id,adapter_number):
    try:
        system = models.System.objects.get(id=system_id)
        found = system.check_for_adapter(adapter_number)
    except:
        pass
    return HttpResponse(found)
def check_dupe_nic_name(requessdft,system_id,adapter_name):
    try:
        system = models.System.objects.get(id=system_id)
        found = system.check_for_adapter_name(adapter_name)
    except:
        pass
    return HttpResponse(found)
@allow_anyone
def system_rack_elevation(request, rack_id):
    r = Rack(rack_id)
    data  = {
            'rack_ru': r.ru,
            'ethernet_patch_panels_24': r.ethernet_patch_panel_24,
            'ethernet_patch_panels_48': r.ethernet_patch_panel_48,
            'systems': r.systems,
    }
    data = json.dumps(data)
    return render_to_response('systems/rack_elevation.html', {
        'data':data,
        },
        RequestContext(request))

@allow_anyone
def system_auto_complete_ajax(request):
    query = request.GET['query']
    system_list = models.System.objects.filter(hostname__icontains=query)
    hostname_list = [system.hostname for system in system_list]
    id_list = [system.id for system in system_list]
    ret_dict = {}
    ret_dict['query'] = query
    ret_dict['suggestions'] = hostname_list
    ret_dict['data'] = id_list
    return HttpResponse(json.dumps(ret_dict))

@allow_anyone
def list_all_systems_ajax(request):
#iSortCol_0 = which column is sorted
#sSortDir_0 = which direction   
    
    cols = ['hostname','serial','asset_tag','server_model','system_rack', 'oob_ip', 'system_status']
    sort_col = cols[0]
    if 'iSortCol_0' in request.GET:
        sort_col = cols[int(request.GET['iSortCol_0'])]

    sort_dir = 'asc'
    if 'sSortDir_0' in request.GET:
        sort_dir = request.GET['sSortDir_0']


    if 'sEcho' in request.GET:
        sEcho = request.GET['sEcho']

    if 'sSearch' in request.GET and request.GET['sSearch'] > '':
        search_term = request.GET['sSearch']
    else:
        search_term = None

    if 'iDisplayLength' in request.GET and request.GET['iDisplayLength'] > '':
        iDisplayLength = request.GET['iDisplayLength']
    else:
        iDisplayLength = 100

    if 'iDisplayStart' in request.GET and request.GET['iDisplayStart'] > '':
        iDisplayStart = request.GET['iDisplayStart']
    else:
        iDisplayStart = 0

    if search_term is None:
        end_display = int(iDisplayStart) + int(iDisplayLength)
        system_count = models.System.objects.all().count()
        systems = models.System.objects.all()[iDisplayStart:end_display]
        the_data = build_json(request, systems, sEcho, system_count, iDisplayLength, sort_col, sort_dir)

    if search_term is not None and len(search_term) > 0:
        if search_term.startswith('/') and len(search_term) > 1:
            try:
                search_term = search_term[1:]
                search_q = Q(hostname__regex=search_term)
            except:
                search_q = Q(hostname__icontains=search_term)
        else:
            search_q = Q(hostname__icontains=search_term)
        search_q |= Q(serial__contains=search_term)
        search_q |= Q(notes__contains=search_term)
        search_q |= Q(asset_tag=search_term)
        search_q |= Q(oob_ip__contains=search_term)
        search_q |= Q(keyvalue__value__contains=search_term)
        try:
            total_count = models.System.with_related.filter(search_q).distinct('hostname').count()<commit_msg>Made system search case insensitive. Resolved issue #100<commit_after>import csv

from django.core.exceptions import ValidationError
from django.core.paginator import Paginator, InvalidPage, EmptyPage

from django.db.models import Q
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import  redirect, get_object_or_404
from django.template import RequestContext
from django.template.loader import render_to_string
try:
    import json
except:
    from django.utils import simplejson as json
import _mysql_exceptions

import models
from middleware.restrict_to_remote import allow_anyone,sysadmin_only, LdapGroupRequired

import re
from django.test.client import Client
from jinja2.filters import contextfilter
from django.utils import translation
from libs.jinja import render_to_response as render_to_response
from jingo import render
from django.views.decorators.csrf import csrf_exempt
from Rack import Rack
from MozInvAuthorization.KeyValueACL import KeyValueACL 
# Source: http://nedbatchelder.com/blog/200712/human_sorting.html
# Author: <NAME>
def tryint(s):
    try:
        return int(s)
    except:
        return s

def alphanum_key(s):
    """ Turn a string into a list of string and number chunks.
        "z23a" -> ["z", 23, "a"]
    """
    return [ tryint(c) for c in re.split('([0-9]+)', s) ]

def sort_nicely(l):
    """ Sort the given list in the way that humans expect.
    """
    l.sort(key=alphanum_key)
def parse_title_num(title):
   val = 0
   try:
      val = int(title.rsplit('#')[-1])
   except ValueError:
      pass
   return val

def check_dupe_nic(request,system_id,adapter_number):
    try:
        system = models.System.objects.get(id=system_id)
        found = system.check_for_adapter(adapter_number)
    except:
        pass
    return HttpResponse(found)
def check_dupe_nic_name(requessdft,system_id,adapter_name):
    try:
        system = models.System.objects.get(id=system_id)
        found = system.check_for_adapter_name(adapter_name)
    except:
        pass
    return HttpResponse(found)
@allow_anyone
def system_rack_elevation(request, rack_id):
    r = Rack(rack_id)
    data  = {
            'rack_ru': r.ru,
            'ethernet_patch_panels_24': r.ethernet_patch_panel_24,
            'ethernet_patch_panels_48': r.ethernet_patch_panel_48,
            'systems': r.systems,
    }
    data = json.dumps(data)
    return render_to_response('systems/rack_elevation.html', {
        'data':data,
        },
        RequestContext(request))

@allow_anyone
def system_auto_complete_ajax(request):
    query = request.GET['query']
    system_list = models.System.objects.filter(hostname__icontains=query)
    hostname_list = [system.hostname for system in system_list]
    id_list = [system.id for system in system_list]
    ret_dict = {}
    ret_dict['query'] = query
    ret_dict['suggestions'] = hostname_list
    ret_dict['data'] = id_list
    return HttpResponse(json.dumps(ret_dict))

@allow_anyone
def list_all_systems_ajax(request):
#iSortCol_0 = which column is sorted
#sSortDir_0 = which direction   
    
    cols = ['hostname','serial','asset_tag','server_model','system_rack', 'oob_ip', 'system_status']
    sort_col = cols[0]
    if 'iSortCol_0' in request.GET:
        sort_col = cols[int(request.GET['iSortCol_0'])]

    sort_dir = 'asc'
    if 'sSortDir_0' in request.GET:
        sort_dir = request.GET['sSortDir_0']


    if 'sEcho' in request.GET:
        sEcho = request.GET['sEcho']

    if 'sSearch' in request.GET and request.GET['sSearch'] > '':
        search_term = request.GET['sSearch']
    else:
        search_term = None

    if 'iDisplayLength' in request.GET and request.GET['iDisplayLength'] > '':
        iDisplayLength = request.GET['iDisplayLength']
    else:
        iDisplayLength = 100

    if 'iDisplayStart' in request.GET and request.GET['iDisplayStart'] > '':
        iDisplayStart = request.GET['iDisplayStart']
    else:
        iDisplayStart = 0

    if search_term is None:
        end_display = int(iDisplayStart) + int(iDisplayLength)
        system_count = models.System.objects.all().count()
        systems = models.System.objects.all()[iDisplayStart:end_display]
        the_data = build_json(request, systems, sEcho, system_count, iDisplayLength, sort_col, sort_dir)

    if search_term is not None and len(search_term) > 0:
        if search_term.startswith('/') and len(search_term) > 1:
            try:
                search_term = search_term[1:]
                search_q = Q(hostname__regex=search_term)
            except:
                search_q = Q(hostname__icontains=search_term)
        else:
            search_q = Q(hostname__icontains=search_term)
        search_q |= Q(serial__icontains=search_term)
        search_q |= Q(notes__icontains=search_term)
        search_q |= Q(asset_tag=search_term)
        search_q |= Q(oob_ip__icontains=search_term)
        search_q |= Q(keyvalue__value__icontains=search_term)
        try:
            total_count = models.System.with_related.filter(search_q).distinct('hostname').count()
{'fraction_non_alphanumeric': 0.09939609939609939, 'fraction_numerical': 0.008217008217008217, 'mean_word_length': 3.4210065645514223, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4845978', 'n_tokens_mistral': 3299, 'n_tokens_neox': 3212, 'n_words': 700}