[STYLE] black

This commit is contained in:
Stepan Savelyev 2020-01-20 18:48:12 +05:00
parent b2ff60057d
commit 5b4c0b2876
5 changed files with 79 additions and 38 deletions

View File

@ -110,7 +110,11 @@ class PartnerContract(models.Model, IDocument, Extension):
def get_filename_by_document_template(self, document_template_id): def get_filename_by_document_template(self, document_template_id):
return _("{type} {number} from {date}").format( return _("{type} {number} from {date}").format(
type=_(dict(document_template_id._fields['document_type'].selection).get(document_template_id.document_type)), type=_(
dict(document_template_id._fields["document_type"].selection).get(
document_template_id.document_type
)
),
number=self.name, number=self.name,
date=self.get_date().strftime("%d.%m.%Y"), date=self.get_date().strftime("%d.%m.%Y"),
) )

View File

@ -27,7 +27,7 @@ class ContractOrderAnnex(models.Model, IDocument, Extension):
date_conclusion = fields.Date( date_conclusion = fields.Date(
string="Conclusion Date", default=fields.Date.today(), string="Conclusion Date", default=fields.Date.today(),
) )
number = fields.Integer(string="",help="Counter of Contract Annexes") number = fields.Integer(string="", help="Counter of Contract Annexes")
development_period = fields.Integer("Product Development Period (days)",) development_period = fields.Integer("Product Development Period (days)",)
@ -70,12 +70,14 @@ class ContractOrderAnnex(models.Model, IDocument, Extension):
} }
@api.multi @api.multi
@api.depends('name') @api.depends("name")
def _compute_display_name(self): def _compute_display_name(self):
for record in self: for record in self:
record.display_name = "{} {}".format(record.number or record.contract_id.contract_annex_number, record.name) record.display_name = "{} {}".format(
record.number or record.contract_id.contract_annex_number, record.name
)
@api.depends('specification_name', 'contract_id', 'order_id') @api.depends("specification_name", "contract_id", "order_id")
def _compute_specification_name(self): def _compute_specification_name(self):
self.specification_name = _("{name} from {date}").format( self.specification_name = _("{name} from {date}").format(
name="{}-{}".format(self.contract_id.name, self.order_id.name), name="{}-{}".format(self.contract_id.name, self.order_id.name),
@ -111,30 +113,41 @@ class ContractOrderAnnex(models.Model, IDocument, Extension):
} }
def get_name_by_document_template(self, document_template_id): def get_name_by_document_template(self, document_template_id):
return { return (
{
"specification": "{number} {name}", "specification": "{number} {name}",
"approval_list": "{number}.1 {name}-1", "approval_list": "{number}.1 {name}-1",
"act_at": "{number}.2 {name}-2", "act_at": "{number}.2 {name}-2",
"act_ad": "{number}.3 {name}-3", "act_ad": "{number}.3 {name}-3",
}.get(document_template_id.document_type_name, "Unknown").format( }
number=self.number, .get(document_template_id.document_type_name, "Unknown")
name=self.name, .format(number=self.number, name=self.name,)
) )
def get_filename_by_document_template(self, document_template_id): def get_filename_by_document_template(self, document_template_id):
return "{type}{name}".format( return "{type}{name}".format(
type=_(dict(document_template_id._fields['document_type'].selection).get(document_template_id.document_type)), type=_(
dict(document_template_id._fields["document_type"].selection).get(
document_template_id.document_type
)
),
name={ name={
"bill": "{number} {type} {name}", "bill": "{number} {type} {name}",
"specification": "{number} {type} {name}", "specification": "{number} {type} {name}",
"approval_list": "{number}.1 {type} {name}-1", "approval_list": "{number}.1 {type} {name}-1",
"act_at": "{number}.2 {type} {name}-2", "act_at": "{number}.2 {type} {name}-2",
"act_ad": "{number}.3 {type} {name}-3", "act_ad": "{number}.3 {type} {name}-3",
}.get(document_template_id.document_type_name).format( }
.get(document_template_id.document_type_name)
.format(
number=self.number, number=self.number,
type=_(dict(document_template_id._fields['document_type_name'].selection).get(document_template_id.document_type_name)), type=_(
dict(
document_template_id._fields["document_type_name"].selection
).get(document_template_id.document_type_name)
),
name=self.name, name=self.name,
) ),
) )
def modf(self, arg): def modf(self, arg):

View File

