#NEWLINE# def export_to_csv( search_keys , sec_keys_names , documents ) : #NEWLINE# #INDENT# 'Helper to produce proper CSV file for search results export' #NEWLINE# dman = DocumentTypeRuleManager ( ) #NEWLINE# docs = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for document in doc_row : #NEWLINE# #INDENT# docs [ document . id ] = document . _doc #NEWLINE# #UNINDENT# response = HttpResponse ( content_type = 'text/csv' ) #NEWLINE# response [ 'Content-Disposition' ] = 'attachment; filename=search_results.csv' #NEWLINE# writer = csv . writer ( response ) #NEWLINE# counter = ( ( ( ( ( [ 'File' ] + [ 'Date' ] ) + [ 'Username' ] ) + [ 'Description' ] ) + sec_keys_names ) + [ 'Type' ] ) #NEWLINE# writer . writerow ( counter ) #NEWLINE# for ( name , doc ) in docs . iteritems ( ) : #NEWLINE# #INDENT# doc_sec_keys = [ ] #NEWLINE# for sec_key in sec_keys_names : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# doc_sec_keys . append ( unicode ( doc [ 'mdt_indexes' ] [ sec_key ] ) . encode ( 'utf8' ) ) #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# doc_sec_keys . append ( 'Not given' ) #NEWLINE# #UNINDENT# #UNINDENT# cr_date = date_standardized ( re . sub ( 'T\\d{2}:\\d{2}:\\d{2}Z' , '' , doc [ 'metadata_created_date' ] ) ) #NEWLINE# docrule = dman . get_docrule_by_id ( doc [ 'metadata_doc_type_rule_id' ] ) #NEWLINE# docrule_name = docrule . get_title ( ) #NEWLINE# doc_row = ( ( ( ( ( [ unicode ( name ) . encode ( 'utf8' ) ] + [ cr_date ] ) + [ unicode ( doc [ 'metadata_user_name' ] ) . encode ( 'utf8' ) ] ) + [ unicode ( doc [ 'metadata_description' ] ) . encode ( 'utf8' ) ] ) + doc_sec_keys ) + [ unicode ( docrule_name ) . encode ( 'utf8' ) ] ) #NEWLINE# writer . writerow ( doc_row ) #NEWLINE# #UNINDENT# writer . writerow ( '\r\n' ) #NEWLINE# writer . writerow ( [ 'Search with Keys:' ] ) #NEWLINE# writer . writerow ( [ 'Key' , 'Value' ] ) #NEWLINE# for ( item , value ) in search_keys . iteritems ( ) : #NEWLINE# #INDENT# if ( item == 'date' ) : #NEWLINE# #INDENT# item = 'Indexing Date' #NEWLINE# #UNINDENT# if ( item == 'end_date' ) : #NEWLINE# #INDENT# item = 'Indexing Date to' #NEWLINE# #UNINDENT# if ( item == 'description' ) : #NEWLINE# #INDENT# item = 'Description' #NEWLINE# #UNINDENT# if ( item == 'docrule_id' ) : #NEWLINE# #INDENT# item = 'Document Type' #NEWLINE# id = value #NEWLINE# docrule = dman . get_docrule_by_id ( id ) #NEWLINE# value = docrule . get_title ( ) #NEWLINE# #UNINDENT# if ( not ( value . __class__ . __name__ == 'tuple' ) ) : #NEWLINE# #INDENT# writer . writerow ( [ unicode ( item ) . encode ( 'utf8' ) , unicode ( value ) . encode ( 'utf8' ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# writer . writerow ( ( ( ( ( ( unicode ( item ) . encode ( 'utf8' ) + ': (from: ' ) + unicode ( value [ 0 ] ) . encode ( 'utf8' ) ) + ' to: ' ) + unicode ( value [ 1 ] ) . encode ( 'utf8' ) ) + ')' ) ) #NEWLINE# #UNINDENT# #UNINDENT# return response