def twitter_search():
    """
       RESTful CRUD controller to add keywords
       for Twitter Search
    """

    tablename = "msg_twitter_search"
    table = s3db[tablename]

    table.is_processed.writable = False
    table.is_searched.writable = False
    table.is_processed.readable = False
    table.is_searched.readable = False

    langs = settings.get_L10n_languages().keys()

    # Tweak languages to those supported by Twitter

    S3Msg = s3base.S3Msg()
    try:
        import tweepy
    except:
        session.error = T("tweepy module not available within the running Python - this needs installing for non-Tropo Twitter support!")
        redirect(URL(c="msg", f="index"))

    twitter_settings = S3Msg.get_twitter_api()
    supported_languages = ['fr', 'en', 'ar', 'ja', 'es', 'de', 'it', 'id', 'pt', 'ko', 'tr', 'ru', 'nl', 'fil',
                       'msa', 'zh-tw', 'zh-cn', 'hi', 'no', 'sv', 'fi', 'da', 'pl', 'hu', 'fa', 'he', 'ur', 'th']
    if twitter_settings:
        twitter_api = twitter_settings[0]

        try:
            supported_languages = map(lambda x: str(x["code"]), twitter_api.supported_languages())
        except (tweepy.TweepError, __HOLE__):
            # List according to Twitter 1.1 API https://dev.twitter.com/docs/api/1.1/get/help/languages
            pass

    substitute_list = {"en-gb": "en",
                       "pt-br": "pt"}

    new_langs = []
    lang_default = current.response.s3.language

    for l in langs:
        if l in supported_languages:
            new_langs.append(l)
        else:
            supported_substitute = substitute_list.get(l)
            if supported_substitute:
                if lang_default == l:
                    lang_default = supported_substitute
                if supported_substitute not in langs:
                    new_langs.append(supported_substitute)
            else:
                if lang_default == l:
                    lang_default = 'en'

    langs = new_langs

    table.lang.requires = IS_IN_SET(langs)
    table.lang.default = lang_default

    comment = "Add the keywords separated by single spaces."
    table.keywords.comment = DIV(_class="tooltip",
                                 _title="%s|%s" % (T("Keywords"),
                                                   T(comment)))

    # CRUD Strings
    s3.crud_strings[tablename] = Storage(
        title_display = T("Twitter Search Queries"),
        title_list = T("Twitter Search Queries"),
        label_create = T("Add Twitter Search Query"),
        title_update = T("Edit Twitter Search Query"),
        label_list_button = T("View Queries"),
        msg_record_created = T("Query added"),
        msg_record_deleted = T("Query deleted"),
        msg_list_empty = T("No Query currently defined"),
        msg_record_modified = T("Query updated")
        )

    if request.post_vars.get("search_after_save"):
        url_after_save = URL(f="twitter_result")
    else:
        url_after_save = None

    s3db.configure(tablename,
                   listadd=True,
                   deletable=True,
                   create_onaccept=action_after_save,
                   create_next=url_after_save
                   )

    def prep(r):
        if r.interactive:
            table = s3db.msg_twitter_channel
            if not db(table.id > 0).select(table.id,
                                           limitby=(0, 1)).first():
                session.error = T("Need to configure Twitter Authentication")
                redirect(URL(f="twitter_channel"))
        return True
    s3.prep = prep

    def postp(r, output):
        if r.interactive:
            # Normal Action Buttons
            s3_action_buttons(r)

            # Custom Action Buttons
            rtable = r.table
            query = (rtable.deleted == False) & \
                    (rtable.is_searched == False)
            records = db(query).select(rtable.id)

            restrict_s = [str(record.id) for record in records]

            query = (rtable.deleted == False) & \
                    (rtable.is_processed == False)
            records = db(query).select(rtable.id)

            restrict_k = [str(record.id) for record in records]

            # @ToDo: Make these S3Methods rather than additional controllers
            from s3 import s3_str
            s3.actions += [dict(label=s3_str(T("Search")),
                                _class="action-btn",
                                url=URL(args=["[id]", "poll"]),
                                        restrict = restrict_s),
                           dict(label=s3_str(T("Analyze with KeyGraph")),
                                _class="action-btn",
                                url = URL(args=["[id]", "keygraph"]),
                                          restrict = restrict_k),
                           ]

            inject_search_after_save(output)

        return output
    s3.postp = postp

    return s3_rest_controller()

# -----------------------------------------------------------------------------
dataset/ETHPy150Open sahana/eden/controllers/msg.py/twitter_search