#NEWLINE# def build_response( stmt_result ) : #NEWLINE# #INDENT# sha2s = [ ] #NEWLINE# mime_type = 'application/json' #NEWLINE# if isinstance ( stmt_result , dict ) : #NEWLINE# #INDENT# statements = stmt_result [ 'statements' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# statements = json . loads ( stmt_result ) [ 'statements' ] #NEWLINE# #UNINDENT# for stmt in statements : #NEWLINE# #INDENT# if ( 'attachments' in stmt ) : #NEWLINE# #INDENT# st_atts = Statement . objects . get ( statement_id = stmt [ 'id' ] ) . stmt_attachments #NEWLINE# if st_atts : #NEWLINE# #INDENT# for att in st_atts . all ( ) : #NEWLINE# #INDENT# if att . payload : #NEWLINE# #INDENT# sha2s . append ( ( att . canonical_data [ 'sha2' ] , att . payload , att . canonical_data [ 'contentType' ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if sha2s : #NEWLINE# #INDENT# string_list = [ ] #NEWLINE# line_feed = '\r\n' #NEWLINE# boundary = '======ADL_LRS======' #NEWLINE# string_list . append ( ( ( ( line_feed + '--' ) + boundary ) + line_feed ) ) #NEWLINE# string_list . append ( ( ( 'Content-Type:application/json' + line_feed ) + line_feed ) ) #NEWLINE# if isinstance ( stmt_result , dict ) : #NEWLINE# #INDENT# string_list . append ( ( json . dumps ( stmt_result ) + line_feed ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# string_list . append ( ( stmt_result + line_feed ) ) #NEWLINE# #UNINDENT# for sha2 in sha2s : #NEWLINE# #INDENT# string_list . append ( ( ( '--' + boundary ) + line_feed ) ) #NEWLINE# string_list . append ( ( ( 'Content-Type:%s' % str ( sha2 [ 2 ] ) ) + line_feed ) ) #NEWLINE# string_list . append ( ( 'Content-Transfer-Encoding:binary' + line_feed ) ) #NEWLINE# string_list . append ( ( ( ( 'X-Experience-API-Hash:' + str ( sha2 [ 0 ] ) ) + line_feed ) + line_feed ) ) #NEWLINE# chunks = [ ] #NEWLINE# try : #NEWLINE# #INDENT# for chunk in sha2 [ 1 ] . chunks ( ) : #NEWLINE# #INDENT# chunks . append ( chunk ) #NEWLINE# #UNINDENT# #UNINDENT# except OSError : #NEWLINE# #INDENT# raise OSError ( 2 , 'No such file or directory' , sha2 [ 1 ] . name . split ( '/' ) [ 1 ] ) #NEWLINE# #UNINDENT# string_list . append ( ( '' . join ( chunks ) + line_feed ) ) #NEWLINE# #UNINDENT# string_list . append ( ( ( '--' + boundary ) + '--' ) ) #NEWLINE# mime_type = ( 'multipart/mixed; boundary=' + boundary ) #NEWLINE# attachment_body = '' . join ( [ str ( s ) for s in string_list ] ) #NEWLINE# return ( attachment_body , mime_type , len ( attachment_body ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( stmt_result , dict ) : #NEWLINE# #INDENT# res = json . dumps ( stmt_result ) #NEWLINE# return ( res , mime_type , len ( res ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( att , mime_type , len ( stmt_result ) )