summaryrefslogtreecommitdiff
path: root/addons/website_crm/controllers/main.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/website_crm/controllers/main.py
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/website_crm/controllers/main.py')
-rw-r--r--addons/website_crm/controllers/main.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/addons/website_crm/controllers/main.py b/addons/website_crm/controllers/main.py
new file mode 100644
index 00000000..56279553
--- /dev/null
+++ b/addons/website_crm/controllers/main.py
@@ -0,0 +1,69 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import http
+from odoo.http import request
+from odoo.addons.website_form.controllers.main import WebsiteForm
+
+
+class WebsiteForm(WebsiteForm):
+
+ def _get_country(self):
+ country_code = request.session.geoip and request.session.geoip.get('country_code') or False
+ if country_code:
+ return request.env['res.country'].sudo().search([('code', '=', country_code)], limit=1)
+ return request.env['res.country']
+
+ def _get_phone_fields_to_validate(self):
+ return ['phone', 'mobile']
+
+ # Check and insert values from the form on the model <model> + validation phone fields
+ def _handle_website_form(self, model_name, **kwargs):
+ model_record = request.env['ir.model'].sudo().search([('model', '=', model_name), ('website_form_access', '=', True)])
+ if model_record and hasattr(request.env[model_name], 'phone_format'):
+ try:
+ data = self.extract_data(model_record, request.params)
+ except:
+ # no specific management, super will do it
+ pass
+ else:
+ record = data.get('record', {})
+ phone_fields = self._get_phone_fields_to_validate()
+ country = request.env['res.country'].browse(record.get('country_id'))
+ contact_country = country.exists() and country or self._get_country()
+ for phone_field in phone_fields:
+ if not record.get(phone_field):
+ continue
+ number = record[phone_field]
+ fmt_number = request.env[model_name].phone_format(number, contact_country)
+ request.params.update({phone_field: fmt_number})
+
+ if model_name == 'crm.lead' and not request.params.get('state_id'):
+ geoip_country_code = request.session.get('geoip', {}).get('country_code')
+ geoip_state_code = request.session.get('geoip', {}).get('region')
+ if geoip_country_code and geoip_state_code:
+ state = request.env['res.country.state'].search([('code', '=', geoip_state_code), ('country_id.code', '=', geoip_country_code)])
+ if state:
+ request.params['state_id'] = state.id
+ return super(WebsiteForm, self)._handle_website_form(model_name, **kwargs)
+
+ def insert_record(self, request, model, values, custom, meta=None):
+ is_lead_model = model.model == 'crm.lead'
+ if is_lead_model:
+ if 'company_id' not in values:
+ values['company_id'] = request.website.company_id.id
+ lang = request.context.get('lang', False)
+ values['lang_id'] = values.get('lang_id') or request.env['res.lang']._lang_get_id(lang)
+
+ result = super(WebsiteForm, self).insert_record(request, model, values, custom, meta=meta)
+
+ if is_lead_model:
+ visitor_sudo = request.env['website.visitor']._get_visitor_from_request()
+ if visitor_sudo and result:
+ lead_sudo = request.env['crm.lead'].browse(result).sudo()
+ if lead_sudo.exists():
+ vals = {'lead_ids': [(4, result)]}
+ if not visitor_sudo.lead_ids and not visitor_sudo.partner_id:
+ vals['name'] = lead_sudo.contact_name
+ visitor_sudo.write(vals)
+ return result