-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelay_export.py
More file actions
147 lines (121 loc) · 4.93 KB
/
Copy pathdelay_export.py
File metadata and controls
147 lines (121 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Copyright 2019 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import base64
import json
import operator
from dateutil.relativedelta import relativedelta
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.addons.web.controllers.export import CSVExport, ExcelExport
class DelayExport(models.Model):
_name = "delay.export"
_description = "Asynchronous Export"
user_ids = fields.Many2many("res.users", string="Users", index=True)
model_description = fields.Char()
url = fields.Char()
expiration_date = fields.Date()
@api.model
def delay_export(self, data):
"""Delay the export, called from js"""
params = json.loads(data.get("data"))
if not self.env.user.email:
raise UserError(_("You must set an email address to your user."))
self.with_delay().export(params)
@api.model
def _get_file_content(self, params):
export_format = params.get("format")
items = operator.itemgetter(
"model", "fields", "ids", "domain", "import_compat", "context", "user_ids"
)(params)
(model_name, fields_name, ids, domain, import_compat, context, user_ids) = items
model = self.env[model_name].with_context(
import_compat=import_compat, **context
)
records = model.browse(ids) or model.search(
domain, offset=0, limit=False, order=False
)
if not model._is_an_ordinary_table():
fields_name = [field for field in fields_name if field["name"] != "id"]
field_names = [f["name"] for f in fields_name]
import_data = records.export_data(field_names).get("datas", [])
if import_compat:
columns_headers = field_names
else:
columns_headers = [val["label"].strip() for val in fields_name]
if export_format == "csv":
csv = CSVExport()
return csv.from_data(columns_headers, import_data)
else:
xls = ExcelExport()
return xls.from_data(columns_headers, import_data)
@api.model
def export(self, params):
"""Delayed export of a file sent by email
The ``params`` is a dict of parameters, contains:
* format: csv/excel
* model: model to export
* fields: list of fields to export, a list of dict:
[{'label': '', 'name': ''}]
* ids: list of ids to export
* domain: domain for the export
* context: context for the export (language, ...)
* import_compat: if the export is export/import compatible (boolean)
* user_ids: optional list of user ids who receive the file
"""
content = self._get_file_content(params)
items = operator.itemgetter("model", "context", "format", "user_ids")(params)
model_name, context, export_format, user_ids = items
users = self.env["res.users"].browse(user_ids)
export_record = self.sudo().create({"user_ids": [(6, 0, users.ids)]})
name = "{}.{}".format(model_name, export_format)
attachment = (
self.env["ir.attachment"]
.sudo()
.create(
{
"name": name,
"datas": base64.b64encode(content),
"type": "binary",
"res_model": self._name,
"res_id": export_record.id,
}
)
)
url = "{}/web/content/ir.attachment/{}/datas/{}?download=true".format(
self.env["ir.config_parameter"].sudo().get_param("web.base.url"),
attachment.id,
attachment.name,
)
time_to_live = (
self.env["ir.config_parameter"].sudo().get_param("attachment.ttl", 7)
)
date_today = fields.Date.today()
expiration_date = fields.Date.to_string(
date_today + relativedelta(days=+int(time_to_live))
)
odoo_bot = self.sudo().env.ref("base.partner_root")
email_from = odoo_bot.email
model_description = self.env[model_name]._description
export_record.write(
{
"url": url,
"expiration_date": expiration_date,
"model_description": model_description,
}
)
self.env.ref("base_export_async.delay_export_mail_template").send_mail(
export_record.id,
email_values={
"email_from": email_from,
"reply_to": email_from,
"recipient_ids": [(6, 0, users.mapped("partner_id").ids)],
},
)
@api.model
def cron_delete(self):
time_to_live = (
self.env["ir.config_parameter"].sudo().get_param("attachment.ttl", 7)
)
date_today = fields.Date.today()
date_to_delete = date_today + relativedelta(days=-int(time_to_live))
self.search([("create_date", "<=", date_to_delete)]).unlink()