Files
odoo_source/addons/website_crm/controllers/main.py
T
David Beguin 3977e28baf [MOV-IMP] crm : move crm.lead.lang_id from enterprise to community
Since introduction of website_visitor,
we don't need to identifiy the lead with a lead_id cookie.
The visitor is identified and if a lead is created with the
contact form, the lead is created and linked to the visitor.

To avoid losing data, each time a visitor send a contact form,
it creates a new lead. All those leads are linked between them
via the website_visitor.

As the website_visitor already has the lang set on itself,
there is no reason to keep the lang_id of crm_lead at enterprise
side. So that lang_id on crm.lead has been moved to community.

This commit is directly linked to another one in enterprise
See https://github.com/odoo/enterprise/pull/4834 to get more details.

Task ID : 2028059
PR #34624
2019-08-19 06:34:20 +00:00

68 lines
3.4 KiB
Python

# -*- 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
@http.route('/website_form/<string:model_name>', type='http', auth="public", methods=['POST'], website=True)
def 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']
request.params['state_id'] = State.search([('code', '=', geoip_state_code), ('country_id.code', '=', geoip_country_code)]).id
return super(WebsiteForm, self).website_form(model_name, **kwargs)
def insert_record(self, request, model, values, custom, meta=None):
if model.model == 'crm.lead':
if 'company_id' not in values:
values['company_id'] = request.website.company_id.id
lang = request.context.get('lang', False)
lang_id = request.env["res.lang"].sudo().search([('code', '=', lang)], limit=1).id
values['lang_id'] = lang_id
result = super(WebsiteForm, self).insert_record(request, model, values, custom, meta=meta)
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.user_partner_id:
vals['name'] = request.env['crm.lead'].browse(result).sudo().contact_name
visitor_sudo.write(vals)
return result