Skip to content

Commit 19cd867

Browse files
yelizarievtrojikman
authored andcommitted
😍 ir_attachment_url: cleanup code
1 parent 87d5408 commit 19cd867

File tree

3 files changed

+25
-54
lines changed

3 files changed

+25
-54
lines changed

ir_attachment_url/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"summary": """Use attachment URL and upload data to external storage""",
66
"category": "Tools",
77
"images": [],
8-
"version": "14.0.2.0.1",
8+
"version": "14.0.2.1.0",
99
"application": False,
1010
"author": "IT-Projects LLC, Ildar Nasyrov",
1111
"website": "https://twitter.com/OdooFree",

ir_attachment_url/doc/changelog.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
`2.1.0`
2+
-------
3+
4+
- **Improvement:** Performance improvement
5+
16
`2.0.1`
27
-------
38

49
- **Fix:** error on creating new record with Binary field
510

6-
711
`2.0.0`
812
-------
913

10-
- **Improvment:** Images defined by URL are now uploaded as binary files
11-
- **Improvment:** Request to binary files with non-falsy `url` attribute return HTTP 301 response
12-
- **Improvment:** Removed requirement of setting ir_attachment_url as server wide module
14+
- **Improvement:** Images defined by URL are now uploaded as binary files
15+
- **Improvement:** Request to binary files with non-falsy `url` attribute return HTTP 301 response
16+
- **Improvement:** Removed requirement of setting ir_attachment_url as server wide module
1317
- **Discard:** Passing url to Binary/Image field is no longer supported
1418
- **Fix:** Returing 302 instead of 301 http redirections to escape browser caching
1519

ir_attachment_url/models/ir_attachment.py

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright 2016-2018 Ildar Nasyrov <https://it-projects.info/team/iledarn>
2-
# Copyright 2016-2020 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
2+
# Copyright 2016-2018,2020 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
33
# Copyright 2020 Eugene Molotov <https://it-projects.info/team/em230418>
44
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
55

@@ -39,69 +39,36 @@ def _filter_protected_attachments(self):
3939

4040
@api.model_create_multi
4141
def create(self, vals_list):
42-
record_tuple_set = set()
42+
url_fields = self.env.context.get("ir_attachment_url_fields")
43+
if url_fields:
44+
url_fields = url_fields.split(",")
45+
self._set_where_to_store(vals_list)
4346
for values in vals_list:
44-
# ---
45-
# em230418: handling ir_attachment_url_fields context
4647
if (
47-
self.env.context.get("ir_attachment_url_fields")
48+
url_fields
4849
and values.get("type") != "url"
4950
and not values.get("url")
5051
and values.get("res_model")
5152
and values.get("res_field")
5253
and values.get("datas")
5354
):
5455
full_field_name = values["res_model"] + "." + values["res_field"]
55-
if full_field_name in self.env.context.get(
56-
"ir_attachment_url_fields"
57-
).split(","):
56+
if full_field_name in url_fields:
5857
values["url"] = values["datas"]
5958
values["type"] = "url"
6059
del values["datas"]
61-
# ---
62-
63-
self._set_where_to_store(vals_list)
64-
65-
# everything else is based on
66-
# https://github.com/odoo/odoo/blob/fa852ba1c5707b71469c410063f338eef261ab2b/odoo/addons/base/models/ir_attachment.py#L506-L524
67-
68-
# remove computed field depending of datas
69-
for field in ("file_size", "checksum"):
70-
values.pop(field, False)
7160
bucket = values.pop("_bucket", None)
72-
values = self._check_contents(values)
73-
if "datas" in values:
74-
# ===============
75-
# if bucket is not available, attachment is saved as usual
61+
if (
62+
bucket
63+
and values.get("datas")
64+
and values.get("res_model") not in ["ir.ui.view", "ir.ui.menu"]
65+
):
66+
values = self._check_contents(values)
7667
data = values.pop("datas")
7768
mimetype = values.pop("mimetype")
78-
if (
79-
bucket
80-
and values.get("res_model") not in ["ir.ui.view", "ir.ui.menu"]
81-
and self._storage() != "db"
82-
and data
83-
):
84-
values.update(
85-
self._get_datas_related_values_with_bucket(
86-
bucket, data, mimetype
87-
)
88-
)
89-
else:
90-
values.update(
91-
self._get_datas_related_values(
92-
base64.b64decode(data or b""), mimetype
93-
)
94-
)
95-
# end
96-
# ===============
97-
# 'check()' only uses res_model and res_id from values, and make an exists.
98-
# We can group the values by model, res_id to make only one query when
99-
# creating multiple attachments on a single record.
100-
record_tuple = (values.get("res_model"), values.get("res_id"))
101-
record_tuple_set.add(record_tuple)
102-
for record_tuple in record_tuple_set:
103-
(res_model, res_id) = record_tuple
104-
self.check("create", values={"res_model": res_model, "res_id": res_id})
69+
values.update(
70+
self._get_datas_related_values_with_bucket(bucket, data, mimetype)
71+
)
10572
return super(IrAttachment, self).create(vals_list)
10673

10774
def _get_datas_related_values_with_bucket(

0 commit comments

Comments
 (0)