Created
April 29, 2021 11:28
-
-
Save ilyasProgrammer/824e999844a2e9637cc3125b09013f9b to your computer and use it in GitHub Desktop.
Rotate CSV from attachment
This file contains hidden or 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
atts = self.env['ir.attachment'].search([('res_model', '=', 'syscoon.financeinterface'), | |
('type', '=', 'binary'), | |
('res_id', '=', export_id)]) | |
for at in atts: | |
# order lines by date asc. just revert CSV file data rows. | |
data = base64.decodebytes(at.datas).decode("latin") | |
stream = StringIO(data) | |
dialect = csv.Sniffer().sniff(data[:5000]) | |
data_list = list(csv.reader(stream, dialect=dialect)) | |
file_path = "/var/tmp/" + at.name | |
with open(file_path, 'w') as w: | |
write = csv.writer(w, delimiter=";", dialect='excel') | |
write.writerow(data_list[0]) | |
write.writerow(data_list[1]) | |
write.writerows(reversed(data_list[2:])) | |
with open(file_path, 'rb') as r: | |
at.datas = base64.b64encode(r.read()) | |
os.remove(file_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment