-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
116 lines (82 loc) · 3.29 KB
/
Copy pathapp.py
File metadata and controls
116 lines (82 loc) · 3.29 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import logging
from flask import Flask, request, jsonify, make_response, render_template
from auth import check_odoo_alive, check_odoo_login
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
app.config['SHOW_LOGS'] = os.getenv("SHOW_LOGS")
if app.config['SHOW_LOGS'] == 'True':
proxy_log_file = os.getenv("PROXY_LOG_FILE")
logging.basicConfig(filename=proxy_log_file, level=logging.DEBUG,
format=f'%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s')
@app.route('/')
def index():
message = ""
response = check_odoo_alive()
if type(response) == list and '502' in response[0]:
message += "But, there is an error trying to connect Odoo server: [Bad Gateway - 502], " \
"check if Odoo is running and proxy configurations HOST and/or PORT are correct."
return render_template('home.html', message=message)
@app.route('/api/login/', methods=['POST'])
def login():
"""
Manage app login
:return: True or False
"""
data = request.get_json()
if data['database'] and data['username'] and data['password']:
response = check_odoo_login(data, 'login')
return make_response(jsonify({"message": response[0]}), response[1])
else:
return make_response(jsonify({"message": "Unauthorized - 401"}), 401)
@app.route('/api/call_kw/', methods=['POST'])
def call_kw():
"""
Manage call to methods in Odoo
:return: Response
"""
options = []
try:
data = request.get_json()
if data['host'] and data['port'] and data['database'] and data['username'] \
and data['password']:
response = check_odoo_login(data, 'call_kw')
if 'options' in data:
options = data['options']
if response and data['model'] and data['method']:
result = response.execute(data['model'], data['method'], options)
return jsonify(message="Response success - 200", response=result)
else:
return jsonify(message=response)
else:
return make_response(jsonify(message="Unauthorized - 401"), 401)
except Exception:
return make_response(jsonify(message="Not Implemented - 501"), 501)
def build_logs(file_url):
"""Creates logging information"""
number_logs_lines = int(os.getenv("NUMBER_LOGS_LINES"))
try:
with open(file_url) as f:
logs = f.readlines()[-number_logs_lines:]
return logs
except IOError:
return ['Log file not found, check your configuration file.']
@app.route('/proxy-logs')
def proxy_logs():
"""Returns proxy logging information"""
if app.config['SHOW_LOGS'] == 'True':
log_file = os.getenv("PROXY_LOG_FILE")
return render_template('logs.html', log_type="Proxy", logs_data=build_logs(log_file))
return render_template('no_debug.html')
@app.route('/odoo-logs')
def odoo_logs():
"""Returns odoo logging information"""
if app.config['SHOW_LOGS'] == 'True':
log_file = os.getenv("ODOO_LOG_FILE")
return render_template('logs.html', log_type="Odoo", logs_data=build_logs(log_file))
return render_template('no_debug.html')
if __name__ == "__main__":
app.run(host='0.0.0.0',
debug=True,
port=8080)