<commit_before>import csv
import operator

from django.template import RequestContext
from django.forms.extras.widgets import SelectDateWidget
from django.db import connection
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.utils import simplejson as json
from django.http import HttpResponse, HttpResponseRedirect
from django.core.mail import send_mail

import forms
import models
from systems import models as system_models
from datetime import datetime, timedelta
from libs import ldap_lib
import settings
from settings.local import USER_SYSTEM_ALLOWED_DELETE, FROM_EMAIL_ADDRESS, UNAUTHORIZED_EMAIL_ADDRESS
from django.core.exceptions import PermissionDenied
from django.shortcuts import render_to_response, redirect, get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from MozInvAuthorization.UnmanagedSystemACL import UnmanagedSystemACL
@csrf_exempt
def owners_quicksearch_ajax(request):
    """Returns systems sort table"""
    search = request.POST['quicksearch']
    filters = [Q(**{"%s__icontains" % t: search})
                    for t in models.Owner.search_fields]

    owners = models.Owner.objects.filter(
                reduce(operator.or_, filters))

    return render_to_response('user_systems/owners_quicksearch.html', {
            'owners': owners,
           },
           RequestContext(request))
@csrf_exempt
def license_edit(request, object_id):
    license = get_object_or_404(models.UserLicense, pk=object_id)
    if request.method == 'POST':
        form = forms.UserLicenseForm(request.POST, instance=license)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/user_systems/licenses/')
    else:
        form = forms.UserLicenseForm(instance=license)

    return render_to_response('user_systems/userlicense_form.html', {
            'form': form,
           },
           RequestContext(request))
def owner_list(request):
    owners = models.Owner.objects.select_related('user_location').all()
    upgradeable_users = models.Owner.objects.filter(unmanagedsystem__date_purchased__lt=datetime.now() - timedelta(days=730)).distinct().count()
    return render_to_response('user_systems/owner_list.html', {
            'owner_list': owners,
            'upgradeable_users':upgradeable_users,
           },
           RequestContext(request))
def owner_show(request, object_id):
    owner = get_object_or_404(models.Owner, pk=object_id)

    return render_to_response('user_systems/owner_detail.html', {
            'owner': owner,
           },
           RequestContext(request))

def owner_delete(request, object_id):
    owner = get_object_or_404(models.Owner, pk=object_id)
    if request.method == "POST":
        owner.delete()
        return HttpResponseRedirect('/user_systems/owners/')
    else:
        return render_to_response('user_systems/owner_confirm_delete.html', {
                'owner': owner,
            },
            RequestContext(request))
@csrf_exempt
def owner_edit(request, object_id):
    owner = get_object_or_404(models.Owner, pk=object_id)
    initial = {}
    if request.method == 'POST':
        form = forms.OwnerForm(request.POST, instance=owner)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/user_systems/owners/')
    else:
        form = forms.OwnerForm(instance=owner)

    return render_to_response('user_systems/owner_form.html', {
            'form': form,
           },
           RequestContext(request))
def owner_create(request):
    initial = {}
    if request.method == 'POST':
        form = forms.OwnerForm(request.POST, initial=initial)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/user_systems/owners/')
    else:
        form = forms.OwnerForm(initial=initial)

    return render_to_response('user_systems/owner_form.html', {
            'form': form,
           },
           RequestContext(request))
def license_new(request):
    initial = {}
    if request.method == 'POST':
        form = forms.UserLicenseForm(request.POST, initial=initial)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/user_systems/licenses/')
    else:
        form = forms.UserLicenseForm(initial=initial)

    return render_to_response('user_systems/userlicense_form.html', {
            'form': form,
           },
           RequestContext(request))
@csrf_exempt
def license_quicksearch_ajax(request):
    """Returns systems sort table"""
    search = request.POST['quicksearch']
    filters = [Q(**{"%s__icontains" % t: search})
                    for t in models.UserLicense.search_fields]

    licenses = models.UserLicense.objects.filter(
                reduce(operator.or_, filters))

    return render_to_response('user_systems/license_quicksearch.html', {
            'licenses': licenses,
           },
           RequestContext(request))
