[IMP] crm_iap_lead_website: make it compatible with multi-website
The purpose of this commit is to allow to define lead generations rules based on the website visited. Task-ID: 1964025 closes odoo/odoo#33391 Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
This commit is contained in:
@@ -28,6 +28,7 @@ class CRMRevealRule(models.Model):
|
||||
|
||||
# Website Traffic Filter
|
||||
country_ids = fields.Many2many('res.country', string='Countries', help='Only visitors of following countries will be converted into leads/opportunities (using GeoIP).')
|
||||
website_id = fields.Many2one('website', help='Restrict Lead generation to this website.')
|
||||
state_ids = fields.Many2many('res.country.state', string='States', help='Only visitors of following states will be converted into leads/opportunities.')
|
||||
regex_url = fields.Char(string='URL Expression', help='Regex to track website pages. Leave empty to track the entire website, or / to target the homepage. Example: /page* to track all the pages which begin with /page')
|
||||
sequence = fields.Integer(help='Used to order the rules with same URL and countries. '
|
||||
@@ -122,13 +123,15 @@ class CRMRevealRule(models.Model):
|
||||
'rules': [
|
||||
{
|
||||
'id': 0,
|
||||
'url': ***,
|
||||
'regex': ***,
|
||||
'website_id': 1,
|
||||
'country_codes': ['BE', 'US'],
|
||||
'state_codes': [('BE', False), ('US', 'NY'), ('US', 'CA')]
|
||||
},
|
||||
{
|
||||
'id': 1,
|
||||
'url': ***,
|
||||
'regex': ***,
|
||||
'website_id': 1,
|
||||
'country_codes': ['BE'],
|
||||
'state_codes': [('BE', False)]
|
||||
}
|
||||
@@ -159,6 +162,7 @@ class CRMRevealRule(models.Model):
|
||||
rules.append({
|
||||
'id': rule.id,
|
||||
'regex': regex_url,
|
||||
'website_id': rule.website_id.id if rule.website_id else False,
|
||||
'country_codes': countries,
|
||||
'state_codes': states
|
||||
})
|
||||
@@ -178,9 +182,9 @@ class CRMRevealRule(models.Model):
|
||||
country_rules[country].append(rule_index)
|
||||
return country_rules
|
||||
|
||||
def _match_url(self, url, country_code, state_code, rules_excluded):
|
||||
def _match_url(self, website_id, url, country_code, state_code, rules_excluded):
|
||||
"""
|
||||
Return the matching rule based on the country and URL.
|
||||
Return the matching rule based on the country, the website and URL.
|
||||
"""
|
||||
all_rules = self._get_active_rules()
|
||||
rules_id = all_rules['country_rules'].get(country_code, [])
|
||||
@@ -189,6 +193,7 @@ class CRMRevealRule(models.Model):
|
||||
for rule_index in rules_id:
|
||||
rule = all_rules['rules'][rule_index]
|
||||
if ((country_code, state_code) in rule['state_codes'] or (country_code, False) in rule['state_codes'])\
|
||||
and (not rule['website_id'] or rule['website_id'] == website_id)\
|
||||
and str(rule['id']) not in rules_excluded\
|
||||
and re.search(rule['regex'], url):
|
||||
rules_matched.append(rule)
|
||||
|
||||
@@ -40,9 +40,9 @@ class CRMRevealView(models.Model):
|
||||
domain.append(('create_date', '<', fields.Datetime.to_string(datetime.date.today() - relativedelta(weeks=weeks_valid))))
|
||||
self.search(domain).unlink()
|
||||
|
||||
def _create_reveal_view(self, url, ip_address, country_code, state_code, rules_excluded):
|
||||
def _create_reveal_view(self, website_id, url, ip_address, country_code, state_code, rules_excluded):
|
||||
# we are avoiding reveal if reveal_view already created for this IP
|
||||
rules = self.env['crm.reveal.rule']._match_url(url, country_code, state_code, rules_excluded)
|
||||
rules = self.env['crm.reveal.rule']._match_url(website_id, url, country_code, state_code, rules_excluded)
|
||||
if rules:
|
||||
query = """
|
||||
INSERT INTO crm_reveal_view (reveal_ip, reveal_rule_id, reveal_state, create_date)
|
||||
|
||||
@@ -24,9 +24,10 @@ class IrHttp(models.AbstractModel):
|
||||
try:
|
||||
url = request.httprequest.url
|
||||
ip_address = request.httprequest.remote_addr
|
||||
website_id = request.website.id
|
||||
rules_excluded = (request.httprequest.cookies.get('rule_ids') or '').split(',')
|
||||
before = time.time()
|
||||
new_rules_excluded = request.env['crm.reveal.view'].sudo()._create_reveal_view(url, ip_address, country_code, state_code, rules_excluded)
|
||||
new_rules_excluded = request.env['crm.reveal.view'].sudo()._create_reveal_view(website_id, url, ip_address, country_code, state_code, rules_excluded)
|
||||
# even when we match, no view may have been created if this is a duplicate
|
||||
_logger.info('Reveal process time: [%s], match rule: [%s?], country code: [%s], ip: [%s]',
|
||||
time.time() - before, new_rules_excluded == rules_excluded, country_code,
|
||||
|
||||
@@ -36,8 +36,9 @@
|
||||
</group>
|
||||
<group string="Website Traffic Conditions">
|
||||
<field name="country_ids" widget="many2many_tags"/>
|
||||
<field name="website_id" options="{'no_open': True, 'no_create_edit': True}" groups="website.group_multi_website"/>
|
||||
<field name="state_ids" widget="many2many_tags" attrs="{'invisible': [('country_ids', '=', [])]}" domain="[('country_id', 'in', country_ids)]"/>
|
||||
<field name="regex_url" widget="website_urls"/>
|
||||
<field name="regex_url" widget="website_urls" placeholder="e.g. /page"/>
|
||||
<field name="sequence"/>
|
||||
</group>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user