def survey_build_template_summary(template_id):

    from s3.s3data import S3DataTable
    T = current.T

    table = TABLE(_id="template_summary",
                  _class="dataTable display")
    table_header = TR(TH(T("Position")), TH(T("Section")))
    question_type_list = {}
    posn = 1
    for (key, qtype) in survey_question_type.items():
        if key == "Grid" or key == "GridChild":
            continue
        table_header.append(TH(qtype().type_represent()))
        question_type_list[key] = posn
        posn += 1
    table_header.append(TH(T("Total")))
    header = THEAD(table_header)

    num_of_question_types = len(survey_question_type) - 1 # exclude the grid questions
    questions = survey_getAllQuestionsForTemplate(template_id)
    section_title = ""
    line = []
    body = TBODY()
    section = 0
    total = ["", T("Total")] + [0] * num_of_question_types
    for question in questions:
        if section_title != question["section"]:
            if line != []:
                temp_row = TR()
                for cell in line:
                    temp_row.append(cell)
                body.append(temp_row)
            section += 1
            section_title = question["section"]
            line = [section, section_title] + [0] * num_of_question_types
        if question["type"] == "Grid":
            continue
        if question["type"] == "GridChild":
            # get the real grid question type
            widget_obj = survey_getWidgetFromQuestion(question["qstn_id"])
            question["type"] = widget_obj.typeDescription
        line[question_type_list[question["type"]]+1] += 1
        line[num_of_question_types+1] += 1
        total[question_type_list[question["type"]]+1] += 1
        total[num_of_question_types+1] += 1
    # Add the trailing row
    empty_row = TR()
    for cell in line:
        empty_row.append(cell)
    body.append(empty_row)
    # Add the footer to the table
    foot = TFOOT()
    footer_row = TR()
    for cell in total:
        footer_row.append(TD(B(cell))) # don't use TH() otherwise dataTables will fail
    foot.append(footer_row)

    table.append(header)
    table.append(body)
    table.append(foot)

    # Turn off server side pagination
    s3 = current.response.s3
    s3.no_sspag = True
    s3.no_formats = True

    s3.dataTableID = None
    attr = S3DataTable.getConfigData()
    form = S3DataTable.htmlConfig(table,
                                  "template_summary",
                                  [[0, 'asc']], # order by
                                  "", # the filter string
                                  None, # the rfields
                                  dt_action_col = -1,
                                  **attr
                                  )
    return form

# =============================================================================
Get number of impressions available for line item.
dataset/ETHPy150Open sahana/eden/modules/s3db/survey.py/survey_build_template_summary--crossover with--dataset/ETHPy150Open chartbeat-labs/parselmouth/parselmouth/base.py/Parselmouth.get_line_item_available_inventory