@csrf_exempt
def user_system_quicksearch_ajax(request):
    """Returns systems sort table"""
    from settings import BUG_URL as BUG_URL
    search = request.POST['quicksearch']
    print request.POST
    filters = [Q(**{"%s__icontains" % t: search})
                    for t in models.UnmanagedSystem.search_fields]

    systems = models.UnmanagedSystem.objects.filter(
                reduce(operator.or_, filters))

    return render_to_response('user_systems/quicksearch.html', {
            'systems': systems,
            'BUG_URL': BUG_URL,
           },
           RequestContext(request))

@csrf_exempt
def user_system_view(request, template, data, instance=None):
    if request.method == 'POST':
        post_data = request.POST.copy()

        owner, c = models.Owner.objects.get_or_create(
            name=request.POST['owner_name'])

        post_data['owner'] = owner.id

        try:
            os, c = models.OperatingSystem.objects.get_or_create(
                    name=request.POST['js_os_name'],
                    version=request.POST['js_os_version'])
            post_data['operating_system'] = os.id
        except KeyError:
            pass

        try:
            server_model, c = models.ServerModel.objects.get_or_create(
                            vendor=request.POST['js_server_model_vendor'],
                            model=request.POST['js_server_model_model'])
            post_data['server_model'] = server_model.id
        except KeyError:
            pass

        if instance:
            old_owner = instance.owner
            old_serial = instance.serial
            old_asset_tag = instance.asset_tag
            old_notes = instance.notes
        else:
            old_owner = None
            old_serial = None
            old_asset_tag = None
            old_notes = None

        form = forms.UserSystemForm(post_data, instance=instance)
        if form.is_valid():
            saved_instance = form.save()
            if not instance or old_notes != saved_instance.notes:
                if old_notes:
                    models.History(
                        change="Notes changed from %s" % old_notes,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                if saved_instance.notes:
                    models.History(
                        change="Notes changed to %s" % saved_instance.notes,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                else:
                    models.History(
                        change="System has no Notes",
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
            if not instance or old_asset_tag != saved_instance.asset_tag:
                if old_asset_tag:
                    models.History(
                        change="Asset Tag changed from %s" % old_asset_tag,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                if saved_instance.asset_tag:
                    models.History(
                        change="Asset Tag changed to %s" % saved_instance.asset_tag,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                else:
                    models.History(
                        change="System has no Asset Tag",
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
            if not instance or old_serial != saved_instance.serial:
                if old_serial:
                    models.History(
                        change="Serial changed from %s" % old_serial,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                if saved_instance.serial:
                    models.History(
                        change="Serial changed to %s" % saved_instance.serial,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                else:
                    models.History(
                        change="System has no serial",
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
            if not instance or old_owner != saved_instance.owner:
                if old_owner:
                    models.History(
                        change="Owner changed from %s" % old_owner,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                if saved_instance.owner:
                    models.History(
                        change="Owner changed to %s" % saved_instance.owner,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                else:
                    models.History(
                        change="System has no owner",
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
            return redirect('user-system-list')
    else:
        form = forms.UserSystemForm(instance=instance)

    data['form'] = form
    if settings.USE_LDAP:
        the_owner_list = ldap_lib.get_all_names()
    else:
        the_owner_list = []
    the_owner_list.append("STOCK")
    the_owner_list.append("STOCK-SFO")
    the_owner_list.append("STOCK-MTV")
    the_owner_list.append("STOCK-TOR")
    the_owner_list.append("STOCK-LON")
    data['owner_json'] = json.dumps(the_owner_list)
<commit_msg>Added more static users for bug 753543 resolves Issue #90<commit_after>import csv
import operator

from django.template import RequestContext
from django.forms.extras.widgets import SelectDateWidget
from django.db import connection
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.utils import simplejson as json
from django.http import HttpResponse, HttpResponseRedirect
from django.core.mail import send_mail

import forms
import models
from systems import models as system_models
from datetime import datetime, timedelta
from libs import ldap_lib
import settings
from settings.local import USER_SYSTEM_ALLOWED_DELETE, FROM_EMAIL_ADDRESS, UNAUTHORIZED_EMAIL_ADDRESS
from django.core.exceptions import PermissionDenied
from django.shortcuts import render_to_response, redirect, get_object_or_404
from django.views.decorators.csrf import csrf_exempt
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from MozInvAuthorization.UnmanagedSystemACL import UnmanagedSystemACL
@csrf_exempt
def owners_quicksearch_ajax(request):
    """Returns systems sort table"""
    search = request.POST['quicksearch']
    filters = [Q(**{"%s__icontains" % t: search})
                    for t in models.Owner.search_fields]

    owners = models.Owner.objects.filter(
                reduce(operator.or_, filters))

    return render_to_response('user_systems/owners_quicksearch.html', {
            'owners': owners,
           },
           RequestContext(request))
@csrf_exempt
def license_edit(request, object_id):
    license = get_object_or_404(models.UserLicense, pk=object_id)
    if request.method == 'POST':
        form = forms.UserLicenseForm(request.POST, instance=license)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/user_systems/licenses/')
    else:
        form = forms.UserLicenseForm(instance=license)

    return render_to_response('user_systems/userlicense_form.html', {
            'form': form,
           },
           RequestContext(request))
def owner_list(request):
    owners = models.Owner.objects.select_related('user_location').all()
    upgradeable_users = models.Owner.objects.filter(unmanagedsystem__date_purchased__lt=datetime.now() - timedelta(days=730)).distinct().count()
    return render_to_response('user_systems/owner_list.html', {
            'owner_list': owners,
            'upgradeable_users':upgradeable_users,
           },
           RequestContext(request))
def owner_show(request, object_id):
    owner = get_object_or_404(models.Owner, pk=object_id)

    return render_to_response('user_systems/owner_detail.html', {
            'owner': owner,
           },
           RequestContext(request))

def owner_delete(request, object_id):
    owner = get_object_or_404(models.Owner, pk=object_id)
    if request.method == "POST":
        owner.delete()
        return HttpResponseRedirect('/user_systems/owners/')
    else:
        return render_to_response('user_systems/owner_confirm_delete.html', {
                'owner': owner,
            },
            RequestContext(request))
@csrf_exempt
def owner_edit(request, object_id):
    owner = get_object_or_404(models.Owner, pk=object_id)
    initial = {}
    if request.method == 'POST':
        form = forms.OwnerForm(request.POST, instance=owner)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/user_systems/owners/')
    else:
        form = forms.OwnerForm(instance=owner)

    return render_to_response('user_systems/owner_form.html', {
            'form': form,
           },
           RequestContext(request))
def owner_create(request):
    initial = {}
    if request.method == 'POST':
        form = forms.OwnerForm(request.POST, initial=initial)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/user_systems/owners/')
    else:
        form = forms.OwnerForm(initial=initial)

    return render_to_response('user_systems/owner_form.html', {
            'form': form,
           },
           RequestContext(request))
def license_new(request):
    initial = {}
    if request.method == 'POST':
        form = forms.UserLicenseForm(request.POST, initial=initial)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/user_systems/licenses/')
    else:
        form = forms.UserLicenseForm(initial=initial)

    return render_to_response('user_systems/userlicense_form.html', {
            'form': form,
           },
           RequestContext(request))
@csrf_exempt
def license_quicksearch_ajax(request):
    """Returns systems sort table"""
    search = request.POST['quicksearch']
    filters = [Q(**{"%s__icontains" % t: search})
                    for t in models.UserLicense.search_fields]

    licenses = models.UserLicense.objects.filter(
                reduce(operator.or_, filters))

    return render_to_response('user_systems/license_quicksearch.html', {
            'licenses': licenses,
           },
           RequestContext(request))
@csrf_exempt
def user_system_quicksearch_ajax(request):
    """Returns systems sort table"""
    from settings import BUG_URL as BUG_URL
    search = request.POST['quicksearch']
    print request.POST
    filters = [Q(**{"%s__icontains" % t: search})
                    for t in models.UnmanagedSystem.search_fields]

    systems = models.UnmanagedSystem.objects.filter(
                reduce(operator.or_, filters))

    return render_to_response('user_systems/quicksearch.html', {
            'systems': systems,
            'BUG_URL': BUG_URL,
           },
           RequestContext(request))

@csrf_exempt
def user_system_view(request, template, data, instance=None):
    if request.method == 'POST':
        post_data = request.POST.copy()

        owner, c = models.Owner.objects.get_or_create(
            name=request.POST['owner_name'])

        post_data['owner'] = owner.id

        try:
            os, c = models.OperatingSystem.objects.get_or_create(
                    name=request.POST['js_os_name'],
                    version=request.POST['js_os_version'])
            post_data['operating_system'] = os.id
        except KeyError:
            pass

        try:
            server_model, c = models.ServerModel.objects.get_or_create(
                            vendor=request.POST['js_server_model_vendor'],
                            model=request.POST['js_server_model_model'])
            post_data['server_model'] = server_model.id
        except KeyError:
            pass

        if instance:
            old_owner = instance.owner
            old_serial = instance.serial
            old_asset_tag = instance.asset_tag
            old_notes = instance.notes
        else:
            old_owner = None
            old_serial = None
            old_asset_tag = None
            old_notes = None

        form = forms.UserSystemForm(post_data, instance=instance)
        if form.is_valid():
            saved_instance = form.save()
            if not instance or old_notes != saved_instance.notes:
                if old_notes:
                    models.History(
                        change="Notes changed from %s" % old_notes,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                if saved_instance.notes:
                    models.History(
                        change="Notes changed to %s" % saved_instance.notes,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                else:
                    models.History(
                        change="System has no Notes",
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
            if not instance or old_asset_tag != saved_instance.asset_tag:
                if old_asset_tag:
                    models.History(
                        change="Asset Tag changed from %s" % old_asset_tag,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                if saved_instance.asset_tag:
                    models.History(
                        change="Asset Tag changed to %s" % saved_instance.asset_tag,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                else:
                    models.History(
                        change="System has no Asset Tag",
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
            if not instance or old_serial != saved_instance.serial:
                if old_serial:
                    models.History(
                        change="Serial changed from %s" % old_serial,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                if saved_instance.serial:
                    models.History(
                        change="Serial changed to %s" % saved_instance.serial,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                else:
                    models.History(
                        change="System has no serial",
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
            if not instance or old_owner != saved_instance.owner:
                if old_owner:
                    models.History(
                        change="Owner changed from %s" % old_owner,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                if saved_instance.owner:
                    models.History(
                        change="Owner changed to %s" % saved_instance.owner,
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
                else:
                    models.History(
                        change="System has no owner",
                        changed_by=get_changed_by(request),
                        system=saved_instance).save()
            return redirect('user-system-list')
    else:
        form = forms.UserSystemForm(instance=instance)

    data['form'] = form
    if settings.USE_LDAP:
        the_owner_list = ldap_lib.get_all_names()
    else:
        the_owner_list = []
    the_owner_list.append("STOCK")
    the_owner_list.append("STOCK-SFO")
    the_owner_list.append("STOCK-MTV")
    the_owner_list.append("STOCK-TOR")
    the_owner_list.append("STOCK-LON")
    the_owner_list.append("STOCK-LON")
    the_owner_list.append("desktop-mtv1")
    the_owner_list.append("desktop-sfo1")
    the_owner_list.append("desktop-tor1")
    the_owner_list.append("desktop-lon1")
    the_owner_list.append("desktop-par1")
    the_owner_list.append("desktop-yvr1")
    data['owner_json'] = json.dumps(the_owner_list)

{'fraction_non_alphanumeric': 0.09212990453569635, 'fraction_numerical': 0.0022838350157584615, 'mean_word_length': 2.085835095137421, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '6680459', 'n_tokens_mistral': 5950, 'n_tokens_neox': 5676, 'n_words': 1098}