From 9a7cc8f5ff9fa315ba38fc7d3477c00b8cabd9fd Mon Sep 17 00:00:00 2001 From: houssine Date: Thu, 21 Nov 2019 22:22:30 +0100 Subject: [PATCH] [REFACT] get mail template from function in order to allow override. --- easy_my_coop/models/account_invoice.py | 10 +++++----- easy_my_coop/models/coop.py | 20 +++++++++++--------- easy_my_coop/models/operation_request.py | 14 ++++++++++---- easy_my_coop_loan/models/loan_issue_line.py | 14 ++++++++++---- 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/easy_my_coop/models/account_invoice.py b/easy_my_coop/models/account_invoice.py index f3731fd..f5a366b 100644 --- a/easy_my_coop/models/account_invoice.py +++ b/easy_my_coop/models/account_invoice.py @@ -46,8 +46,10 @@ class account_invoice(models.Model): def get_mail_template_certificate(self): if self.partner_id.member: - return 'easy_my_coop.email_template_certificat_increase' - return 'easy_my_coop.email_template_certificat' + mail_template = 'easy_my_coop.email_template_certificat_increase' + else: + mail_template = 'easy_my_coop.email_template_certificat' + return self.env.ref(mail_template) def get_sequence_register(self): return self.env.ref('easy_my_coop.sequence_subscription', False) @@ -100,14 +102,12 @@ class account_invoice(models.Model): sub_register_obj = self.env['subscription.register'] share_line_obj = self.env['share.line'] - mail_template_id = self.get_mail_template_certificate() - self.set_membership() sequence_operation = self.get_sequence_operation() sub_reg_operation = sequence_operation.next_by_id() - certificate_email_template = self.env.ref(mail_template_id, False) + certificate_email_template = self.get_mail_template_certificate() for line in self.invoice_line_ids: sub_reg_vals = self.get_subscription_register_vals(line, diff --git a/easy_my_coop/models/coop.py b/easy_my_coop/models/coop.py index a747f31..2dff4a4 100644 --- a/easy_my_coop/models/coop.py +++ b/easy_my_coop/models/coop.py @@ -44,9 +44,10 @@ class SubscriptionRequest(models.Model): def get_mail_template_notif(self, is_company=False): if is_company: - return 'easy_my_coop.email_template_confirmation_company' + mail_template = 'easy_my_coop.email_template_confirmation_company' else: - return 'easy_my_coop.email_template_confirmation' + mail_template = 'easy_my_coop.email_template_confirmation' + return self.env.ref(mail_template, False) def is_member(self, vals, cooperator): if cooperator.member: @@ -57,7 +58,7 @@ class SubscriptionRequest(models.Model): @api.model def create(self, vals): partner_obj = self.env['res.partner'] - mail_template = self.get_mail_template_notif(False) + mail_template_notif = self.get_mail_template_notif(False) if not vals.get('partner_id'): cooperator = False @@ -80,14 +81,13 @@ class SubscriptionRequest(models.Model): cooperator.write({'cooperator': True}) subscr_request = super(SubscriptionRequest, self).create(vals) - confirmation_mail_template = self.env.ref(mail_template, False) - confirmation_mail_template.send_mail(subscr_request.id) + mail_template_notif.send_mail(subscr_request.id) return subscr_request @api.model def create_comp_sub_req(self, vals): - mail_template = self.get_mail_template_notif(True) + confirmation_mail_template = self.get_mail_template_notif(True) vals["name"] = vals['company_name'] if not vals.get('partner_id'): cooperator = self.env['res.partner'].get_cooperator_from_crn(vals.get('company_register_number')) @@ -97,7 +97,6 @@ class SubscriptionRequest(models.Model): vals['already_cooperator'] = True subscr_request = super(SubscriptionRequest, self).create(vals) - confirmation_mail_template = self.env.ref(mail_template, False) confirmation_mail_template.send_mail(subscr_request.id) return subscr_request @@ -401,9 +400,12 @@ class SubscriptionRequest(models.Model): } return res - def send_capital_release_request(self, invoice): + def get_capital_release_mail_template(self): template = 'easy_my_coop.email_template_release_capital' - email_template = self.env.ref(template, False) + return self.env.ref(template, False) + + def send_capital_release_request(self, invoice): + email_template = self.get_capital_release_mail_template() # we send the email with the capital release request in attachment # TODO remove sudo() and give necessary access right diff --git a/easy_my_coop/models/operation_request.py b/easy_my_coop/models/operation_request.py index f4b5ded..27594e8 100644 --- a/easy_my_coop/models/operation_request.py +++ b/easy_my_coop/models/operation_request.py @@ -206,6 +206,14 @@ class operation_request(models.Model): " the information before" " submitting")) + def get_share_trans_mail_template(self): + return self.env.ref('easy_my_coop.email_template_share_transfer', + False) + + def get_share_update_mail_template(self): + return self.env.ref('easy_my_coop.email_template_share_update', + False) + @api.multi def execute_operation(self): self.ensure_one() @@ -300,12 +308,10 @@ class operation_request(models.Model): # send mail to the receiver if rec.operation_type == 'transfer': - mail_template = 'easy_my_coop.email_template_share_transfer' - cert_email_template = self.env.ref(mail_template, False) + cert_email_template = self.get_share_trans_mail_template() cert_email_template.send_mail(rec.partner_id_to.id, False) self.env['subscription.register'].create(values) - mail_template = 'easy_my_coop.email_template_share_update' - cert_email_template = self.env.ref(mail_template, False) + cert_email_template = self.get_share_update_mail_template() cert_email_template.send_mail(rec.partner_id.id, False) diff --git a/easy_my_coop_loan/models/loan_issue_line.py b/easy_my_coop_loan/models/loan_issue_line.py index b0885a7..2d36581 100644 --- a/easy_my_coop_loan/models/loan_issue_line.py +++ b/easy_my_coop_loan/models/loan_issue_line.py @@ -57,10 +57,17 @@ class LoanIssueLine(models.Model): string="Company", readonly=True) + def get_loan_sub_mail_template(self): + return self.env.ref('easy_my_coop_loan.loan_subscription_confirmation', + False) + + def get_loan_pay_req_mail_template(self): + return self.env.ref('easy_my_coop_loan.loan_issue_payment_request', + False) + @api.model def create(self, vals): - mail_template = 'easy_my_coop_loan.loan_subscription_confirmation' - confirmation_mail_template = self.env.ref(mail_template, False) + confirmation_mail_template = self.get_loan_sub_mail_template() line = super(LoanIssueLine, self).create(vals) confirmation_mail_template.send_mail(line.id) @@ -79,8 +86,7 @@ class LoanIssueLine(models.Model): @api.multi def action_request_payment(self): - mail_template = 'easy_my_coop_loan.loan_issue_payment_request' - pay_req_mail_template = self.env.ref(mail_template, False) + pay_req_mail_template = self.get_loan_pay_req_mail_template() for line in self: pay_req_mail_template.send_mail(line.id)