From cfe0fb0d7a704b364582005be58b7e4153a68e5c Mon Sep 17 00:00:00 2001 From: Stepan Savelyev Date: Fri, 17 Jan 2020 16:54:33 +0500 Subject: [PATCH] [IMP] generate filename of document --- models/res_partner_contract.py | 7 +++++++ models/res_partner_contract_annex.py | 16 ++++++++++++++++ wizard/res_partner_contract_wizard.py | 27 +-------------------------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/models/res_partner_contract.py b/models/res_partner_contract.py index 736d148..0bb7b97 100644 --- a/models/res_partner_contract.py +++ b/models/res_partner_contract.py @@ -105,6 +105,13 @@ class PartnerContract(models.Model, Extension): "context": {"self_id": self.id}, } + def get_filename_by_document_template(self, document_template_id): + return _("{type} {number} from {date}").format( + type=_(dict(document_template_id._fields['document_type'].selection).get(document_template_id.document_type)), + number=self.name, + date=self.get_date().strftime("%d.%m.%Y"), + ) + def get_date(self): """Uses in xml action (data/fields_default) diff --git a/models/res_partner_contract_annex.py b/models/res_partner_contract_annex.py index 8e56f59..3cadda4 100644 --- a/models/res_partner_contract_annex.py +++ b/models/res_partner_contract_annex.py @@ -85,6 +85,22 @@ class ContractOrderAnnex(models.Model, Extension): "context": {"self_id": self.id}, } + def get_filename_by_document_template(self, document_template_id): + return "{type} №{name}".format( + type=_(dict(document_template_id._fields['document_type'].selection).get(document_template_id.document_type)), + name={ + "bill": "{number} {type} {name}", + "specification": "{number} {type} {name}", + "approval_list": "{number}.1 {type} {name}-1", + "act_at": "{number}.2 {type} {name}-2", + "act_ad": "{number}.3 {type} {name}-3", + }.get(document_template_id.document_type_name).format( + number=self.number, + type=_(dict(document_template_id._fields['document_type_name'].selection).get(document_template_id.document_type_name)), + name=self.name, + ) + ) + def modf(self, arg): """Math.modf function for using in XML ir.action.server code Uses in data/fields_default.xml diff --git a/wizard/res_partner_contract_wizard.py b/wizard/res_partner_contract_wizard.py index 0accc9e..20c96ba 100644 --- a/wizard/res_partner_contract_wizard.py +++ b/wizard/res_partner_contract_wizard.py @@ -176,32 +176,7 @@ class ContractWizard(models.TransientModel): binary_data = get_document_from_values_stream(path_to_template, fields).read() encoded_data = base64.b64encode(binary_data) - # TODO: bad - if self.target._name == "res.partner.contract": - contract = self.target - attachment_name = _("{type} {number} from {date}").format( - type=_(dict(self.document_template._fields['document_type'].selection).get(self.document_template.document_type)), - number=contract.name, - date=contract.get_date().strftime("%d.%m.%Y"), - ) - elif self.target._name == "res.partner.contract.annex": - annex = self.target - attachment_name = "{type} №{name}".format( - type=_(dict(self.document_template._fields['document_type'].selection).get(self.document_template.document_type)), - name={ - "specification": "{number} {type} {name}", - "approval_list": "{number}.1 {type} {name}-1", - "act_at": "{number}.2 {type} {name}-2", - "act_ad": "{number}.3 {type} {name}-3", - }.get(self.document_template.document_type_name).format( - number=annex.number, - type=_(dict(self.document_template._fields['document_type_name'].selection).get(self.document_template.document_type_name)), - name=annex.name, - ) - ) - else: - attachment_name = "Unknown" - + attachment_name = self.target.get_filename_by_document_template(self.document_template) or "Unknown" attachment_name = "{}.docx".format(attachment_name) document_as_attachment = self.env["ir.attachment"].create(