Skip to content

Conversation

nd-dew
Copy link
Contributor

@nd-dew nd-dew commented Jul 15, 2024

[FIX] hr_recruitment: use context lang for partner creation

When partner is created from Applicant's email_from inverse, in a situation when
job application is submitted, we should use context lang for him.

Useful when submiting application for multi-lang website and becuase
partner's lang is used to send appropriate translations of recruitment
templates.

[Reproduce Original Error]

  • Install website,hr_recruitment
  • Set couple of languages on the website (e.g., en_US as default, es_MX as the second language).
  • In incognito, using non-default language, apply for some position (url: /es_MX/jobs )
  • BUG: The email sent to the applicant is in the default language

opw-3984176

@robodoo
Copy link
Contributor

robodoo commented Jul 15, 2024

Pull request status dashboard

@C3POdoo C3POdoo added the OE the report is linked to a support ticket (opw-...) label Jul 15, 2024
@nd-dew nd-dew changed the title [FIX] base: infers partner's lang from content [FIX] base: infers partner's lang from context Jul 16, 2024
@nd-dew nd-dew force-pushed the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch from 2a3b46d to 0674671 Compare July 16, 2024 07:57
@nd-dew nd-dew changed the title [FIX] base: infers partner's lang from context [FIX] base: infers partner's language from context Jul 16, 2024
@nd-dew nd-dew force-pushed the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch from 0674671 to 77f4b70 Compare July 16, 2024 09:52
@nd-dew
Copy link
Contributor Author

nd-dew commented Jul 16, 2024

test_layout_email_lang_template fails because template takes lang from mail.test.lang here:

def render_inline_template(template_instructions, variables):
    results = []
    for string, expression in template_instructions:
        results.append(string)

        if expression:
            result = safe_eval.safe_eval(expression, variables)  <--- HERE lang is taken from the mail.test.lang object
            if result:
                results.append(str(result))

    return ''.join(results)

and adds it to the context here in MailRenderMixin._classify_per_lang:

self.ensure_one()
if self.env.context.get('template_preview_lang'):
lang_to_res_ids = {self.env.context['template_preview_lang']: res_ids}
else:
lang_to_res_ids = {}
for res_id, lang in self._render_lang(res_ids, engine=engine).items():
lang_to_res_ids.setdefault(lang, []).append(res_id)
return dict(
(lang, (self.with_context(lang=lang) if lang else self, lang_res_ids))
for lang, lang_res_ids in lang_to_res_ids.items()

Copy link
Contributor

@nle-odoo nle-odoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me

when there is a parent partner, we are doing in the _onchange_parent_id_for_lang:

self.lang = self.parent_id.lang or self.env.context.get('default_lang') or self.env.lang

(self.env.lang is a shortcut to self.env.context.get('lang') (that also has a check to see if this is a valid language))

so it seems ok to also have a similar fallback when there is no parent partner.

probably we should also handle the default_lang with higher priority than context lang, because currently this could be working like this and people could be using default_lang with the context having another language.

@nle-odoo
Copy link
Contributor

nle-odoo commented Jul 16, 2024

sorry, looking more at it, I see that the lang should already defined in python at:

https://github.com/odoo/odoo/blob/77f4b70552feb536a6edc33f6fc4ad24c5e8d5ff/odoo/addons/base/models/res_partner.py#L188-L189

so I'm not sure how this doesn't work, since we should already fallback on the context lang (self.env.lang) if there is no default_lang in context (that would be in values.get('lang') coming from the super call to default_get.

edit: oh maybe in this flow we don't use the default_get. If it's the case, then maybe:

  • this flow should use (not sure where maybe in website/hr_recruitement/ mail / ...) default_get or set default_lang in context itself
  • or a change like in this PR (in base) should be done

@nd-dew nd-dew force-pushed the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch from dbd0e73 to 3bf513a Compare July 16, 2024 15:39
@nd-dew
Copy link
Contributor Author

nd-dew commented Jul 17, 2024

Experiment: Remove ir_default tz -> Fail

Removing default timezone ir_default table with:

diff --git a/odoo/addons/base/models/res_lang.py b/odoo/addons/base/models/res_lang.py
--- a/odoo/addons/base/models/res_lang.py
+++ b/odoo/addons/base/models/res_lang.py
@@ -202,7 +202,6 @@ class Lang(models.Model):
         IrDefault = self.env['ir.default']
         default_value = IrDefault._get('res.partner', 'lang')
         if default_value is None:
-            IrDefault.set('res.partner', 'lang', lang_code)

... solves the issue, because then default_get will grab language from the context.
BUT too many things depends on the default lang beeing in the ir_default table, so this broke too many tests
Reverting....

@nd-dew nd-dew force-pushed the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch from 3bf513a to a6b6387 Compare July 17, 2024 08:07
@nd-dew
Copy link
Contributor Author

nd-dew commented Jul 17, 2024

Experiment 2: reorder default_get lang -> works but too wide

So it doens't make sense that getting value base default get is first
because afaik it will always have lang from the ir_default. ihmo we
should take higher granuality first, so the context lang, later from
parent, and last from the database

@nd-dew nd-dew force-pushed the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch from a6b6387 to 93cbc5d Compare July 17, 2024 16:49
@nd-dew
Copy link
Contributor Author

nd-dew commented Jul 17, 2024

Narrowing down...

...to hr_recruitment module, to the partner creation from email_from inverse

@nd-dew nd-dew force-pushed the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch 4 times, most recently from 477c411 to 627ec2e Compare July 18, 2024 10:56
@nd-dew nd-dew changed the title [FIX] base: infers partner's language from context [FIX] hr_recruitment: use context lang as defult Jul 18, 2024
@nd-dew nd-dew force-pushed the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch 3 times, most recently from a30035e to f3a63b2 Compare July 24, 2024 15:30
@nd-dew nd-dew changed the title [FIX] hr_recruitment: use context lang as defult [FIX] hr_recruitment: use context lang for partner creation Jul 24, 2024
@nd-dew nd-dew force-pushed the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch from f3a63b2 to dc7de9f Compare July 24, 2024 15:36
@nd-dew nd-dew marked this pull request as ready for review July 25, 2024 11:33
@C3POdoo C3POdoo requested a review from a team July 25, 2024 11:35
Copy link
Contributor

@Bertrand2 Bertrand2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!
The fix seems to work, so aside from my comment, LGTM! :)

Comment on lines 11 to 12
self.env['res.lang']._activate_lang('pl_PL')
self.assertGreaterEqual(len(self.env['res.lang'].get_installed()), 2, "Need more langagues than 'pl_PL' installed")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best that you ensure that more than 2 languages are installed rather than having a check for it, and same for the default as well :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, thanks for having a look ;)

