Odoo Help


convert survey responses to objects

Tamás Dombos
on 9/2/15, 8:26 PM 712 views

Is there any best practice for using the survey module to input data from clients (who are not users on odoo) and then turn them to various business objects (most importantly contacts, but also employees in some other cases)?

I work for an NGO and we often have various registration forms that vary depending on the event or the activity to which the registration is related to. However, some of the questions (such as name, email, phone number, sometimes bank account) come up in most of them. It would be great, if we did not have to manually copy the data from such survey forms to the relevant business objects. I am considering writing a module that would extend the survey question with a reusable codename field (such as name, email, bank account) write a script that maps these fields to various business objects and then have an automated action run the mapping script when a new response is submitted. 

Has anyone tried something similar? Any other suggestions on how to go about this?

On 9/2/15, 9:02 PM

If you look in module 'website_crm' you will find it does a very similar thing. you can follow same procedure based on your need.  It creates leads from contact form, code looks like:

@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

        _TECHNICAL = ['show_info', 'view_from', 'view_callback']  # 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

        post_file = []  # List of file to add to ir_attachment once we have the ID
        post_description = []  # Info to add after the message
        values = {}
        values['medium_id'] = request.registry['ir.model.data'].xmlid_to_res_id(request.cr, SUPERUSER_ID, 'crm.crm_medium_website')
        values['section_id'] = request.registry['ir.model.data'].xmlid_to_res_id(request.cr, SUPERUSER_ID, 'website.salesteam_website_sales')

        for field_name, field_value in kwargs.items():
            if hasattr(field_value, 'filename'):
            elif field_name in request.registry['crm.lead']._fields 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 values.get(field))

        if error:
            values = dict(values, error=error, kwargs=kwargs.items())
            return request.website.render(kwargs.get("view_from", "website.contactus"), values)

        # 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)
        if kwargs.get('feedback') == 'i':
            lead_id = self.create_lead(request, dict(values, user_id=False), kwargs)
            lead_id = self.create_claim(request, dict(values, user_id=False), kwargs)
        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)

        return self.get_contactus_response(values, kwargs)

As far as I understand, the suggestion you gave was to forget altogether about the survey module, and build website forms. This, however, means that every time a new registration form is needed, some programming has to go into it. I was looking for a solution where a simple user can create a registration form (via the survey module), and then the system can process those common fields that correspond to fields in business objects.

Tamás Dombos
on 9/2/15, 9:07 PM

Not at all you understood it wrong, I would have suggested that only if your form was fixed, as you asked "Has anyone tried something similar? Any other suggestions on how to go about this?" So I told you that similar thing is done in contact form in 'website_crm' Note: I have pointed you code for you can look how that is done. I never said that it is existing you can use contact form.

on 9/2/15, 9:24 PM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 9/2/15, 8:26 PM
Seen: 712 times
Last updated: 9/2/15, 9:02 PM