<s> import csv <EOL> import operator <EOL> from django . template import RequestContext <EOL> from django . forms . extras . widgets import SelectDateWidget <EOL> from django . db import connection <EOL> from django . core . urlresolvers import reverse <EOL> from django . db . models import Q <EOL> from django . utils import simplejson as json <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . core . mail import send_mail <EOL> import forms <EOL> import models <EOL> from systems import models as system_models <EOL> from datetime import datetime , timedelta <EOL> from libs import ldap_lib <EOL> import settings <EOL> from settings . local import USER_SYSTEM_ALLOWED_DELETE , FROM_EMAIL_ADDRESS , UNAUTHORIZED_EMAIL_ADDRESS <EOL> from settings import BUG_URL as BUG_URL <EOL> from django . core . exceptions import PermissionDenied <EOL> from django . shortcuts import redirect , get_object_or_404 , render <EOL> from libs . jinja import render_to_response as render_to_response <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django . core . paginator import Paginator , EmptyPage , PageNotAnInteger <EOL> from MozInvAuthorization . UnmanagedSystemACL import UnmanagedSystemACL <EOL> def license_version_search ( request ) : <EOL> query = request . GET . get ( '<STR_LIT>' ) <EOL> tmp = [ str ( m [ '<STR_LIT:version>' ] ) for m in models . UserLicense . objects . filter ( version__icontains = query ) . values ( '<STR_LIT:version>' ) . distinct ( ) ] <EOL> versions = list ( set ( tmp ) ) <EOL> ret_dict = { } <EOL> ret_dict [ '<STR_LIT>' ] = query <EOL> ret_dict [ '<STR_LIT>' ] = versions <EOL> ret_dict [ '<STR_LIT:data>' ] = versions <EOL> return HttpResponse ( json . dumps ( ret_dict ) ) <EOL> def license_type_search ( request ) : <EOL> query = request . GET . get ( '<STR_LIT>' ) <EOL> types = [ m [ '<STR_LIT>' ] for m in models . UserLicense . objects . filter ( license_type__icontains = query ) . values ( '<STR_LIT>' ) . distinct ( ) ] <EOL> ret_dict = { } <EOL> ret_dict [ '<STR_LIT>' ] = query <EOL> ret_dict [ '<STR_LIT>' ] = types <EOL> ret_dict [ '<STR_LIT:data>' ] = types <EOL> return HttpResponse ( json . dumps ( ret_dict ) ) <EOL> @ csrf_exempt <EOL> def owners_quicksearch_ajax ( request ) : <EOL> """<STR_LIT>""" <EOL> search = request . POST [ '<STR_LIT>' ] <EOL> filters = [ Q ( ** { "<STR_LIT>" % t : search } ) <EOL> for t in models . Owner . search_fields ] <EOL> owners = models . Owner . objects . filter ( <EOL> reduce ( operator . or_ , filters ) ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : owners , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> @ csrf_exempt <EOL> def license_edit ( request , object_id ) : <EOL> license = get_object_or_404 ( models . UserLicense , pk = object_id ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . UserLicenseForm ( request . POST , instance = license ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = forms . UserLicenseForm ( instance = license ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def owner_list ( request ) : <EOL> owners = models . Owner . objects . select_related ( '<STR_LIT>' ) . all ( ) <EOL> upgradeable_users = models . Owner . objects . filter ( unmanagedsystem__date_purchased__lt = datetime . now ( ) - timedelta ( days = <NUM_LIT> ) ) . distinct ( ) . count ( ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : owners , <EOL> '<STR_LIT>' : upgradeable_users , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def owner_show ( request , object_id ) : <EOL> owner = get_object_or_404 ( models . Owner , pk = object_id ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : owner , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def owner_delete ( request , object_id ) : <EOL> owner = get_object_or_404 ( models . Owner , pk = object_id ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> owner . delete ( ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> else : <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : owner , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> @ csrf_exempt <EOL> def owner_edit ( request , object_id ) : <EOL> owner = get_object_or_404 ( models . Owner , pk = object_id ) <EOL> initial = { } <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . OwnerForm ( request . POST , instance = owner ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = forms . OwnerForm ( instance = owner ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def owner_create ( request ) : <EOL> initial = { } <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . OwnerForm ( request . POST , initial = initial ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = forms . OwnerForm ( initial = initial ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def license_new ( request ) : <EOL> initial = { } <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . UserLicenseForm ( request . POST , initial = initial ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return HttpResponseRedirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = forms . UserLicenseForm ( initial = initial ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : form , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def license_quicksearch_ajax ( request ) : <EOL> """<STR_LIT>""" <EOL> search = request . GET . get ( '<STR_LIT>' , None ) <EOL> if search : <EOL> filters = [ Q ( ** { "<STR_LIT>" % t : search } ) <EOL> for t in models . UserLicense . search_fields ] <EOL> licenses = models . UserLicense . objects . filter ( <EOL> reduce ( operator . or_ , filters ) ) <EOL> else : <EOL> licenses = None <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : licenses , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> @ csrf_exempt <EOL> def user_system_quicksearch_ajax ( request ) : <EOL> """<STR_LIT>""" <EOL> search = request . POST [ '<STR_LIT>' ] <EOL> filters = [ <EOL> Q ( ** { "<STR_LIT>" % t : search } ) <EOL> for t in models . UnmanagedSystem . search_fields <EOL> ] <EOL> systems = models . UnmanagedSystem . objects . filter ( <EOL> reduce ( operator . or_ , filters ) <EOL> ) <EOL> for s in systems : <EOL> try : <EOL> s . server_model <EOL> except : <EOL> s . server_model = None <EOL> return render_to_response ( <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : systems , '<STR_LIT>' : BUG_URL } , <EOL> RequestContext ( request ) <EOL> ) <EOL> @ csrf_exempt <EOL> def user_system_view ( request , template , data , instance = None ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> post_data = request . POST . copy ( ) <EOL> owner , c = models . Owner . objects . get_or_create ( <EOL> name = request . POST [ '<STR_LIT>' ] ) <EOL> post_data [ '<STR_LIT>' ] = owner . id <EOL> try : <EOL> os , c = models . OperatingSystem . objects . get_or_create ( <EOL> name = request . POST [ '<STR_LIT>' ] , <EOL> version = request . POST [ '<STR_LIT>' ] ) <EOL> post_data [ '<STR_LIT>' ] = os . id <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> server_model , c = models . ServerModel . objects . get_or_create ( <EOL> vendor = request . POST [ '<STR_LIT>' ] , <EOL> model = request . POST [ '<STR_LIT>' ] ) <EOL> post_data [ '<STR_LIT>' ] = server_model . id <EOL> except KeyError : <EOL> pass <EOL> if instance : <EOL> old_owner = instance . owner <EOL> old_serial = instance . serial <EOL> old_asset_tag = instance . asset_tag <EOL> old_notes = instance . notes <EOL> else : <EOL> old_owner = None <EOL> old_serial = None <EOL> old_asset_tag = None <EOL> old_notes = None <EOL> form = forms . UserSystemForm ( post_data , instance = instance ) <EOL> if form . is_valid ( ) : <EOL> saved_instance = form . save ( ) <EOL> if not instance or old_notes != saved_instance . notes : <EOL> if old_notes : <EOL> models . History ( <EOL> change = "<STR_LIT>" % old_notes , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> if saved_instance . notes : <EOL> models . History ( <EOL> change = "<STR_LIT>" % saved_instance . notes , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> else : <EOL> models . History ( <EOL> change = "<STR_LIT>" , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> if not instance or old_asset_tag != saved_instance . asset_tag : <EOL> if old_asset_tag : <EOL> models . History ( <EOL> change = "<STR_LIT>" % old_asset_tag , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> if saved_instance . asset_tag : <EOL> models . History ( <EOL> change = "<STR_LIT>" % saved_instance . asset_tag , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> else : <EOL> models . History ( <EOL> change = "<STR_LIT>" , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> if not instance or old_serial != saved_instance . serial : <EOL> if old_serial : <EOL> models . History ( <EOL> change = "<STR_LIT>" % old_serial , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> if saved_instance . serial : <EOL> models . History ( <EOL> change = "<STR_LIT>" % saved_instance . serial , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> else : <EOL> models . History ( <EOL> change = "<STR_LIT>" , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> if not instance or old_owner != saved_instance . owner : <EOL> if old_owner : <EOL> models . History ( <EOL> change = "<STR_LIT>" % old_owner , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> if saved_instance . owner : <EOL> models . History ( <EOL> change = "<STR_LIT>" % saved_instance . owner , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> else : <EOL> models . History ( <EOL> change = "<STR_LIT>" , <EOL> changed_by = get_changed_by ( request ) , <EOL> system = saved_instance ) . save ( ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> else : <EOL> form = forms . UserSystemForm ( instance = instance ) <EOL> data [ '<STR_LIT>' ] = form <EOL> if settings . USE_LDAP : <EOL> the_owner_list = ldap_lib . get_all_names ( ) <EOL> else : <EOL> the_owner_list = [ ] <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> the_owner_list . append ( "<STR_LIT>" ) <EOL> data [ '<STR_LIT>' ] = json . dumps ( the_owner_list ) <EOL> return render_to_response ( template , data , RequestContext ( request ) ) <EOL> def get_changed_by ( request ) : <EOL> try : <EOL> remote_user = request . META [ '<STR_LIT>' ] <EOL> except : <EOL> remote_user = None <EOL> return remote_user <EOL> def license_show ( request , object_id ) : <EOL> license = get_object_or_404 ( models . UserLicense , pk = object_id ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : license , <EOL> } , RequestContext ( request ) ) <EOL> def license_index ( request ) : <EOL> from settings import BUG_URL as BUG_URL <EOL> system_list = models . UserLicense . objects . select_related ( '<STR_LIT>' ) . all ( ) <EOL> paginator = Paginator ( system_list , <NUM_LIT> ) <EOL> if '<STR_LIT>' in request . GET : <EOL> page = request . GET . get ( '<STR_LIT>' ) <EOL> else : <EOL> page = <NUM_LIT:1> <EOL> try : <EOL> systems = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> systems = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> systems = paginator . page ( paginator . num_pages ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : systems , <EOL> '<STR_LIT>' : BUG_URL <EOL> } , RequestContext ( request ) ) <EOL> def user_system_index ( request ) : <EOL> from settings import BUG_URL as BUG_URL <EOL> system_list = models . UnmanagedSystem . objects . select_related ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) . order_by ( '<STR_LIT>' ) <EOL> paginator = Paginator ( system_list , <NUM_LIT> ) <EOL> if '<STR_LIT>' in request . GET : <EOL> page = request . GET . get ( '<STR_LIT>' ) <EOL> else : <EOL> page = <NUM_LIT:1> <EOL> try : <EOL> systems = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> systems = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> systems = paginator . page ( paginator . num_pages ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : systems , <EOL> '<STR_LIT>' : BUG_URL <EOL> } , RequestContext ( request ) ) <EOL> def license_delete ( request , object_id ) : <EOL> license = get_object_or_404 ( models . UserLicense , pk = object_id ) <EOL> try : <EOL> license . delete ( ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> except PermissionDenied , e : <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def unmanaged_system_delete ( request , object_id ) : <EOL> user_system = get_object_or_404 ( models . UnmanagedSystem , pk = object_id ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> try : <EOL> acl = UnmanagedSystemACL ( request ) <EOL> acl . check_delete ( ) <EOL> user_system_notes = user_system . notes <EOL> user_system . delete ( ) <EOL> send_mail ( '<STR_LIT>' , '<STR_LIT>' % ( user_system , request . user . username , user_system_notes ) , FROM_EMAIL_ADDRESS , UNAUTHORIZED_EMAIL_ADDRESS , fail_silently = False ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> except PermissionDenied , e : <EOL> send_mail ( '<STR_LIT>' , '<STR_LIT>' % ( user_system , request . user . username ) , FROM_EMAIL_ADDRESS , UNAUTHORIZED_EMAIL_ADDRESS , fail_silently = False ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> else : <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : user_system , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def show_by_model ( request , object_id ) : <EOL> system_list = models . UnmanagedSystem . objects . filter ( server_model = models . ServerModel . objects . get ( id = object_id ) ) <EOL> if '<STR_LIT>' in request . GET : <EOL> paginator = Paginator ( system_list , system_list . count ( ) ) <EOL> else : <EOL> paginator = Paginator ( system_list , <NUM_LIT> ) <EOL> if '<STR_LIT>' in request . GET : <EOL> page = request . GET . get ( '<STR_LIT>' ) <EOL> else : <EOL> page = <NUM_LIT:1> <EOL> try : <EOL> systems = paginator . page ( page ) <EOL> except PageNotAnInteger : <EOL> systems = paginator . page ( <NUM_LIT:1> ) <EOL> except EmptyPage : <EOL> systems = paginator . page ( paginator . num_pages ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : systems , <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def user_system_show ( request , object_id ) : <EOL> system = get_object_or_404 ( models . UnmanagedSystem , id = object_id ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : system , <EOL> '<STR_LIT>' : settings , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def user_system_show_by_asset_tag ( request , id ) : <EOL> system = get_object_or_404 ( models . UnmanagedSystem , asset_tag = id ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : system , <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def user_system_new ( request ) : <EOL> return user_system_view ( <EOL> request , <EOL> '<STR_LIT>' , <EOL> { } ) <EOL> @ csrf_exempt <EOL> def user_system_edit ( request , id ) : <EOL> system = get_object_or_404 ( models . UnmanagedSystem , pk = id ) <EOL> return user_system_view ( <EOL> request , <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT>' : system } , <EOL> system ) <EOL> def user_system_csv ( request ) : <EOL> systems = models . UnmanagedSystem . objects . all ( ) . order_by ( '<STR_LIT>' ) <EOL> try : <EOL> ref_split = request . META [ '<STR_LIT>' ] . split ( '<STR_LIT:/>' ) <EOL> type , id = ref_split [ - <NUM_LIT:3> : - <NUM_LIT:1> ] <EOL> if type == '<STR_LIT>' : <EOL> systems = systems . filter ( server_model__id = id ) <EOL> except : <EOL> pass <EOL> response = HttpResponse ( mimetype = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> writer = csv . writer ( response ) <EOL> writer . writerow ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> for s in systems : <EOL> try : <EOL> location = s . owner . user_location <EOL> except AttributeError : <EOL> location = '<STR_LIT>' <EOL> writer . writerow ( [ s . owner , location , s . serial , s . asset_tag , <EOL> s . operating_system , s . server_model , s . date_purchased , s . cost ] ) <EOL> return response <EOL> def fillin_csv ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . method == '<STR_LIT:POST>' : <EOL> f = forms . CSVForm ( request . POST , request . FILES ) <EOL> if f . is_valid ( ) : <EOL> response = HttpResponse ( mimetype = '<STR_LIT>' ) <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> in_csv = csv . reader ( f . cleaned_data [ '<STR_LIT>' ] . read ( ) . splitlines ( ) ) <EOL> out_csv = csv . writer ( response ) <EOL> for row in in_csv : <EOL> if row [ <NUM_LIT:4> ] : <EOL> serials = [ i . strip ( ) for i in row [ <NUM_LIT:4> ] . split ( '<STR_LIT:;>' ) ] <EOL> owners = [ <EOL> str ( i . owner ) <EOL> for i in models . UnmanagedSystem . objects . filter ( <EOL> serial__in = serials ) . filter ( owner__isnull = False ) ] <EOL> locations = [ <EOL> str ( i . system_rack . location ) <EOL> for i in system_models . System . objects . filter ( <EOL> serial__in = serials ) . filter ( system_rack__location__isnull = False ) ] <EOL> locations += [ <EOL> str ( i . owner . user_location ) <EOL> for i in models . UnmanagedSystem . objects . filter ( <EOL> serial__in = serials ) . filter ( owner__user_location__isnull = False ) ] <EOL> if owners : <EOL> row [ <NUM_LIT:6> ] = "<STR_LIT>" . join ( owners ) <EOL> if locations : <EOL> row [ <NUM_LIT:7> ] = "<STR_LIT>" . join ( locations ) <EOL> out_csv . writerow ( row ) <EOL> return response <EOL> else : <EOL> f = forms . CSVForm ( ) <EOL> return render_to_response ( <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : f } , <EOL> RequestContext ( request ) ) </s>
