From c3c142e7b1a91ace35d2dfc97773e72d851bf70c Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Fri, 14 Aug 2020 09:33:43 -0300 Subject: [PATCH 01/17] [ADD] DMS integration with attachment --- dms/models/__init__.py | 2 ++ dms/models/directory.py | 7 +++++++ dms/models/dms_file.py | 18 ++++++++++++++++-- dms/models/ir_attachment.py | 21 +++++++++++++++++++++ dms/models/storage.py | 3 ++- dms/views/directory.xml | 3 +++ 6 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 dms/models/ir_attachment.py diff --git a/dms/models/__init__.py b/dms/models/__init__.py index 50084255a..c11b7d156 100644 --- a/dms/models/__init__.py +++ b/dms/models/__init__.py @@ -11,3 +11,5 @@ from . import res_company from . import res_config_settings + +from . import ir_attachment diff --git a/dms/models/directory.py b/dms/models/directory.py index 7c09c07e8..7befb72d2 100644 --- a/dms/models/directory.py +++ b/dms/models/directory.py @@ -199,6 +199,13 @@ class DmsDirectory(models.Model): """, ) + ir_model_id = fields.Many2one( + comodel_name='ir.model', + string='Attachment Model', + required=False + ) + + @api.depends("name", "complete_name") def _compute_display_name(self): if not self.env.context.get("directory_short_name", False): diff --git a/dms/models/dms_file.py b/dms/models/dms_file.py index 662c1fc4a..b06b0ccc4 100644 --- a/dms/models/dms_file.py +++ b/dms/models/dms_file.py @@ -150,6 +150,13 @@ class File(models.Model): attachment=True, string="Content File", prefetch=False, invisible=True ) + attachment_id = fields.Many2one( + comodel_name='ir.attachment', + string='Attachment File', + prefetch=False, + invisible=True + ) + # ---------------------------------------------------------- # Helper # ---------------------------------------------------------- @@ -172,6 +179,8 @@ def _update_content_vals(self, vals, binary): ) if self.storage_id.save_type == "file": new_vals["content_file"] = self.content + elif self.storage_id.save_type == "attachment": + new_vals['attachment_id'] = self.content elif self.storage_id.save_type == "database": new_vals["content_binary"] = self.content and binary return new_vals @@ -356,15 +365,20 @@ def _compute_mimetype(self): mimetype = guess_mimetype(binary, default="application/octet-stream") record.res_mimetype = mimetype - @api.depends("content_binary", "content_file") + @api.depends("content_binary", "content_file", "attachment_id") def _compute_content(self): bin_size = self.env.context.get("bin_size", False) for record in self: if record.content_file: context = {"human_size": True} if bin_size else {"base64": True} record.content = record.with_context(context).content_file - else: + elif record.content_binary: record.content = base64.b64encode(record.content_binary) + else: + context = {"human_size": True} if bin_size else { + "base64": True} + record.content = \ + record.with_context(context).attachment_id.datas @api.depends("content_binary", "content_file") def _compute_save_type(self): diff --git a/dms/models/ir_attachment.py b/dms/models/ir_attachment.py new file mode 100644 index 000000000..323d30032 --- /dev/null +++ b/dms/models/ir_attachment.py @@ -0,0 +1,21 @@ +from odoo import api, models, fields, tools + + +class IrAttachment(models.Model): + + _inherit = "ir.attachment" + + @api.model + def create(self, vals): + attachment_id = super(IrAttachment, self).create(vals) + category_id = self.env['dms.directory'].search([ + ('ir_model_id', '=', vals['res_model'])]) + + if category_id: + self.env['dms.file'].create({ + 'name': vals['name'], + 'directory_id': category_id.id, + 'attachment_id': attachment_id.id, + }) + + return attachment_id diff --git a/dms/models/storage.py b/dms/models/storage.py index 52ad1584a..a2cf44580 100644 --- a/dms/models/storage.py +++ b/dms/models/storage.py @@ -22,7 +22,8 @@ class Storage(models.Model): name = fields.Char(string="Name", required=True) save_type = fields.Selection( - selection=[("database", _("Database")), ("file", _("Filestore"))], + selection=[("database", _("Database")), ("file", _("Filestore")), + ("attachment", _("Attachment"))], string="Save Type", default="database", required=True, diff --git a/dms/views/directory.xml b/dms/views/directory.xml index 670ccbe0f..988b66e1d 100644 --- a/dms/views/directory.xml +++ b/dms/views/directory.xml @@ -473,6 +473,9 @@ + + + Date: Fri, 14 Aug 2020 11:36:00 -0300 Subject: [PATCH 02/17] [ADD] Record sub directory --- dms/models/directory.py | 6 ++++++ dms/models/dms_file.py | 3 ++- dms/models/ir_attachment.py | 22 +++++++++++++++++++--- dms/views/directory.xml | 1 + 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/dms/models/directory.py b/dms/models/directory.py index 7befb72d2..16c41047c 100644 --- a/dms/models/directory.py +++ b/dms/models/directory.py @@ -205,6 +205,12 @@ class DmsDirectory(models.Model): required=False ) + record_sub_directory = fields.Boolean( + string='Record Sub Directoty', + required=False, + default=True, + ) + @api.depends("name", "complete_name") def _compute_display_name(self): diff --git a/dms/models/dms_file.py b/dms/models/dms_file.py index b06b0ccc4..7f66dfcac 100644 --- a/dms/models/dms_file.py +++ b/dms/models/dms_file.py @@ -154,7 +154,8 @@ class File(models.Model): comodel_name='ir.attachment', string='Attachment File', prefetch=False, - invisible=True + invisible=True, + ondelete = "cascade" ) # ---------------------------------------------------------- diff --git a/dms/models/ir_attachment.py b/dms/models/ir_attachment.py index 323d30032..7955ef9e1 100644 --- a/dms/models/ir_attachment.py +++ b/dms/models/ir_attachment.py @@ -8,13 +8,29 @@ class IrAttachment(models.Model): @api.model def create(self, vals): attachment_id = super(IrAttachment, self).create(vals) - category_id = self.env['dms.directory'].search([ + directory_id = self.env['dms.directory'].search([ ('ir_model_id', '=', vals['res_model'])]) - if category_id: + if directory_id and directory_id.record_sub_directory: + save_directory_id = self.env['dms.directory'].search([ + ('name', '=', attachment_id.res_name)]) + if not save_directory_id: + save_directory_id = self.env['dms.directory'].create({ + 'name': attachment_id.res_name.replace('/','-'), + 'parent_id': directory_id.id, + 'storage_id': directory_id.root_storage_id.id, + }) + + self.env['dms.file'].create({ + 'name': vals['name'], + 'directory_id': save_directory_id.id, + 'attachment_id': attachment_id.id, + }) + + elif directory_id: self.env['dms.file'].create({ 'name': vals['name'], - 'directory_id': category_id.id, + 'directory_id': directory_id.id, 'attachment_id': attachment_id.id, }) diff --git a/dms/views/directory.xml b/dms/views/directory.xml index 988b66e1d..95b792447 100644 --- a/dms/views/directory.xml +++ b/dms/views/directory.xml @@ -475,6 +475,7 @@ + From 377562f685bca14db644e53a4dfacc89c01654c2 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Fri, 14 Aug 2020 11:53:16 -0300 Subject: [PATCH 03/17] [FIX] Typo --- dms/models/dms_file.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dms/models/dms_file.py b/dms/models/dms_file.py index 7f66dfcac..eb5e09abf 100644 --- a/dms/models/dms_file.py +++ b/dms/models/dms_file.py @@ -155,7 +155,7 @@ class File(models.Model): string='Attachment File', prefetch=False, invisible=True, - ondelete = "cascade" + ondelete="cascade" ) # ---------------------------------------------------------- From 0437c0efc6ebcf0ced376af7839e966ce3598d96 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Mon, 17 Aug 2020 08:42:40 -0300 Subject: [PATCH 04/17] [ADD] dms_file record reference --- dms/models/dms_file.py | 10 ++++++++++ dms/models/ir_attachment.py | 4 ++++ dms/views/dms_file.xml | 3 +++ 3 files changed, 17 insertions(+) diff --git a/dms/models/dms_file.py b/dms/models/dms_file.py index eb5e09abf..9d03f718f 100644 --- a/dms/models/dms_file.py +++ b/dms/models/dms_file.py @@ -158,6 +158,12 @@ class File(models.Model): ondelete="cascade" ) + record_ref = fields.Reference( + string='Record', + selection='_select_reference', + readonly=True, + ) + # ---------------------------------------------------------- # Helper # ---------------------------------------------------------- @@ -649,3 +655,7 @@ def _compute_locked(self): ) else: record.update({"is_locked": False, "is_lock_editor": False}) + + def _select_reference(self): + model_ids = self.env['ir.model'].search([]) + return [(r['model'], r['name']) for r in model_ids] + [('', '')] diff --git a/dms/models/ir_attachment.py b/dms/models/ir_attachment.py index 7955ef9e1..220ae3099 100644 --- a/dms/models/ir_attachment.py +++ b/dms/models/ir_attachment.py @@ -25,6 +25,8 @@ def create(self, vals): 'name': vals['name'], 'directory_id': save_directory_id.id, 'attachment_id': attachment_id.id, + 'record_ref': "{},{}".format( + attachment_id.res_model, attachment_id.res_id), }) elif directory_id: @@ -32,6 +34,8 @@ def create(self, vals): 'name': vals['name'], 'directory_id': directory_id.id, 'attachment_id': attachment_id.id, + 'record_ref': "{},{}".format( + attachment_id.res_model, attachment_id.res_id), }) return attachment_id diff --git a/dms/views/dms_file.xml b/dms/views/dms_file.xml index 6db5a8855..8212a1e47 100644 --- a/dms/views/dms_file.xml +++ b/dms/views/dms_file.xml @@ -410,6 +410,9 @@ /> + + + From 3e99b201d7b1a949f522d9234bea616beb9d4fa1 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Mon, 17 Aug 2020 08:43:48 -0300 Subject: [PATCH 05/17] [ADD] Directory record reference --- dms/models/directory.py | 10 ++++++++++ dms/models/ir_attachment.py | 2 ++ dms/views/directory.xml | 1 + 3 files changed, 13 insertions(+) diff --git a/dms/models/directory.py b/dms/models/directory.py index 16c41047c..19c7485ec 100644 --- a/dms/models/directory.py +++ b/dms/models/directory.py @@ -211,6 +211,12 @@ class DmsDirectory(models.Model): default=True, ) + record_ref = fields.Reference( + string='Record', + selection='_select_reference', + readonly=True, + ) + @api.depends("name", "complete_name") def _compute_display_name(self): @@ -570,3 +576,7 @@ def unlink(self): DmsDirectory, self.sudo().search([("id", "child_of", self.ids)]) ).unlink() return super().unlink() + + def _select_reference(self): + model_ids = self.env['ir.model'].search([]) + return [(r['model'], r['name']) for r in model_ids] + [('', '')] diff --git a/dms/models/ir_attachment.py b/dms/models/ir_attachment.py index 220ae3099..8a1a6ae36 100644 --- a/dms/models/ir_attachment.py +++ b/dms/models/ir_attachment.py @@ -19,6 +19,8 @@ def create(self, vals): 'name': attachment_id.res_name.replace('/','-'), 'parent_id': directory_id.id, 'storage_id': directory_id.root_storage_id.id, + 'record_ref': "{},{}".format( + attachment_id.res_model, attachment_id.res_id), }) self.env['dms.file'].create({ diff --git a/dms/views/directory.xml b/dms/views/directory.xml index 95b792447..b0421ac41 100644 --- a/dms/views/directory.xml +++ b/dms/views/directory.xml @@ -476,6 +476,7 @@ + From 147a897d1af37510feb886b80d6670b6148469b8 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Mon, 17 Aug 2020 08:44:12 -0300 Subject: [PATCH 06/17] [REM] Blank Line --- dms/models/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/dms/models/__init__.py b/dms/models/__init__.py index c11b7d156..982e7b0e2 100644 --- a/dms/models/__init__.py +++ b/dms/models/__init__.py @@ -11,5 +11,4 @@ from . import res_company from . import res_config_settings - from . import ir_attachment From 5b724de1e7233490669ee998f72e9b4e36ee697a Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Mon, 17 Aug 2020 09:46:03 -0300 Subject: [PATCH 07/17] [ADD] Handle attachment with same name --- dms/models/ir_attachment.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/dms/models/ir_attachment.py b/dms/models/ir_attachment.py index 8a1a6ae36..44fdb4bbd 100644 --- a/dms/models/ir_attachment.py +++ b/dms/models/ir_attachment.py @@ -1,3 +1,5 @@ +import re + from odoo import api, models, fields, tools @@ -5,6 +7,18 @@ class IrAttachment(models.Model): _inherit = "ir.attachment" + def _dms_file_name(self, directory_id, file_name): + count = 1 + for file in directory_id.file_ids: + + dms_file_name = re.sub(r' \([0-9]*\)', '', file.name) + if dms_file_name == file_name: + count += 1 + + file, ext = re.split(r'\.', file_name) + return file + " ({}).".format(count) + ext if count > 1 else file_name + + @api.model def create(self, vals): attachment_id = super(IrAttachment, self).create(vals) @@ -16,15 +30,17 @@ def create(self, vals): ('name', '=', attachment_id.res_name)]) if not save_directory_id: save_directory_id = self.env['dms.directory'].create({ - 'name': attachment_id.res_name.replace('/','-'), + 'name': attachment_id.res_name.replace('/', '-'), 'parent_id': directory_id.id, 'storage_id': directory_id.root_storage_id.id, 'record_ref': "{},{}".format( attachment_id.res_model, attachment_id.res_id), }) + file_name = self._dms_file_name(save_directory_id, vals['name']) + self.env['dms.file'].create({ - 'name': vals['name'], + 'name': file_name, 'directory_id': save_directory_id.id, 'attachment_id': attachment_id.id, 'record_ref': "{},{}".format( @@ -32,8 +48,11 @@ def create(self, vals): }) elif directory_id: + + file_name = self._dms_file_name(directory_id, vals['name']) + self.env['dms.file'].create({ - 'name': vals['name'], + 'name': file_name, 'directory_id': directory_id.id, 'attachment_id': attachment_id.id, 'record_ref': "{},{}".format( From 253ebaa38174b9f1df3a0e56f505ed96b04930c2 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Tue, 18 Aug 2020 12:35:36 -0300 Subject: [PATCH 08/17] [ADD] Create attachment from dms file --- dms/models/directory.py | 17 ++----- dms/models/dms_file.py | 32 +++++++++++++ dms/models/ir_attachment.py | 93 +++++++++++++++++++++---------------- 3 files changed, 91 insertions(+), 51 deletions(-) diff --git a/dms/models/directory.py b/dms/models/directory.py index 19c7485ec..e2371b579 100644 --- a/dms/models/directory.py +++ b/dms/models/directory.py @@ -200,24 +200,17 @@ class DmsDirectory(models.Model): ) ir_model_id = fields.Many2one( - comodel_name='ir.model', - string='Attachment Model', - required=False + comodel_name="ir.model", string="Attachment Model", required=False ) record_sub_directory = fields.Boolean( - string='Record Sub Directoty', - required=False, - default=True, + string="Create Sub Directory for Record", required=False, default=True, ) record_ref = fields.Reference( - string='Record', - selection='_select_reference', - readonly=True, + string="Record Referenced", selection="_select_reference", readonly=True, ) - @api.depends("name", "complete_name") def _compute_display_name(self): if not self.env.context.get("directory_short_name", False): @@ -578,5 +571,5 @@ def unlink(self): return super().unlink() def _select_reference(self): - model_ids = self.env['ir.model'].search([]) - return [(r['model'], r['name']) for r in model_ids] + [('', '')] + model_ids = self.env["ir.model"].search([]) + return [(r["model"], r["name"]) for r in model_ids] + [("", "")] diff --git a/dms/models/dms_file.py b/dms/models/dms_file.py index 9d03f718f..5b9e630e8 100644 --- a/dms/models/dms_file.py +++ b/dms/models/dms_file.py @@ -581,6 +581,31 @@ def _inverse_content(self): for vals, ids in updates.items(): self.browse(ids).write(dict(vals)) + def _create_model_attachment(self, vals): + directory_id = self.env["dms.directory"].search( + [("id", "=", vals["directory_id"])] + ) + + if directory_id and directory_id.record_ref: + + if directory_id.record_ref: + model_name = directory_id.record_ref._name + model_id = directory_id.record_ref.id + + attachment_id = self.env["ir.attachment"].create( + { + "name": vals["name"], + "datas": vals["content"], + "res_model": model_name, + "res_id": model_id, + "dms_file": True, + } + ) + + vals["attachment_id"] = attachment_id.id + vals["record_ref"] = "{},{}".format(model_name, model_id) + del vals["content"] + def copy(self, default=None): self.ensure_one() default = dict(default or []) @@ -607,6 +632,13 @@ def unlink(self): # will be deleted return super(File, self.sudo()).unlink() + @api.model + def create(self, vals): + if not "record_ref" in vals: + self._create_model_attachment(vals) + + return super(File, self).create(vals) + # ---------------------------------------------------------- # Locking fields and functions # ---------------------------------------------------------- diff --git a/dms/models/ir_attachment.py b/dms/models/ir_attachment.py index 44fdb4bbd..92735c3ac 100644 --- a/dms/models/ir_attachment.py +++ b/dms/models/ir_attachment.py @@ -21,42 +21,57 @@ def _dms_file_name(self, directory_id, file_name): @api.model def create(self, vals): - attachment_id = super(IrAttachment, self).create(vals) - directory_id = self.env['dms.directory'].search([ - ('ir_model_id', '=', vals['res_model'])]) - - if directory_id and directory_id.record_sub_directory: - save_directory_id = self.env['dms.directory'].search([ - ('name', '=', attachment_id.res_name)]) - if not save_directory_id: - save_directory_id = self.env['dms.directory'].create({ - 'name': attachment_id.res_name.replace('/', '-'), - 'parent_id': directory_id.id, - 'storage_id': directory_id.root_storage_id.id, - 'record_ref': "{},{}".format( - attachment_id.res_model, attachment_id.res_id), - }) - - file_name = self._dms_file_name(save_directory_id, vals['name']) - - self.env['dms.file'].create({ - 'name': file_name, - 'directory_id': save_directory_id.id, - 'attachment_id': attachment_id.id, - 'record_ref': "{},{}".format( - attachment_id.res_model, attachment_id.res_id), - }) - - elif directory_id: - - file_name = self._dms_file_name(directory_id, vals['name']) - - self.env['dms.file'].create({ - 'name': file_name, - 'directory_id': directory_id.id, - 'attachment_id': attachment_id.id, - 'record_ref': "{},{}".format( - attachment_id.res_model, attachment_id.res_id), - }) - - return attachment_id + if "dms_file" in vals and vals["dms_file"]: + del vals["dms_file"] + return super(IrAttachment, self).create(vals) + else: + attachment_id = super(IrAttachment, self).create(vals) + directory_id = self.env["dms.directory"].search( + [("ir_model_id", "=", vals["res_model"])] + ) + + if directory_id and directory_id.record_sub_directory: + save_directory_id = self.env["dms.directory"].search( + [("name", "=", attachment_id.res_name.replace("/", "-"))] + ) + if not save_directory_id: + save_directory_id = self.env["dms.directory"].create( + { + "name": attachment_id.res_name.replace("/", "-"), + "parent_id": directory_id.id, + "storage_id": directory_id.root_storage_id.id, + "record_ref": "{},{}".format( + attachment_id.res_model, attachment_id.res_id + ), + } + ) + + file_name = self._dms_file_name(save_directory_id, vals["name"]) + + self.env["dms.file"].create( + { + "name": file_name, + "directory_id": save_directory_id.id, + "attachment_id": attachment_id.id, + "record_ref": "{},{}".format( + attachment_id.res_model, attachment_id.res_id + ), + } + ) + + elif directory_id: + + file_name = self._dms_file_name(directory_id, vals["name"]) + + self.env["dms.file"].create( + { + "name": file_name, + "directory_id": directory_id.id, + "attachment_id": attachment_id.id, + "record_ref": "{},{}".format( + attachment_id.res_model, attachment_id.res_id + ), + } + ) + + return attachment_id From 52b5e74329c476a782efd351ce00a9b01cefc108 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Tue, 18 Aug 2020 12:37:41 -0300 Subject: [PATCH 09/17] [REF] Rename record field --- dms/models/dms_file.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dms/models/dms_file.py b/dms/models/dms_file.py index 5b9e630e8..9b780b406 100644 --- a/dms/models/dms_file.py +++ b/dms/models/dms_file.py @@ -159,9 +159,7 @@ class File(models.Model): ) record_ref = fields.Reference( - string='Record', - selection='_select_reference', - readonly=True, + string="Record Referenced", selection="_select_reference", readonly=True, ) # ---------------------------------------------------------- From 8d08a28bb05c77705cb2d3ce0eb9ee145f5384eb Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Tue, 18 Aug 2020 12:38:36 -0300 Subject: [PATCH 10/17] [REF] Directory invisible field --- dms/views/directory.xml | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/dms/views/directory.xml b/dms/views/directory.xml index b0421ac41..59871e718 100644 --- a/dms/views/directory.xml +++ b/dms/views/directory.xml @@ -474,9 +474,18 @@ - - - + + + From 4e2de2957c2cbdd76aa33da522b88a258d4275bc Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Tue, 18 Aug 2020 12:41:41 -0300 Subject: [PATCH 11/17] [REF] Formated attachment integration code --- dms/models/dms_file.py | 20 +++++++++----------- dms/models/ir_attachment.py | 7 +++---- dms/models/storage.py | 7 +++++-- dms/views/dms_file.xml | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/dms/models/dms_file.py b/dms/models/dms_file.py index 9b780b406..e9c252de8 100644 --- a/dms/models/dms_file.py +++ b/dms/models/dms_file.py @@ -151,11 +151,11 @@ class File(models.Model): ) attachment_id = fields.Many2one( - comodel_name='ir.attachment', - string='Attachment File', + comodel_name="ir.attachment", + string="Attachment File", prefetch=False, invisible=True, - ondelete="cascade" + ondelete="cascade", ) record_ref = fields.Reference( @@ -185,7 +185,7 @@ def _update_content_vals(self, vals, binary): if self.storage_id.save_type == "file": new_vals["content_file"] = self.content elif self.storage_id.save_type == "attachment": - new_vals['attachment_id'] = self.content + new_vals["attachment_id"] = self.content elif self.storage_id.save_type == "database": new_vals["content_binary"] = self.content and binary return new_vals @@ -380,10 +380,8 @@ def _compute_content(self): elif record.content_binary: record.content = base64.b64encode(record.content_binary) else: - context = {"human_size": True} if bin_size else { - "base64": True} - record.content = \ - record.with_context(context).attachment_id.datas + context = {"human_size": True} if bin_size else {"base64": True} + record.content = record.with_context(context).attachment_id.datas @api.depends("content_binary", "content_file") def _compute_save_type(self): @@ -632,7 +630,7 @@ def unlink(self): @api.model def create(self, vals): - if not "record_ref" in vals: + if "record_ref" not in vals: self._create_model_attachment(vals) return super(File, self).create(vals) @@ -687,5 +685,5 @@ def _compute_locked(self): record.update({"is_locked": False, "is_lock_editor": False}) def _select_reference(self): - model_ids = self.env['ir.model'].search([]) - return [(r['model'], r['name']) for r in model_ids] + [('', '')] + model_ids = self.env["ir.model"].search([]) + return [(r["model"], r["name"]) for r in model_ids] + [("", "")] diff --git a/dms/models/ir_attachment.py b/dms/models/ir_attachment.py index 92735c3ac..a006adaa9 100644 --- a/dms/models/ir_attachment.py +++ b/dms/models/ir_attachment.py @@ -1,6 +1,6 @@ import re -from odoo import api, models, fields, tools +from odoo import api, models class IrAttachment(models.Model): @@ -11,14 +11,13 @@ def _dms_file_name(self, directory_id, file_name): count = 1 for file in directory_id.file_ids: - dms_file_name = re.sub(r' \([0-9]*\)', '', file.name) + dms_file_name = re.sub(r" \([0-9]*\)", "", file.name) if dms_file_name == file_name: count += 1 - file, ext = re.split(r'\.', file_name) + file, ext = re.split(r"\.", file_name) return file + " ({}).".format(count) + ext if count > 1 else file_name - @api.model def create(self, vals): if "dms_file" in vals and vals["dms_file"]: diff --git a/dms/models/storage.py b/dms/models/storage.py index a2cf44580..226dc0365 100644 --- a/dms/models/storage.py +++ b/dms/models/storage.py @@ -22,8 +22,11 @@ class Storage(models.Model): name = fields.Char(string="Name", required=True) save_type = fields.Selection( - selection=[("database", _("Database")), ("file", _("Filestore")), - ("attachment", _("Attachment"))], + selection=[ + ("database", _("Database")), + ("file", _("Filestore")), + ("attachment", _("Attachment")), + ], string="Save Type", default="database", required=True, diff --git a/dms/views/dms_file.xml b/dms/views/dms_file.xml index 8212a1e47..969648f96 100644 --- a/dms/views/dms_file.xml +++ b/dms/views/dms_file.xml @@ -411,7 +411,7 @@ - + From e99f61e0a98de00b602d0b510b26d65a59110531 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Wed, 19 Aug 2020 10:31:58 -0300 Subject: [PATCH 12/17] [ADD] Get directory id by context --- dms/models/dms_file.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dms/models/dms_file.py b/dms/models/dms_file.py index e9c252de8..134506db2 100644 --- a/dms/models/dms_file.py +++ b/dms/models/dms_file.py @@ -578,6 +578,14 @@ def _inverse_content(self): self.browse(ids).write(dict(vals)) def _create_model_attachment(self, vals): + + if "default_directory_id" in self._context: + default_directory_id = self.env["dms.directory"].search( + [("id", "=", self._context["default_directory_id"])] + ) + + vals["directory_id"] = default_directory_id.id + directory_id = self.env["dms.directory"].search( [("id", "=", vals["directory_id"])] ) From b9cb1891b8ffe14f719526496ad7196f0479e9d5 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Wed, 9 Sep 2020 12:20:40 -0300 Subject: [PATCH 13/17] [FIX] Check res_model in Attachment vals --- dms/models/ir_attachment.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dms/models/ir_attachment.py b/dms/models/ir_attachment.py index a006adaa9..4774d80f6 100644 --- a/dms/models/ir_attachment.py +++ b/dms/models/ir_attachment.py @@ -25,9 +25,10 @@ def create(self, vals): return super(IrAttachment, self).create(vals) else: attachment_id = super(IrAttachment, self).create(vals) - directory_id = self.env["dms.directory"].search( - [("ir_model_id", "=", vals["res_model"])] - ) + if 'res_model' in vals: + directory_id = self.env["dms.directory"].search( + [("ir_model_id", "=", vals["res_model"])] + ) if directory_id and directory_id.record_sub_directory: save_directory_id = self.env["dms.directory"].search( From f4ee444ce1640cead4d611170ddbe6df931d47a9 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Thu, 17 Sep 2020 08:25:49 -0300 Subject: [PATCH 14/17] [ADD] Storage related model --- dms/models/storage.py | 2 ++ dms/views/storage.xml | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/dms/models/storage.py b/dms/models/storage.py index 226dc0365..1fd862da9 100644 --- a/dms/models/storage.py +++ b/dms/models/storage.py @@ -84,6 +84,8 @@ class Storage(models.Model): compute="_compute_count_storage_files", string="Count Files" ) + model_id = fields.Many2one("ir.model", auto_join=True) + # ---------------------------------------------------------- # Actions # ---------------------------------------------------------- diff --git a/dms/views/storage.xml b/dms/views/storage.xml index ef6a91c9f..03d588608 100644 --- a/dms/views/storage.xml +++ b/dms/views/storage.xml @@ -169,6 +169,10 @@ + From c743fe87e290af44e57d11b5af33c3c8b7f589c4 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Thu, 17 Sep 2020 08:27:16 -0300 Subject: [PATCH 15/17] [REM] record subdirectory and directory related model --- dms/models/directory.py | 8 -------- dms/views/directory.xml | 8 -------- 2 files changed, 16 deletions(-) diff --git a/dms/models/directory.py b/dms/models/directory.py index e2371b579..7cc662864 100644 --- a/dms/models/directory.py +++ b/dms/models/directory.py @@ -199,14 +199,6 @@ class DmsDirectory(models.Model): """, ) - ir_model_id = fields.Many2one( - comodel_name="ir.model", string="Attachment Model", required=False - ) - - record_sub_directory = fields.Boolean( - string="Create Sub Directory for Record", required=False, default=True, - ) - record_ref = fields.Reference( string="Record Referenced", selection="_select_reference", readonly=True, ) diff --git a/dms/views/directory.xml b/dms/views/directory.xml index 59871e718..268bf4f18 100644 --- a/dms/views/directory.xml +++ b/dms/views/directory.xml @@ -474,14 +474,6 @@ - - Date: Thu, 17 Sep 2020 08:28:43 -0300 Subject: [PATCH 16/17] [REF] Adapt attachment integration with storage --- dms/models/ir_attachment.py | 88 ++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/dms/models/ir_attachment.py b/dms/models/ir_attachment.py index 4774d80f6..043d8001e 100644 --- a/dms/models/ir_attachment.py +++ b/dms/models/ir_attachment.py @@ -18,6 +18,21 @@ def _dms_file_name(self, directory_id, file_name): file, ext = re.split(r"\.", file_name) return file + " ({}).".format(count) + ext if count > 1 else file_name + def _create_record_dir(self, storage_id, attachment_id): + dms_field = self.env["ir.module.module"].search([("name", "=", "dms_field")]) + + directory_fields = { + "name": attachment_id.res_name.replace("/", "-"), + "is_root_directory": True, + "parent_id": False, + "root_storage_id": storage_id.id, + } + + if dms_field and dms_field.state == "installed": + directory_fields["res_id"] = attachment_id.res_id + + return self.env["dms.directory"].create(directory_fields) + @api.model def create(self, vals): if "dms_file" in vals and vals["dms_file"]: @@ -25,53 +40,46 @@ def create(self, vals): return super(IrAttachment, self).create(vals) else: attachment_id = super(IrAttachment, self).create(vals) - if 'res_model' in vals: - directory_id = self.env["dms.directory"].search( - [("ir_model_id", "=", vals["res_model"])] + if "res_model" in vals: + storage_id = self.env["dms.storage"].search( + [("model_id", "=", vals["res_model"])] ) - if directory_id and directory_id.record_sub_directory: - save_directory_id = self.env["dms.directory"].search( - [("name", "=", attachment_id.res_name.replace("/", "-"))] - ) - if not save_directory_id: - save_directory_id = self.env["dms.directory"].create( + if storage_id and storage_id.save_type == "attachment": + record_directory = self.env["dms.directory"].search( + [("complete_name", "=", attachment_id.res_name)] + ) + if not record_directory: + record_directory = self._create_record_dir( + storage_id, attachment_id + ) + + attachments_dir_id = record_directory.child_directory_ids.filtered( + lambda r: r.name == "Attachments" + ) + + if not attachments_dir_id: + attachments_dir_id = self.env["dms.directory"].create( + { + "name": "Attachments", + "parent_id": record_directory.id, + "storage_id": storage_id.id, + "record_ref": "{},{}".format( + attachment_id.res_model, attachment_id.res_id + ), + } + ) + file_name = self._dms_file_name(attachments_dir_id, vals["name"]) + + self.env["dms.file"].create( { - "name": attachment_id.res_name.replace("/", "-"), - "parent_id": directory_id.id, - "storage_id": directory_id.root_storage_id.id, + "name": file_name, + "directory_id": attachments_dir_id.id, + "attachment_id": attachment_id.id, "record_ref": "{},{}".format( attachment_id.res_model, attachment_id.res_id ), } ) - file_name = self._dms_file_name(save_directory_id, vals["name"]) - - self.env["dms.file"].create( - { - "name": file_name, - "directory_id": save_directory_id.id, - "attachment_id": attachment_id.id, - "record_ref": "{},{}".format( - attachment_id.res_model, attachment_id.res_id - ), - } - ) - - elif directory_id: - - file_name = self._dms_file_name(directory_id, vals["name"]) - - self.env["dms.file"].create( - { - "name": file_name, - "directory_id": directory_id.id, - "attachment_id": attachment_id.id, - "record_ref": "{},{}".format( - attachment_id.res_model, attachment_id.res_id - ), - } - ) - return attachment_id From 82772c9ccd2ed555269ac66ce338a4e13aa17c24 Mon Sep 17 00:00:00 2001 From: Luis Malta Date: Mon, 5 Oct 2020 10:28:01 -0300 Subject: [PATCH 17/17] [FIX] Travis permission erro --- dms/models/dms_file.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/dms/models/dms_file.py b/dms/models/dms_file.py index 134506db2..5a3de6046 100644 --- a/dms/models/dms_file.py +++ b/dms/models/dms_file.py @@ -580,14 +580,18 @@ def _inverse_content(self): def _create_model_attachment(self, vals): if "default_directory_id" in self._context: - default_directory_id = self.env["dms.directory"].search( - [("id", "=", self._context["default_directory_id"])] + default_directory_id = ( + self.env["dms.directory"] + .with_user(SUPERUSER_ID) + .browse(self._context["default_directory_id"]) ) vals["directory_id"] = default_directory_id.id - directory_id = self.env["dms.directory"].search( - [("id", "=", vals["directory_id"])] + directory_id = ( + self.env["dms.directory"] + .with_user(SUPERUSER_ID) + .browse(vals["directory_id"]) ) if directory_id and directory_id.record_ref: