-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
53 lines (44 loc) · 2.17 KB
/
flask_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from flask import Flask, render_template, request, redirect, url_for, jsonify
from api import Cleaner, Suggester
from crud import get_config, create_config, update_config
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def hello():
config = get_config()
if not config:
create_config('test', 'test', 'ru', 10)
hello()
if request.method == 'POST':
form_data = request.form
if config.api_key == 'test' or config.api_key == 'test':
return jsonify({'status': 'error', 'message': 'У вас тестовый профиль конфигурации'})
else:
cleaner = Cleaner(config.api_key, config.api_secret)
suggester = Suggester(config.api_key, config.api_secret)
cleaned_address = cleaner.clean('address', form_data['address'])
if cleaned_address:
cleaned_address = cleaned_address[0]['result']
suggestion = suggester.suggest('address', cleaned_address, count=config.max_notes,
language=config.response_language)
if suggestion:
suggestion['status'] = 'ok'
return jsonify(suggestion)
else:
return jsonify({'status': 'error', 'message': 'Ошибка при отправке запроса'})
else:
return jsonify({'status': 'error', 'message': 'Ошибка при отправке запроса'})
return render_template('index.html', addresses=[])
@app.route('/config', methods=['GET', 'POST'])
def config_page():
config = get_config()
if not config:
create_config('test', 'test', 'ru', 10)
hello()
if request.method == 'POST':
form_data = request.form
update_config(**form_data)
return redirect(url_for('config_page'))
return render_template('config.html', api_key=config.api_key, api_secret=config.api_secret,
max_notes=config.max_notes, response_language=config.response_language)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=5000)