From 13381f9ffa5e50a2f37adccd982f5c435ecb2e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20V=C3=A9zina?= Date: Tue, 7 Nov 2023 08:20:42 -0500 Subject: [PATCH] bemade_partner_email_domain final --- bemade_partner_email_domain/__manifest__.py | 26 ++++- .../controllers/main.py | 40 +++++-- .../data/mail_template.xml | 41 ------- .../data/mail_template_data.xml | 107 ++++++++++++++++++ .../data/template_data.xml | 17 +++ bemade_partner_email_domain/i18n/fr_CA.po | 41 +++++++ .../models/res_partner.py | 31 ++--- .../views/res_partner_views.xml | 10 +- 8 files changed, 237 insertions(+), 76 deletions(-) delete mode 100644 bemade_partner_email_domain/data/mail_template.xml create mode 100644 bemade_partner_email_domain/data/mail_template_data.xml create mode 100644 bemade_partner_email_domain/data/template_data.xml create mode 100644 bemade_partner_email_domain/i18n/fr_CA.po diff --git a/bemade_partner_email_domain/__manifest__.py b/bemade_partner_email_domain/__manifest__.py index 9fe7c77..0e65dc9 100644 --- a/bemade_partner_email_domain/__manifest__.py +++ b/bemade_partner_email_domain/__manifest__.py @@ -1,18 +1,34 @@ { - 'name': 'Partner Email Domain Auto-Set', + 'name': 'Automated Partner Association by Email Domain', 'version': '15.0.0.0.0', 'category': 'Extra Tools', - 'summary': 'Allow to autolink partner based on email domain', + 'summary': 'Automatically associates partners with companies using matching email domains', 'description': """ - This module allows users to automatically link a partner to a company based on the email domain. + This advanced utility module empowers Odoo users with the capacity to automate partner-company associations + based directly on email domains. It helps in eliminating manual effort by establishing a rule of correlation + between the email domain of a partner and the corresponding division or a company. + + The automation proceeds as follows: + + - On creation of partner records, the system will cross-verify the email domain against the companies + in the records. + - If a match is detected, the partner will automatically be linked with that company. + + This module also takes care of instances where there are multiple companies with the same email domain. + In such cases, an email is dispatched to the partner with a selection interface to finalize the association. + + The module is a significant productivity enhancer for businesses that deal with a high number of partners + and require efficient management. """, 'depends': [ 'base', - 'mail' + 'mail', + 'website' ], 'data': [ 'views/res_partner_views.xml', - 'data/mail_template.xml' + 'data/mail_template_data.xml', + 'data/template_data' ], 'license': 'AGPL-3', 'author': 'Bemade', diff --git a/bemade_partner_email_domain/controllers/main.py b/bemade_partner_email_domain/controllers/main.py index 5ed7498..5ed61c7 100644 --- a/bemade_partner_email_domain/controllers/main.py +++ b/bemade_partner_email_domain/controllers/main.py @@ -1,14 +1,40 @@ -from odoo import http -from odoo.http import request +# Import necessary libraries +from odoo import http, _ +#from odoo.http import request + +# Define a controller class that inherits from http.Controller class DivisionCompanyController(http.Controller): - @http.route('/select_division_company', type='http', auth='public', methods=['GET']) + # Decorator to set the routing rules for the method 'select_division_company'. + # The URL for this method will be '/select_division_company' + # It has public access and should be used for HTTP requests of type GET. + @http.route('/select_division_company', website=True, type='http', auth='public') def select_division_company(self, partner_id, access_token, division_id, **kwargs): - partner = request.env['res.partner'].sudo().search([ - ('id', '=', int(partner_id)), - ('access_token', '=', access_token) + + # Search the 'res.partner' model for a record with the given partner_id using Super User access rights + partner = http.request.env['res.partner'].sudo().search([ + ('id', '=', int(partner_id)) ]) + # Check if a partner was found with the given ID if not partner: - return request.render('website.404') \ No newline at end of file + # If no partner found, render an error page with a translatable error message + return http.request.render('bemade_partner_email_domain.error_page', + {'error_message': _('The requested partner is not found.')}) + else: + # If the partner exists and the access token matches, proceed with updating the partner's division + if access_token and access_token == partner.access_token: + # Update the partner's parent with the division_id + partner.write({'parent_id': int(division_id)}) + + # Set a confirmation message signaling that the partner has been associated with the division + confirmation_message = _('Partner %s is associated with the division %s.' % (partner.name, division_id)) + + # Render the error page (which in this case acts as a status page) with the confirmation message. + return http.request.render('bemade_partner_email_domain.error_page', + {'confirmation_message': confirmation_message}) + else: + # If the access token was not provided or does not match, render an error page with an error message + return http.request.render('bemade_partner_email_domain.error_page', + {'error_message': _('No Access Token found or not matching for this partner.')}) \ No newline at end of file diff --git a/bemade_partner_email_domain/data/mail_template.xml b/bemade_partner_email_domain/data/mail_template.xml deleted file mode 100644 index cdf76da..0000000 --- a/bemade_partner_email_domain/data/mail_template.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - Division Selection: Choose Your Division - - {{object.company_id.email_formatted or user.email_formatted}} - {{object.id}} - Select Your Division at {{object.company_id.name}} - -
-

- Hello Brandon Freeman, -

-

- We noticed that you have registered with our company. To provide you with the best possible service, we need you to confirm the division of our company that you are working with. -

-

- Please click on the link below to select your division: -

-

- Choose Your Division -

-

- If you have any questions or did not register with us, please disregard this email or contact us directly. -

-

-
Best regards, -

-

-
The team.
-
- -

-
-
- {{ object.lang }} - -
-
-
diff --git a/bemade_partner_email_domain/data/mail_template_data.xml b/bemade_partner_email_domain/data/mail_template_data.xml new file mode 100644 index 0000000..4ff77c6 --- /dev/null +++ b/bemade_partner_email_domain/data/mail_template_data.xml @@ -0,0 +1,107 @@ + + + + + Division Selection: Choose Your Division + + {{object.company_id.email_formatted or user.email_formatted}} + {{object.id}} + Select Your Division at {{object.company_id.name}} + + + + + + + + + + + +
+ + + + + + + + + + + + + + + +
+ +
+ + + + + + + +
+

Hello Brandon Freeman,

+

We noticed that you have registered with our company by sending an email or using our website. To provide you with the best possible service, we need you to confirm the division of your company that you are working for.

+

Please click on the link below to select your division:

+ +

If you have any questions or did not register with us, please disregard this email or contact us directly.

+

Best regards,

+

The Durpro team.

+
+
+ + + + + + + + + +
+ +
+ + + | + + + | + +
+
+
+ + + + + + +
+ Powered by Odoo +
+
+
+ {{ object.lang }} + +
+
+
+ diff --git a/bemade_partner_email_domain/data/template_data.xml b/bemade_partner_email_domain/data/template_data.xml new file mode 100644 index 0000000..471491e --- /dev/null +++ b/bemade_partner_email_domain/data/template_data.xml @@ -0,0 +1,17 @@ + + + + + \ No newline at end of file diff --git a/bemade_partner_email_domain/i18n/fr_CA.po b/bemade_partner_email_domain/i18n/fr_CA.po new file mode 100644 index 0000000..9f6a648 --- /dev/null +++ b/bemade_partner_email_domain/i18n/fr_CA.po @@ -0,0 +1,41 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * bemade_partner_email_domain +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: it@bemade.org\n" +"Last-Translator: chatgpt\n" +"Language-Team: French (http://www.transifex.com/odoo/odoo-14-0/language/fr_CA/)\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#: model:ir.model.fields,field_description:bemade_partner_email_domain.field_res_partner_access_token +msgid "Access Token" +msgstr "Jeton d'accès" + +#: model:ir.model.fields,field_description:bemade_partner_email_domain.field_res_partner_email_domain +msgid "Email Domain" +msgstr "Domaine de courriel" + +#: model:ir.model.fields,field_description:bemade_partner_email_domain.field_res_partner_is_subdivision +msgid "Subdivision" +msgstr "Subdivision" + +#: code:addons/bemade_partner_email_domain/controllers/main.py:0 +#, python-format +msgid "The requested partner is not found." +msgstr "Le partenaire demandé n'est pas trouvé." + +#: code:addons/bemade_partner_email_domain/controllers/main.py:0 +#, python-format +msgid "No Access Token found or not matching for this partner." +msgstr "Aucun jeton d'accès trouvé ou ne correspondant pas à ce partenaire." + +#: code:addons/bemade_partner_email_domain/controllers/main.py:0 +#, python-format +msgid "Partner %s is associated with the division %s." +msgstr "Le partenaire %s est associé à la division %s." \ No newline at end of file diff --git a/bemade_partner_email_domain/models/res_partner.py b/bemade_partner_email_domain/models/res_partner.py index 3c07918..456af40 100644 --- a/bemade_partner_email_domain/models/res_partner.py +++ b/bemade_partner_email_domain/models/res_partner.py @@ -1,4 +1,5 @@ from odoo import api, fields, models, Command +import uuid from odoo.addons.website.controllers.main import Website @@ -6,7 +7,6 @@ from odoo.addons.website.controllers.main import Website class Partner(models.Model): _inherit = 'res.partner' - domain_identifier = fields.Boolean(string='Domain identifier', default=False) email_domain = fields.Char(string='Email Domain') is_subdivision = fields.Boolean(string='Subdivision', default=False) access_token = fields.Char(string='Access Token') @@ -14,23 +14,25 @@ class Partner(models.Model): def _generate_access_token(self): return uuid.uuid4().hex - def _send_selection_email(self, partner, division_companies): + def _send_selection_email(self, division_companies): + self.ensure_one() # Generate a token and save it to the partner access_token = self._generate_access_token() - partner.write({'access_token': access_token}) + print(f'partner id: {self.id}') + self.write({'access_token': access_token}) # Now include the token in the links base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url') links = { - company.id: f'{base_url}/select_division_company?partner_id={partner.id}&access_token={access_token}&division_id={company.id}' - for company in division_companies + company.name: (f'{base_url}/select_division_company?partner_id={self.id}&access_token={access_token}&division_id={company.id}') for company in division_companies } + print(f'links: {links}') # Render the email template and send the email - template = self.env.ref('bemade_partner_email_domain.mail_template_selection_email') - template.with_context(links=links).send_mail(partner.id, force_send=True) + template = self.env.ref('bemade_partner_email_domain.email_template_select_parent') + template.with_context(links=links).send_mail(self.id, force_send=True) - @api.onchange('email') + # @api.onchange('email') def _check_parent_from_email_domain(self): for rec in self: if rec.parent_id: @@ -50,17 +52,8 @@ class Partner(models.Model): # If the current email domain matches the main domain, return True company_domain = self.env['res.partner'].search([('email_domain', 'ilike', email_domain)]) if company_domain: - if company_domain.domain_identifier: - division_company = self.env['res.partner'].search([('parent_id', '=', company_domain.id), - ('is_company', '=', True)]) - # need to send an email to the new partner asking him to select his parent in the liste - # of company under domain identifier - if division_company: - # Send an email to the partner with the division company selection link - if len(division_company) > 1: - self._send_selection_email(self, division_company) - else: - rec.parent_id = division_company[0].id + if len(company_domain) > 1: + rec._send_selection_email(company_domain) else: rec.parent_id = company_domain.id return diff --git a/bemade_partner_email_domain/views/res_partner_views.xml b/bemade_partner_email_domain/views/res_partner_views.xml index 06bb562..4d8217c 100644 --- a/bemade_partner_email_domain/views/res_partner_views.xml +++ b/bemade_partner_email_domain/views/res_partner_views.xml @@ -6,14 +6,16 @@ -