Skip to content

Commit

Permalink
IMP: attachment save check constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
mboscolo committed Oct 7, 2023
1 parent 6a0aede commit 9443d30
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion plm/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
##############################################################################
{
"name": "Product Lifecycle Management",
"version": "16.0.19",
"version": "16.0.20",
"author": "OmniaSolutions",
"website": "https://odooplm.omniasolutions.website",
"category": "Manufacturing/Product Lifecycle Management (PLM)",
Expand Down
34 changes: 23 additions & 11 deletions plm/models/ir_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@
_logger = logging.getLogger(__name__)






def random_name():
random.seed()
d = [random.choice(string.ascii_letters) for _x in range(20)]
Expand All @@ -66,11 +62,12 @@ class IrAttachment(models.Model):

printout = fields.Binary(_('Printout Content'),
help=_("Print PDF content."))
printout_name = fields.Char(_('Printout Name'), compute="_getPrintoutName")
preview = fields.Image(_('Preview Content'),
max_width=1920,
max_height=1920,
attachment=False)

checkout_user = fields.Char(string=_("Checked-Out to"),
compute='_get_checkout_state')
is_checkout = fields.Boolean(_('Is Checked-Out'),
Expand Down Expand Up @@ -107,7 +104,11 @@ class IrAttachment(models.Model):
is_library = fields.Boolean("Is Library file",
default=False)
library_path = fields.Char("File library path")


def _getPrintoutName(self):
for ir_attachment_id in self:
ir_attachment_id.printout_name=f"{ir_attachment_id.engineering_code}_{ir_attachment_id.engineering_revision}.pdf"

def getPrintoutUrl(self):
self.ensure_one()
base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
Expand Down Expand Up @@ -832,15 +833,16 @@ def action_draft(self):
"""
action to be executed for Draft state
"""
return self.commonWFAction(True, START_STATUS, False)
self.commonWFAction(True, START_STATUS, False)
return False


def action_confirm(self):
"""
action to be executed for Confirm state
"""
return self.commonWFAction(False, CONFIRMED_STATUS, False)

self.commonWFAction(False, CONFIRMED_STATUS, False)
return False

def action_release(self):
"""
Expand All @@ -859,7 +861,8 @@ def action_obsolete(self):
"""
obsolete the object
"""
return self.commonWFAction(False, OBSOLATED_STATUS, False)
self.commonWFAction(False, OBSOLATED_STATUS, False)
return False


def action_reactivate(self):
Expand All @@ -869,7 +872,7 @@ def action_reactivate(self):
for attachment_id in self:
if attachment_id.ischecked_in():
attachment_id.with_context(check=False).move_to_state(START_STATUS)
return True
return False


def blindwrite(self, vals):
Expand Down Expand Up @@ -921,6 +924,13 @@ def plm_sanitize(self, vals):
if k in all_keys:
out.append(k)
return out

def _check_unique_document(self, vals):
if self.env.context.get('odooPLM'):
if 'name' in vals and 'engineering_code' in vals:
if self.search_count([('engineering_code','=', vals['engineering_code']),
('name','!=',vals['name'])]):
raise Exception(_(f"You are trying to create a new attachment [{vals['name']}] with the some engineering code [{vals['engineering_code']}]"))

@api.model_create_multi
def create(self, vals):
Expand All @@ -936,6 +946,7 @@ def create(self, vals):
vals_dict['engineering_workflow_user'] = self.env.uid
vals_dict['engineering_workflow_date'] = datetime.now()
to_create_vals.append(vals_dict)
self._check_unique_document(vals_dict)
res = super(IrAttachment, self).create(to_create_vals)
res.with_context(create=True).check_unique()
return res
Expand All @@ -957,6 +968,7 @@ def write(self, vals):
if not self.is_plm_state_writable() and not (self.env.user._is_admin() or self.env.user._is_superuser()):
raise UserError(_("The active state does not allow you to make save action"))
self.writeCheckDatas(vals)
self._check_unique_document(vals)
vals.update(self.checkMany2oneClient(vals))
vals = self.plm_sanitize(vals)
res = super(IrAttachment, self).write(vals)
Expand Down
3 changes: 2 additions & 1 deletion plm/models/ir_attachment_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class PlmDocumentRelations(models.Model):
notes = fields.Char(string="Notes: ")

_sql_constraints = [
('relation_uniq', 'unique (parent_id,child_id,link_kind)', _('The Document Relation must be unique !'))
('relation_uniq', 'unique (parent_id,child_id,link_kind)', _('The Document Relation must be unique !')),
('parent_child_check', 'CHECK (parent_id <> child_id)', _('Parent child product must be different !'))
]

def copy(self, default=None):
Expand Down
3 changes: 2 additions & 1 deletion plm/views/ir_attachment_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@
decoration-muted="engineering_state == 'obsoleted'">

<field name="preview" widget="image" options="{ 'size': [60, 60]}"/>
<field name="printout" widget="binary" filename="name" readonly="True"/>
<field name="printout_name" invisible="True"/>
<field name="printout" widget="binary" filename="printout_name" readonly="True"/>
<field name="name" select="True"/>
<field name="engineering_revision" select="True"/>
<field name="engineering_state" select="True"/>
Expand Down

0 comments on commit 9443d30

Please sign in to comment.