evolution 2 of email_domain

This commit is contained in:
Benoît Vézina 2023-11-03 15:17:57 -04:00
parent de27e3200e
commit f5976d89ea
7 changed files with 107 additions and 7 deletions

View file

@ -1 +1,2 @@
from . import models
from . import models
from . import controllers

View file

@ -7,10 +7,12 @@
This module allows users to automatically link a partner to a company based on the email domain.
""",
'depends': [
'base'
'base',
'mail'
],
'data': [
'views/res_partner_views.xml'
'views/res_partner_views.xml',
'data/mail_template.xml'
],
'license': 'AGPL-3',
'author': 'Bemade',

View file

@ -0,0 +1 @@
from . import main

View file

@ -0,0 +1,14 @@
from odoo import http
from odoo.http import request
class DivisionCompanyController(http.Controller):
@http.route('/select_division_company', type='http', auth='public', methods=['GET'])
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)
])
if not partner:
return request.render('website.404')

View file

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
<data noupdate="1">
<record id="email_template_division_selection" model="mail.template">
<field name="name">Division Selection: Choose Your Division</field>
<field name="model_id" ref="base.model_res_partner"/>
<field name="email_from">{{object.company_id.email_formatted or user.email_formatted}}</field>
<field name="partner_to">{{object.id}}</field>
<field name="subject">Select Your Division at {{object.company_id.name}}</field>
<field name="body_html" type="html">
<div style="margin: 0px; padding: 0px">
<p style="margin: 0px; padding: 0px; font-size: 13px;">
Hello <t t-out="object.name or ''">Brandon Freeman</t>,
</p>
<p style="margin: 0px; padding: 0px; font-size: 13px;">
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.
</p>
<p style="margin: 0px; padding: 0px; font-size: 13px;">
Please click on the link below to select your division:
</p>
<p style="margin: 16px; padding: 0px; font-size: 13px;">
<a href="{{object.get_division_selection_url()}}" style="text-decoration: none; color: #875A7B;">Choose Your Division</a>
</p>
<p style="margin: 0px; padding: 0px; font-size: 13px;">
If you have any questions or did not register with us, please disregard this email or contact us directly.
</p>
<p style="margin: 0px; padding: 0px; font-size: 13px;">
<br/>Best regards,
</p>
<p style="margin: 0px; padding: 0px; font-size: 13px;">
<br/>The <t t-out="object.company_id.name"/> team.<br/>
<t t-esc="object.company_id.email"/><br/>
<t t-esc="object.company_id.phone"/>
</p>
</div>
</field>
<field name="lang">{{ object.lang }}</field>
<field name="auto_delete" eval="True"/>
</record>
</data>
</odoo>

View file

@ -1,10 +1,34 @@
from odoo import api, fields, models, Command
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')
def _generate_access_token(self):
return uuid.uuid4().hex
def _send_selection_email(self, partner, division_companies):
# Generate a token and save it to the partner
access_token = self._generate_access_token()
partner.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
}
# 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)
@api.onchange('email')
def _check_parent_from_email_domain(self):
@ -26,7 +50,19 @@ 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:
rec.parent_id = company_domain.id
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
else:
rec.parent_id = company_domain.id
return
# If not, drop the part before the first '.' to check the next level

View file

@ -5,9 +5,14 @@
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<field name="email" position="after">
<label for="email_domain" string="Domain"/>
<field name="email_domain"/>
<field name="company_type" position="after">
<label for="domain_identifier" string="Domain identifier" attrs="{'invisible': ['|', ('is_company', '=', False), ('is_subdivision', '=', True)]}"/>
<field name="domain_identifier" attrs="{'invisible': ['|', ('is_company', '=', False), ('is_subdivision', '=', True)]}"/>
<label for="is_subdivision" string="Division" attrs="{'invisible': ['|', ('is_company', '=', False), ('domain_identifier', '=', True)]}"/>
<field name="is_subdivision" attrs="{'invisible': ['|', ('is_company', '=', False), ('domain_identifier', '=', True)]}"/>
</field>
<field name="website" position="before">
<field name="email_domain" attrs="{'invisible': [('is_company', '=', False)]}"/>
</field>
</field>
</record>