Exemplo n.º 1
0
def do_search() -> 'html':
    phrase = request.form['phrase']
    letters = request.form['letters']
    results = str(search_for_letters(phrase, letters))
    return render_template(
        'results.html',
        the_title=webapp_title,
        the_phrase=phrase,
        the_letters=letters,
        the_results=results,
    )
Exemplo n.º 2
0
def do_search() -> 'html':
    @copy_current_request_context  # only works in function scope
    def log_request(req: 'flask_request', res: str) -> None:
        '''Log details of the web request and the results'''
        try:
            with UseDatabase(**dbconfig) as cursor:
                cursor.execute(
                    log_insert_query,
                    (
                        req.form['phrase'],
                        req.form['letters'],
                        req.remote_addr,
                        req.user_agent.browser,
                        res,
                    ),
                )
        except DBError as err:
            print('[log_request] DB Error: {}'.format(str(err)))
        except Exception as err:
            print('[log_request] Unhandled Error: {}'.format(str(err)))

    phrase = request.form['phrase']
    letters = request.form['letters']
    results = str(search_for_letters(phrase, letters))

    # log_request is done with thread
    th = Thread(
        target=log_request,
        args=(request, results),
    )
    th.start()

    return render_template(
        'results.html',
        the_title=webapp_title,
        the_phrase=phrase,
        the_letters=letters,
        the_results=results,
    )