Community mailing list archives

community@mail.odoo.com

Qweb templating (Python side) : Access survey_user_input model on survey template using t-field ?

by
Yassine TEIMI
- 11/25/2015 17:59:45
Dears, 

As you know t-field gives fields access via browsable record. 

For the survey module, it's used on survey qweb template. For example, for the survey_init template (the first page of every started survey, describing survey title, and description) here is the code using t-field

<template id="survey_init" name="Survey"> 
    <t t-call="website.layout">
        <div class="wrap">
            <div class="oe_structure" />
                <div class="container">
                    <div groups="base.group_website_publisher" t-ignore="true" class="text-right">
                        <a href="/web#action=survey.action_survey_form" class="btn btn-default">Go back to su                            rveys</a>
                     </div>
                      <div class='jumbotron mt32'>
                         <h1 t-field='survey.title' />
                         <div t-field='survey.description' class="oe_no_empty"/>
                         <a class="btn btn-primary btn-lg" t-att-href="'/survey/fill/%s/%s' % (slug(survey                                ), token)">
                                Start Survey
                            </a>
                         </div>
                    </div>
                <div class="oe_structure" />
            </div>
        </t>
</template>

So how this is can be done ? 

Humble answer : After inspecting the controllers code, it's the start_survey() method that is responsible of launching the survey, it takes the variable survey as a parameter, and send it via data dictionnary trough : website.request.render, wich allows access survey fields value using t-field.

Here is the code part (For one page rendering case "test survey" among others) explainig that : 

 @http.route(['/survey/start/<model("survey.survey"):survey>', 
'/survey/start/<model("survey.survey"):survey>/<string:token>'],

type='http', auth='public', website=True)
def start_survey(self, survey, token=None, **post):
cr, uid, context = request.cr, request.uid, request.context
    survey_obj = request.registry['survey.survey']
    user_input_obj = request.registry['survey.user_input']
    # Test mode
    if token and token == "phantom":
        _logger.info("[survey] Phantom mode")
        user_input_id = user_input_obj.create(cr, uid, {'survey_id': survey.id, 'test_entry': True}, context=context)
        user_input = user_input_obj.browse(cr, uid, [user_input_id], context=context)[0]
        data = {'survey': survey, 'page': None, 'token': user_input.token}
    return request.website.render('survey.survey_init', data)

My question here is how survey is passed as a parameter ? is it by using @route ?

If I know how to pass more parameters to start survey, then may be I can pass a parameter named like "survey_user_input" trough data dictionnary, allowing me to use it on t-field

NB:

I'm calling my survey from hr_evaluation_interview model from the hr_evaluation module.

Here is the method calling survey from hr_evaluation module (maybe I can use it to pass a parameter to start survey via context)  : 

def action_start_survey(self, cr, uid, ids, context=None): 
''' Open the website page with the survey form '''
    trail = ""
    context = dict(context or {}, relative_url=True)
    if 'survey_token' in context:
        trail = "/" + context['survey_token']
    return {
        'type': 'ir.actions.act_url',
        'name': "Start Survey",
        'target': 'self',
        'url': self.read(cr, uid, ids, ['public_url'], context=context)[0]['public_url'] + trail
        }

I hope my question is clear, if not, I'm here for further explainations.

Many thanks for your help.


--

Cordialement

Yassine TEIMI

Consultant & Odoo Project Manager
TEL : +212 6-87 71 41 18