I want to import a folder with files from a path in my computer not from a zip file
this the old code (importing a zip file):
@api.model
def create(self, values):
res = super(Directory, self).create(values)
type_customer = values.get('type_customer', False)
type_file = values.get('type_file', False)
pdf_name_list =[]
content = values.get('content', False)
binary = base64.b64decode(content or "")
f = io.BytesIO(binary)
if zipfile.is_zipfile(f):
with zipfile.ZipFile(f, 'r') as zip:
for name in zip.namelist():
if '.pdf' in name:
name_split = name.split('/')[-1]
id_partner = False
id_station = False
id_terminal = False
date_invoice = False
date_from_statement = False
date_to_statement = False
account = False
period_statement = False
period_invoice = False
if type_customer =='porteur_carte':
if type_file =='invoices':
id_partner = name_split.split('-')[0]
date_invoice = (name_split.split('-')[1]).split('.pdf')[0]
date_invoice = datetime.strptime(date_invoice, '%d%m%Y').date()
period_invoice = date_invoice and str(date_invoice)[:7] or False
elif type_file == 'statements' :
id_partner = name_split.split('_')[0]
partner=self.env['res.partner'].search([('type_customer','=','porteur_carte'),('id_customer','=',id_partner)], limit=1)
if partner:
statement = (name_split.split(id_partner)[1]).split('.pdf')[0]
account = statement.split('_')[1]
date_to_statement = datetime.strptime(statement.split('_')[-1], '%d%m%Y').date()
date_from_statement = datetime.strptime(statement.split('_')[-2], '%d%m%Y').date()
period_statement = date_from_statement and str(date_from_statement)[:7] or False
with zip.open(name, 'r') as file_t:
pdf_file = base64.encodestring(file_t.read())
if len(pdf_file) > 1245 :
new_file = self.env['muk_dms.file'].create({ 'name':name_split,
'content':pdf_file,
'directory':res.id,
'mimetype':'application/pdf',
'id_partner':id_partner,
'type_customer':type_customer,
'type_file':type_file,
'date_invoice':date_invoice,
'account':account,
'date_from_statement':date_from_statement,
'date_to_statement':date_to_statement,
})
if new_file:
self.env['ir.attachment'].create({'name':name_split,
'datas_fname':name_split,
'file_id':new_file.id,
'datas':pdf_file,
'mimetype':'application/pdf',
'res_model':'muk_dms.file',
'res_id':new_file.id})
return res
please help and thanks.