[IMP] target is default field, document_template is init-compute field

This commit is contained in:
Stepan Savelyev 2020-01-20 20:08:21 +05:00
parent 021d0ab997
commit b39cfe2a63

View File

@ -10,22 +10,9 @@ from ..utils.docxtpl import get_document_from_values_stream
class ContractWizard(models.TransientModel): class ContractWizard(models.TransientModel):
_name = "res.partner.contract.wizard" _name = "res.partner.contract.wizard"
def _get_default_template(self): def _default_target(self):
template_type = { return "{model},{target_id}".format(
"res.partner.contract": "contract", model=self.active_model, target_id=int(self.env.context.get("self_id"))
"res.partner.contract.annex": "annex",
}.get(self.active_model, False)
company_type = (
self.partner_id.company_form if self.partner_id.is_company else "person"
)
document_template_domain = [
("template_type", "=", template_type),
("company_type", "=", company_type),
]
return self.env["res.partner.document.template"].search(
document_template_domain, limit=1
) )
target = fields.Reference( target = fields.Reference(
@ -34,6 +21,7 @@ class ContractWizard(models.TransientModel):
("res.partner.contract.annex", "Contract Annex"), ("res.partner.contract.annex", "Contract Annex"),
], ],
string="Target", string="Target",
default=_default_target,
) )
company_id = fields.Many2one( company_id = fields.Many2one(
"res.partner", string="Company", compute="_compute_company_id", "res.partner", string="Company", compute="_compute_company_id",
@ -44,7 +32,8 @@ class ContractWizard(models.TransientModel):
document_template = fields.Many2one( document_template = fields.Many2one(
"res.partner.document.template", "res.partner.document.template",
string="Document Template", string="Document Template",
default=_get_default_template, compute="_compute_document_template",
readonly=False,
) )
document_name = fields.Char( document_name = fields.Char(
string="Document Name", compute="_compute_document_name" string="Document Name", compute="_compute_document_name"
@ -79,6 +68,34 @@ class ContractWizard(models.TransientModel):
self.document_template self.document_template
) )
@api.depends('target')
def _compute_document_template(self):
self.document_template = self.env["res.partner.document.template"].search(self._get_template_domain(), limit=1)
@api.onchange('document_template')
def _domain_document_template(self):
return {
"domain": {
"document_template": self._get_template_domain(),
}
}
def _get_template_domain(self):
template_type = {
"res.partner.contract": "contract",
"res.partner.contract.annex": "annex",
}.get(self.active_model, False)
company_type = {
True: self.partner_id.company_form,
False: "person",
}.get(self.partner_id.is_company, False)
document_template_domain = [
("template_type", "=", template_type),
("company_type", "=", company_type),
]
return document_template_domain
@api.onchange("document_template") @api.onchange("document_template")
def _onchange_document_template(self): def _onchange_document_template(self):
"""Creates transient fields for generate contract template """Creates transient fields for generate contract template
@ -90,11 +107,6 @@ class ContractWizard(models.TransientModel):
[("technical_name", "=", technical_name),] [("technical_name", "=", technical_name),]
) )
# A record is the model called from (manually set with context)
self.target = "{model},{target_id}".format(
model=self.active_model, target_id=int(self.env.context.get("self_id"))
)
model_to_action = { model_to_action = {
"res.partner.contract": "action_get_contract_context", "res.partner.contract": "action_get_contract_context",
"res.partner.contract.annex": "action_get_annex_context", "res.partner.contract.annex": "action_get_annex_context",
@ -131,24 +143,6 @@ class ContractWizard(models.TransientModel):
self.transient_field_ids - self.transient_field_ids_hidden self.transient_field_ids - self.transient_field_ids_hidden
) )
# TODO: remove replicate of code
template_type = {
"res.partner.contract": "contract",
"res.partner.contract.annex": "annex",
}.get(self.active_model, False)
company_type = (
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),
],
}
}
@api.multi @api.multi
def get_docx_contract(self): def get_docx_contract(self):
template = self.document_template.attachment_id template = self.document_template.attachment_id