Created
May 23, 2015 20:49
-
-
Save yk2kus/44bc65bea342db35f036 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def download_attachments(self, cr, uid, ids, context = None): | |
cache_buster = randint(10000000000,90000000000) | |
config_obj = self.pool.get('ir.config_parameter') | |
attachment_obj = self.pool.get('ir.attachment') | |
filestore_path = os.path.join(attachment_obj._filestore(cr, uid), '') | |
print "filestore path..............",filestore_path | |
attachment_dir = filestore_path + 'attachments' | |
#create directory and remove its content | |
if not os.path.exists(attachment_dir): | |
os.makedirs(attachment_dir) | |
else: | |
shutil.rmtree(attachment_dir) | |
os.makedirs(attachment_dir) | |
file_name = 'files' | |
if isinstance(ids, int): | |
ids = [ids] | |
wzrd_obj = self.browse(cr, uid, ids[0]) | |
config_ids = config_obj.search(cr, uid, [('key','=','web.base.url')]) | |
if len(config_ids): | |
value = config_obj.browse(cr, uid, config_ids[0]).value | |
active_model = 'ir.attachment'#wzrd_obj.active_model | |
active_id = wzrd_obj.active_id | |
#tar_dir = attachment_dir + '/' + file_name | |
tar_dir = os.path.join(attachment_dir, file_name) | |
tFile = tarfile.open(tar_dir, 'w') | |
if value and active_model and active_id: | |
#change working directory otherwise file is tared with all its parent directories | |
original_dir = os.getcwd() | |
if not wzrd_obj.attachment_ids: | |
raise Warning(_("No attachment to download")) | |
for attachment in wzrd_obj.attachment_ids: | |
#to get full path of file | |
full_path = attachment_obj._full_path(cr, uid, attachment.store_fname) | |
new_file = os.path.join(attachment_dir, attachment.name) | |
#copying in a new directory with a new name | |
shutil.copy2(full_path, new_file) | |
head, tail = ntpath.split(new_file) | |
#change working directory otherwise it tars all parent directory | |
os.chdir(head) | |
tFile.add(tail) | |
print "added file ...................................",tail | |
os.chdir(original_dir) | |
attachment_id = self.pool.get('ir.attachment').create(cr, uid, | |
{ | |
'name' : file_name+'.tar', | |
'res_model' : 'download.attachments', | |
'res_id' : ids[0], | |
'res_name' : 'test....', | |
'type': 'binary', | |
'store_fname' : 'attachments/files', | |
}) | |
active_id = attachment_id | |
url = '%s/web/binary/saveas?model=%s&field=datas&filename_field=name&id=%s&t=%s"'%(value , active_model ,active_id, cache_buster) | |
print "url................",url | |
return { | |
'type': 'ir.actions.act_url', | |
'url':url, | |
'nodestroy': False, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment