<s> '''<STR_LIT>''' <EOL> from puppet . models import Host , Fact , FactValue <EOL> from django . shortcuts import render <EOL> from django import forms <EOL> from django . contrib . admin . widgets import FilteredSelectMultiple <EOL> def inventory ( request ) : <EOL> '''<STR_LIT>''' <EOL> keys = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> facts = FactValue . objects . all ( ) <EOL> facts = facts . filter ( fact_name__name__in = keys ) <EOL> facts = facts . order_by ( '<STR_LIT:host>' ) <EOL> facts = facts . select_related ( ) <EOL> hosts = [ ] <EOL> from itertools import groupby <EOL> for key , values in groupby ( facts , key = lambda x : x . host . name ) : <EOL> host = { '<STR_LIT:name>' : key } <EOL> for v in values : <EOL> host [ v . name ] = v . value <EOL> hosts . append ( host ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : hosts } ) <EOL> def query ( request ) : <EOL> '''<STR_LIT>''' <EOL> class MatrixForm ( forms . Form ) : <EOL> hosts = forms . ModelMultipleChoiceField ( <EOL> queryset = Host . objects . all ( ) , <EOL> widget = FilteredSelectMultiple ( '<STR_LIT>' , is_stacked = False ) ) <EOL> facts = forms . ModelMultipleChoiceField ( <EOL> queryset = Fact . objects . all ( ) <EOL> . exclude ( name__startswith = '<STR_LIT>' ) <EOL> . exclude ( name__startswith = '<STR_LIT>' ) <EOL> . exclude ( name__startswith = '<STR_LIT>' ) <EOL> . exclude ( name__startswith = '<STR_LIT>' ) <EOL> . exclude ( name__startswith = '<STR_LIT>' ) <EOL> . exclude ( name__startswith = '<STR_LIT>' ) <EOL> . exclude ( name__startswith = '<STR_LIT>' ) , <EOL> widget = FilteredSelectMultiple ( '<STR_LIT>' , is_stacked = False ) ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> f = MatrixForm ( label_suffix = '<STR_LIT>' ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : f } ) <EOL> else : <EOL> f = MatrixForm ( request . POST ) <EOL> if f . is_valid ( ) : <EOL> results = [ ] <EOL> facts = [ ] <EOL> for fact in f . cleaned_data [ '<STR_LIT>' ] : <EOL> facts . append ( fact . name ) <EOL> facts . sort ( ) <EOL> values = FactValue . objects . all ( ) <EOL> values = values . filter ( fact_name__in = f . cleaned_data [ '<STR_LIT>' ] ) <EOL> values = values . filter ( host__in = f . cleaned_data [ '<STR_LIT>' ] ) <EOL> values = values . order_by ( '<STR_LIT:host>' ) <EOL> values = values . select_related ( ) <EOL> from itertools import groupby <EOL> for key , values in groupby ( values , key = lambda x : x . host . name ) : <EOL> row = { } <EOL> for v in values : <EOL> row [ v . name ] = v . value <EOL> row = [ row . get ( k , None ) for k in facts ] <EOL> results . append ( { '<STR_LIT:host>' : key , '<STR_LIT>' : row } ) <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : facts , '<STR_LIT>' : results } ) <EOL> else : <EOL> return render ( request , '<STR_LIT>' , { '<STR_LIT>' : f } ) </s>
