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
117
118
119
120
121
122
123
124
125
126
|
from .. import controller
from odoo import http
from odoo.http import request
class Product(controller.Controller):
prefix = '/api/v1/'
@http.route(prefix + 'product', auth='public', methods=['GET'])
def get_product(self, **kw):
if not self.authenticate():
return self.response(code=401, description='Unauthorized')
name = kw.get('name')
manufactures = kw.get('manufactures')
categories = kw.get('categories')
require_betweens = ['name', 'manufactures', 'categories']
is_fulfill = False
for required in require_betweens:
if kw.get(required):
is_fulfill = True
# If not fulfill in require_between
if not is_fulfill:
return self.response(code=400, description='name or manufactures or categories is required')
query = [('sale_ok', '=', True)]
if name:
name = '%' + name.replace(' ', '%') + '%'
query += [
'|',
('default_code', 'ilike', name),
('name', 'ilike', name),
]
if manufactures:
query.append(('x_manufacture', 'in', [int(x) for x in manufactures.split(',')]))
if categories:
query.append(('public_categ_ids', 'child_of', [int(x) for x in categories.split(',')]))
price_from = kw.get('price_from')
if price_from and int(price_from):
query.append(('web_price_sorting', '>=', int(price_from)))
price_to = kw.get('price_to')
if price_to and int(price_to):
query.append(('web_price_sorting', '<=', int(price_to)))
product_variants = request.env['product.product'].search(query)
product_variant_ids = [x.id for x in product_variants]
query = [('product_variant_ids', 'in', product_variant_ids)]
limit = int(kw.get('limit', 0))
offset = int(kw.get('offset', 0))
order = self.get_product_default_order(kw.get('order'))
product_templates = request.env['product.template'].search(query, limit=limit, offset=offset, order=order)
data = {
'product_total': request.env['product.template'].search_count(query),
'products': [request.env['product.template'].api_single_response(x) for x in product_templates]
}
return self.response(data)
@http.route(prefix + 'product/<id>', auth='public', methods=['GET'])
def get_product_by_id(self, **kw):
if not self.authenticate():
return self.response(code=401, description='Unauthorized')
id = kw.get('id')
if not id:
return self.response(code=400, description='id is required')
data = []
id = [int(x) for x in id.split(',')]
product_templates = request.env['product.template'].search([('id', 'in', id)])
if product_templates:
data = [request.env['product.template'].api_single_response(x, with_detail=True) for x in product_templates]
return self.response(data)
@http.route(prefix + 'product/<id>/similar', auth='public', methods=['GET'])
def get_product_similar_by_id(self, **kw):
if not self.authenticate():
return self.response(code=401, description='Unauthorized')
id = kw.get('id')
if not id:
return self.response(code=400, description='id is required')
id = int(id)
product_template = request.env['product.template'].search([('id', '=', id)])
if not product_template: return self.response([])
query = [('id', '!=', id)]
if product_template.x_manufacture:
query.append(('x_manufacture', '=', product_template.x_manufacture.id))
if product_template.public_categ_ids:
query.append(('public_categ_ids', 'in', [x.id for x in product_template.public_categ_ids]))
if len(query) == 2:
query.insert(0, '|')
limit = int(kw.get('limit', 0))
offset = int(kw.get('offset', 0))
order = self.get_product_default_order(kw.get('order'))
product_templates = request.env['product.template'].search(query, limit=limit, offset=offset, order=order)
data = {
'product_total': request.env['product.template'].search_count(query),
'products': [request.env['product.template'].api_single_response(x) for x in product_templates]
}
return self.response(data)
def get_product_default_order(self, order):
orders = ['product_rating desc']
if order != 'price-asc':
orders.append('web_price_sorting desc')
if order == 'price-asc':
orders.append('web_price_sorting asc')
elif order == 'latest':
orders.append('create_date desc')
return ','.join(orders)
|