'Entry point for REST API
@param r: the S3Request
@param attr: controller arguments'

def apply_method(self, r, **attr):

    if (r.representation != 'xls'):
        r.error(415, current.ERROR.BAD_FORMAT)
    template_id = r.id
    template = r.record
    if (not template):
        r.error(405, current.ERROR.BAD_METHOD)
    T = current.T
    try:
        import xlwt
    except ImportError:
        r.error(501, T('xlwt    not    installed,    so    cannot    export    as    a    Spreadsheet'))
    s3db = current.s3db
    db = current.db
    table = s3db.survey_translate
    record = db((table.id == self.record_id)).select(table.code, table.language, limitby=(0, 1)).first()
    if (record is None):
        r.error(404, current.ERROR.BAD_RECORD)
    code = record.code
    lang_filename = ('applications/%s/languages/%s.py' % (r.application, code))
    try:
        from gluon.languages import read_dict
        strings = read_dict(lang_filename)
    except IOError:
        strings = {}
    output = StringIO()
    book = xlwt.Workbook(encoding='utf-8')
    sheet = book.add_sheet(record.language)
    question_list = s3db.survey_getAllQuestionsForTemplate(template_id)
    original = {}
    original[template.name] = True
    if (template.description != ''):
        original[template.description] = True
    for qstn in question_list:
        original[qstn['name']] = True
        widget_obj = survey_question_type[qstn['type']](question_id=qstn['qstn_id'])
        if isinstance(widget_obj, S3QuestionTypeOptionWidget):
            option_list = widget_obj.getList()
            for option in option_list:
                original[option] = True
    sections = s3db.survey_getAllSectionsForTemplate(template_id)
    for section in sections:
        original[section['name']] = True
        section_id = section['section_id']
        layout_rules = s3db.survey_getQstnLayoutRules(template_id, section_id)
        layout_str = str(layout_rules)
        posn = layout_str.find('heading')
        while (posn != (-1)):
            start = (posn + 11)
            end = layout_str.find('}', start)
            original[layout_str[start:end]] = True
            posn = layout_str.find('heading', end)
    row = 0
    sheet.write(row, 0, u'Original')
    sheet.write(row, 1, u'Translation')
    original_list = original.keys()
    original_list.sort()
    for text in original_list:
        row += 1
        original = s3_unicode(text)
        sheet.write(row, 0, original)
        if (original in strings):
            sheet.write(row, 1, s3_unicode(strings[original]))
    book.save(output)
    from gluon.contenttype import contenttype
    filename = ('%s.xls' % code)
    headers = current.response.headers
    headers['Content-Type'] = contenttype('.xls')
    headers['Content-disposition'] = ('attachment;    filename="%s"' % filename)
    output.seek(0)
    return output.read()
