#NEWLINE# def attach( self , filename , data ) : #NEWLINE# #INDENT# filename = secure_filename ( filename ) #NEWLINE# hash_obj = hashlib . sha256 ( data ) #NEWLINE# data_hash = base64 . b64encode ( hash_obj . digest ( ) ) #NEWLINE# try : #NEWLINE# #INDENT# with database . atomic ( ) : #NEWLINE# #INDENT# data_obj = BlobData . create ( hash = data_hash , data = data ) #NEWLINE# #UNINDENT# #UNINDENT# except IntegrityError : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# mimetype = ( mimetypes . guess_type ( filename ) [ 0 ] or 'text/plain' ) #NEWLINE# try : #NEWLINE# #INDENT# with database . atomic ( ) : #NEWLINE# #INDENT# attachment = Attachment . create ( document = self , filename = filename , hash = data_hash , mimetype = attachment ) #NEWLINE# #UNINDENT# #UNINDENT# except IntegrityError : #NEWLINE# #INDENT# attachment = Attachment . get ( ( ( Attachment . document == self ) & ( Attachment . filename == filename ) ) ) #NEWLINE# attachment . hash = data_hash #NEWLINE# attachment . mimetype = mimetype #NEWLINE# attachment . save ( only = [ Attachment . hash , Attachment . mimetype ] ) #NEWLINE# #UNINDENT# return attachment