forked from cristian-g/vsf-odoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
80 lines (75 loc) · 2.51 KB
/
common.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
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
import ast
import json
import werkzeug.wrappers
host = "http://localhost:8069"
category_offset = 2
size_attribute_name = "Talla"
color_attribute_name = "Color"
def valid_response(data, status=200):
"""Valid response - This will be returned when the http request is successfully processed."""
hits = []
for hit in data:
hits.append({
"_id": str(hit.get("id")),
'_source': hit,
})
data = {
'total': len(data),
'hits': {
'hits': data,
'max_score': None,
'total': len(data),
},
'total': len(data),
'took': 0,
'start': 0,
'perPage': len(data)
}
return werkzeug.wrappers.Response(
status=status,
content_type='application/json; charset=utf-8',
headers=[
('Access-Control-Allow-Origin', '*'),
('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'),
('Access-Control-Allow-Headers', 'CONTENT-TYPE'),
('Access-Control-Allow-Methods', 'GET'),
],
response=json.dumps(data),
)
def simple_response(data, status=200):
return werkzeug.wrappers.Response(
status=status,
content_type='application/json; charset=utf-8',
headers=[
('Access-Control-Allow-Origin', '*'),
('Access-Control-Allow-Methods', 'GET, POST, OPTIONS'),
('Access-Control-Allow-Headers', 'CONTENT-TYPE'),
('Access-Control-Allow-Methods', 'GET'),
],
response=json.dumps(data),
)
def invalid_response(typ, message=None, status=400):
"""Invalid response - This will be the returned value whenever the server runs into an error
either from the client or the server."""
return werkzeug.wrappers.Response(
status=status,
content_type='application/json; charset=utf-8',
response=json.dumps({
'type': typ,
'message': message if message else 'wrong arguments (missing validation)',
}),
)
def extract_arguments(payload, offset=0, limit=0, order=None):
"""."""
fields, domain = [], []
if payload.get('domain'):
domain += ast.literal_eval(payload.get('domain'))
if payload.get('fields'):
fields += ast.literal_eval(payload.get('fields'))
if payload.get('offset'):
offset = int(payload['offset'])
if payload.get('limit'):
limit = int(payload['limit'])
if payload.get('order'):
order = payload.get('order')
return [domain, fields, offset, limit, order]