[IMP] website_crm - cherry pick from 29ad0216e3 - Improve contact_us to fill in all fields from the model crm_lead posted in the form except a blacklist of fields.

This commit is contained in:
Jeremy Kersten 2014-07-03 21:33:53 +02:00
parent a6bf8cd07b
commit 3214dde029
1 changed files with 63 additions and 26 deletions

View File

@ -1,4 +1,7 @@
# -*- coding: utf-8 -*-
import base64
from openerp.tools.translate import _
from openerp.addons.web import http
from openerp.addons.web.http import request
@ -15,42 +18,76 @@ class contactus(http.Controller):
)
return url
@http.route(['/crm/contactus'], type='http', auth="public", website=True, multilang=True)
def contactus(self, description=None, partner_name=None, phone=None, contact_name=None, email_from=None, name=None):
post = {}
post['description'] = description
post['partner_name'] = partner_name
post['phone'] = phone
post['contact_name'] = contact_name
post['email_from'] = email_from
post['name'] = name
@http.route(['/crm/contactus'], type='http', auth="public", website=True)
def contactus(self, **kwargs):
def dict_to_str(title, dictvar):
ret = "\n\n%s" % title
for field in dictvar:
ret += "\n%s" % field
return ret
required_fields = ['contact_name', 'email_from', 'description']
error = set()
values = dict((key, post.get(key)) for key in post)
values['error'] = error
_TECHNICAL = ['show_info'] # Only use for behavior, don't stock it
_BLACKLIST = ['id', 'create_uid', 'create_date', 'write_uid', 'write_date', 'user_id', 'active'] # Allow in description
_REQUIRED = ['name', 'contact_name', 'email_from', 'description'] # Could be improved including required from model
# fields validation
for field in required_fields:
if not post.get(field):
error.add(field)
post_file = [] # List of file to add to ir_attachment once we have the ID
post_description = [] # Info to add after the message
values = {'user_id': False}
lead_model = request.registry['crm.lead']
for field_name, field_value in kwargs.items():
if hasattr(field_value, 'filename'):
post_file.append(field_value)
elif field_name in lead_model._all_columns and field_name not in _BLACKLIST:
values[field_name] = field_value
elif field_name not in _TECHNICAL: # allow to add some free fields or blacklisted field like ID
post_description.append("%s: %s" % (field_name, field_value))
if "name" not in kwargs and values.get("contact_name"): # if kwarg.name is empty, it's an error, we cannot copy the contact_name
values["name"] = values.get("contact_name")
# fields validation : Check that required field from model crm_lead exists
error = set(field for field in _REQUIRED if not kwargs.get(field))
values = dict(values, error=error)
if error:
values.update(kwargs=kwargs.items())
return request.website.render("website.contactus", values)
# if not given: subject is contact name
if not post.get('name'):
post['name'] = post.get('contact_name')
post['user_id'] = False
try:
post['channel_id'] = request.registry['ir.model.data'].get_object_reference(request.cr, SUPERUSER_ID, 'crm', 'crm_case_channel_website')[1]
values['channel_id'] = request.registry['ir.model.data'].get_object_reference(request.cr, SUPERUSER_ID, 'crm', 'crm_case_channel_website')[1]
values['section_id'] = request.registry['ir.model.data'].xmlid_to_res_id(request.cr, SUPERUSER_ID, 'website.salesteam_website_sales')
except ValueError:
pass
request.registry['crm.lead'].create(request.cr, SUPERUSER_ID, post, request.context)
# description is required, so it is always already initialized
if post_description:
values['description'] += dict_to_str(_("Custom Fields: "), post_description)
if kwargs.get("show_info"):
post_description = []
environ = request.httprequest.headers.environ
post_description.append("%s: %s" % ("IP", environ.get("REMOTE_ADDR")))
post_description.append("%s: %s" % ("USER_AGENT", environ.get("HTTP_USER_AGENT")))
post_description.append("%s: %s" % ("ACCEPT_LANGUAGE", environ.get("HTTP_ACCEPT_LANGUAGE")))
post_description.append("%s: %s" % ("REFERER", environ.get("HTTP_REFERER")))
values['description'] += dict_to_str(_("Environ Fields: "), post_description)
lead_id = lead_model.create(request.cr, SUPERUSER_ID, values, request.context)
if lead_id:
for field_value in post_file:
attachment_value = {
'name': field_value.filename,
'res_name': field_value.filename,
'res_model': 'crm.lead',
'res_id': lead_id,
'datas': base64.encodestring(field_value.read()),
'datas_fname': field_value.filename,
}
request.registry['ir.attachment'].create(request.cr, SUPERUSER_ID, attachment_value, context=request.context)
company = request.website.company_id
values = {
'google_map_url': self.generate_google_map_url(company.street, company.city, company.zip, company.country_id and company.country_id.name_get()[0][1] or '')
'google_map_url': self.generate_google_map_url(company.street, company.city, company.zip, company.country_id and company.country_id.name_get()[0][1] or ''),
}
return request.website.render("website_crm.contactus_thanks", values)