[IMP] generate filename of document

This commit is contained in:
Stepan Savelyev 2020-01-17 16:54:33 +05:00
parent 37228919db
commit cfe0fb0d7a
3 changed files with 24 additions and 26 deletions

View File

@ -105,6 +105,13 @@ class PartnerContract(models.Model, Extension):
"context": {"self_id": self.id}, "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): def get_date(self):
"""Uses in xml action (data/fields_default) """Uses in xml action (data/fields_default)

View File

@ -85,6 +85,22 @@ class ContractOrderAnnex(models.Model, Extension):
"context": {"self_id": self.id}, "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): def modf(self, arg):
"""Math.modf function for using in XML ir.action.server code """Math.modf function for using in XML ir.action.server code
Uses in data/fields_default.xml Uses in data/fields_default.xml

View File

@ -176,32 +176,7 @@ class ContractWizard(models.TransientModel):
binary_data = get_document_from_values_stream(path_to_template, fields).read() binary_data = get_document_from_values_stream(path_to_template, fields).read()
encoded_data = base64.b64encode(binary_data) encoded_data = base64.b64encode(binary_data)
# TODO: bad attachment_name = self.target.get_filename_by_document_template(self.document_template) or "Unknown"
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 = "{}.docx".format(attachment_name) attachment_name = "{}.docx".format(attachment_name)
document_as_attachment = self.env["ir.attachment"].create( document_as_attachment = self.env["ir.attachment"].create(