Skip to content

Commit

Permalink
Merge PR #289 into 16.0
Browse files Browse the repository at this point in the history
Signed-off-by lmignon
  • Loading branch information
OCA-git-bot committed Oct 17, 2023
2 parents ce4440b + b866676 commit a387538
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
18 changes: 13 additions & 5 deletions fs_attachment/models/fs_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,30 +310,38 @@ def get_default_storage_code_for_attachments(self):
res_field = self.env.context.get("attachment_res_field")
res_model = self.env.context.get("attachment_res_model")
if res_field and res_model:
field = self.env["ir.model.fields"].search(
[("model", "=", res_model), ("name", "=", res_field)], limit=1
field = (
self.env["ir.model.fields"]
.sudo()
.search([("model", "=", res_model), ("name", "=", res_field)], limit=1)
)
if field:
storage = (
self.env["fs.storage"]
.sudo()
.search([])
.filtered_domain([("field_ids", "in", [field.id])])
)
if storage:
return storage.code
if res_model:
model = self.env["ir.model"].search([("model", "=", res_model)], limit=1)
model = (
self.env["ir.model"].sudo().search([("model", "=", res_model)], limit=1)
)
if model:
storage = (
self.env["fs.storage"]
.sudo()
.search([])
.filtered_domain([("model_ids", "in", [model.id])])
)
if storage:
return storage.code

storages = self.search([]).filtered_domain(
[("use_as_default_for_attachments", "=", True)]
storages = (
self.sudo()
.search([])
.filtered_domain([("use_as_default_for_attachments", "=", True)])
)
if storages:
return storages[0].code
Expand Down
1 change: 1 addition & 0 deletions fs_attachment/readme/newsfragments/289.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix access to technical models to be able to upload attachments for users with basic access
37 changes: 37 additions & 0 deletions fs_attachment/tests/test_fs_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,40 @@ def test_storage_use_filename_obfuscation(self):
self.assertEqual(attachment.checksum, attachment.store_fname.split("/")[-1])
self.assertEqual(attachment.checksum, attachment.fs_url.split("/")[-1])
self.assertEqual(attachment.mimetype, "text/plain")

def test_create_attachments_basic_user(self):
demo_user = self.env.ref("base.user_demo")
demo_partner = self.env.ref("base.partner_demo")
self.temp_backend.use_as_default_for_attachments = True
# Ensure basic access
group_user = self.env.ref("base.group_user")
group_partner_manager = self.env.ref("base.group_partner_manager")
demo_user.write(
{"groups_id": [(6, 0, [group_user.id, group_partner_manager.id])]}
)
# Create basic attachment
self.ir_attachment_model.with_user(demo_user).create(
{"name": "test.txt", "raw": b"content"}
)
# Create attachment related to model
self.ir_attachment_model.with_user(demo_user).create(
{
"name": "test.txt",
"raw": b"content",
"res_model": "res.partner",
"res_id": demo_partner.id,
}
)
# Create attachment related to field
partner_image_field = self.env["ir.model.fields"].search(
[("model", "=", "res.partner"), ("name", "=", "image1920")]
)
self.ir_attachment_model.with_user(demo_user).create(
{
"name": "test.txt",
"raw": b"content",
"res_model": "res.partner",
"res_id": demo_partner.id,
"res_field": partner_image_field.name,
}
)
2 changes: 1 addition & 1 deletion fs_storage/models/fs_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def write(self, vals):
@tools.ormcache()
def get_id_by_code_map(self):
"""Return a dictionary with the code as key and the id as value."""
return {rec.code: rec.id for rec in self.search([])}
return {rec.code: rec.id for rec in self.sudo().search([])}

@api.model
def get_id_by_code(self, code):
Expand Down
1 change: 1 addition & 0 deletions fs_storage/readme/newsfragments/289.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix access to technical models to be able to upload attachments for users with basic access

0 comments on commit a387538

Please sign in to comment.