@ -8,7 +8,10 @@ class DocumentTemplate(models.Model):
name = fields.Char() name = fields.Char()
attachment_id = fields.Many2one( attachment_id = fields.Many2one(
"ir.attachment", string="Template Attachment", ondelete="cascade", required=True, "ir.attachment",
string="Template Attachment",
ondelete="cascade",
required=True,
) )
document_type = fields.Selection( document_type = fields.Selection(
string="Type of document", string="Type of document",
@ -16,7 +19,7 @@ class DocumentTemplate(models.Model):
("contract", _("Contract")), ("contract", _("Contract")),
("annex", _("Annex")), ("annex", _("Annex")),
("addition", _("Addition")), ("addition", _("Addition")),
] ],
) )
document_type_name = fields.Selection( document_type_name = fields.Selection(
string="Document", string="Document",
@ -26,7 +29,7 @@ class DocumentTemplate(models.Model):
("approval_list", _("Approval List")), ("approval_list", _("Approval List")),
("act_at", _("Act of Acceptance and Transfer")), ("act_at", _("Act of Acceptance and Transfer")),
("act_ad", _("Act of Acceptance and Delivery")), ("act_ad", _("Act of Acceptance and Delivery")),
] ],
) )
company_type = fields.Selection( company_type = fields.Selection(
selection=[ selection=[

View File

@ -9,14 +9,17 @@ class IDocument(object):
"""Class must be used as an interface for create new document based model""" """Class must be used as an interface for create new document based model"""
def get_name_by_document_template(self, document_template_id: fields.Many2one): def get_name_by_document_template(self, document_template_id: fields.Many2one):
raise NotImplementedError('Method {} is not implemented'.format(inspect.currentframe().f_code.co_name)) raise NotImplementedError(
"Method {} is not implemented".format(inspect.currentframe().f_code.co_name)
)
def get_filename_by_document_template(self, document_template_id: fields.Many2one): def get_filename_by_document_template(self, document_template_id: fields.Many2one):
raise NotImplementedError('Method {} is not implemented'.format(inspect.currentframe().f_code.co_name)) raise NotImplementedError(
"Method {} is not implemented".format(inspect.currentframe().f_code.co_name)
)
class Extension(object): class Extension(object):
def parse_odoo_date(self, date: str): def parse_odoo_date(self, date: str):
return dt.datetime.strptime(date, DEFAULT_SERVER_DATE_FORMAT) return dt.datetime.strptime(date, DEFAULT_SERVER_DATE_FORMAT)

View File

@ -24,9 +24,7 @@ class ContractWizard(models.TransientModel):
"res.partner.contract": "contract", "res.partner.contract": "contract",
"res.partner.contract.annex": "annex", "res.partner.contract.annex": "annex",
}.get(self.active_model, False) }.get(self.active_model, False)
company_type = ( company_type = partner_id.company_form if partner_id.is_company else "person"
partner_id.company_form if partner_id.is_company else "person"
)
document_template_domain = [ document_template_domain = [
("template_type", "=", template_type), ("template_type", "=", template_type),
@ -45,11 +43,17 @@ class ContractWizard(models.TransientModel):
string="Target", string="Target",
) )
company_id = fields.Many2one("res.partner", string="Company") company_id = fields.Many2one("res.partner", string="Company")
partner_id = fields.Many2one("res.partner", string="Partner", default=_get_default_partner) partner_id = fields.Many2one(
document_template = fields.Many2one( "res.partner", string="Partner", default=_get_default_partner
"res.partner.document.template", string="Document Template", default=_get_default_template, )
document_template = fields.Many2one(
"res.partner.document.template",
string="Document Template",
default=_get_default_template,
)
document_name = fields.Char(
string="Document Name", compute="_compute_document_name"
) )
document_name = fields.Char(string="Document Name", compute='_compute_document_name')
transient_field_ids = fields.One2many( transient_field_ids = fields.One2many(
"res.partner.contract.field.transient", "res.partner.contract.field.transient",
"_contract_wizard_id", "_contract_wizard_id",
@ -64,9 +68,11 @@ class ContractWizard(models.TransientModel):
if not self.document_template: if not self.document_template:
raise ValidationError("You did not set up the template...") raise ValidationError("You did not set up the template...")
@api.depends('document_name', 'document_template', 'target') @api.depends("document_name", "document_template", "target")
def _compute_document_name(self): def _compute_document_name(self):
self.document_name = self.target.get_name_by_document_template(self.document_template) self.document_name = self.target.get_name_by_document_template(
self.document_template
)
@api.onchange("document_template") @api.onchange("document_template")
def _onchange_document_template(self): def _onchange_document_template(self):
@ -143,7 +149,14 @@ class ContractWizard(models.TransientModel):
self.partner_id.company_form if self.partner_id.is_company else "person" self.partner_id.company_form if self.partner_id.is_company else "person"
) )
return {"domain": {"document_template": [("template_type", "=", template_type),("company_type", "=", company_type),],}} return {
"domain": {
"document_template": [
("template_type", "=", template_type),
("company_type", "=", company_type),
],
}
}
@api.multi @api.multi
def get_docx_contract(self): def get_docx_contract(self):
@ -159,15 +172,20 @@ class ContractWizard(models.TransientModel):
if transient_field.technical_name and transient_field.value if transient_field.technical_name and transient_field.value
} }
if self.target._name == "res.partner.contract.annex": if self.target._name == "res.partner.contract.annex":
fields.update({ fields.update(
{
"annex_name": self.document_name, "annex_name": self.document_name,
"specification_name": self.target.specification_name, "specification_name": self.target.specification_name,
}) }
)
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)
attachment_name = self.target.get_filename_by_document_template(self.document_template) or "Unknown" attachment_name = (
self.target.get_filename_by_document_template(self.document_template)
or "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(