When partner is created from Applicant's email_from inverse, in a situation when
job application is submitted, we should use context lang for him.

Useful when submiting application for multi-lang website and becuase
partner's lang is used to send appropriate translations of recruitment
templates.

[Reproduce Original Error]
- Install website,hr_recruitment
- Set couple of languages on the website (e.g., en_US as default, es_MX as the second language).
- In incognito, using non-default language, apply for some position (url: /es_MX/jobs )
- BUG: The email sent to the applicant is in the default language

opw-3984176
@nd-dew nd-dew force-pushed the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch from dc7de9f to fd6dc9d Compare July 31, 2024 14:58
@nle-odoo
Copy link
Contributor

robodoo delegate+

@Bertrand2
Copy link
Contributor

robodoo r+

robodoo pushed a commit that referenced this pull request Aug 1, 2024
When partner is created from Applicant's email_from inverse, in a situation when
job application is submitted, we should use context lang for him.

Useful when submiting application for multi-lang website and becuase
partner's lang is used to send appropriate translations of recruitment
templates.

[Reproduce Original Error]
- Install website,hr_recruitment
- Set couple of languages on the website (e.g., en_US as default, es_MX as the second language).
- In incognito, using non-default language, apply for some position (url: /es_MX/jobs )
- BUG: The email sent to the applicant is in the default language

opw-3984176

closes #173212

Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
@robodoo robodoo closed this in 27198df Aug 1, 2024
@fw-bot fw-bot deleted the 17.0-opw-3984176-infer_language_on_partner_creation-pian branch August 15, 2024 18:46
Dasanchez1992 pushed a commit to Nosolotec/odoo that referenced this pull request Aug 22, 2024
When partner is created from Applicant's email_from inverse, in a situation when
job application is submitted, we should use context lang for him.

Useful when submiting application for multi-lang website and becuase
partner's lang is used to send appropriate translations of recruitment
templates.

[Reproduce Original Error]
- Install website,hr_recruitment
- Set couple of languages on the website (e.g., en_US as default, es_MX as the second language).
- In incognito, using non-default language, apply for some position (url: /es_MX/jobs )
- BUG: The email sent to the applicant is in the default language

opw-3984176

closes odoo#173212

Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
riccardoSANEHO pushed a commit to resultrum/odoo that referenced this pull request Sep 30, 2024
When partner is created from Applicant's email_from inverse, in a situation when
job application is submitted, we should use context lang for him.

Useful when submiting application for multi-lang website and becuase
partner's lang is used to send appropriate translations of recruitment
templates.

[Reproduce Original Error]
- Install website,hr_recruitment
- Set couple of languages on the website (e.g., en_US as default, es_MX as the second language).
- In incognito, using non-default language, apply for some position (url: /es_MX/jobs )
- BUG: The email sent to the applicant is in the default language

opw-3984176

closes odoo#173212

Signed-off-by: Bertrand Dossogne (bedo) <bedo@odoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OE the report is linked to a support ticket (opw-...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants