summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/brand_homepage.py
blob: 42a7c2f39987f1d768e68c76dfdb5bb3a882a498 (plain)
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
from .. import controller
from odoo import http
from odoo.http import request


class BrandHomepage(controller.Controller):
    prefix = '/api/v1/'

    @http.route(prefix + 'brand_homepage', auth='public', methods=['GET', 'OPTIONS'])
    @controller.Controller.must_authorized()
    def get_brand_homepage(self, **kw):
        base_url = request.env['ir.config_parameter'].get_param('web.base.url')

        query = [('status', '=', 'tayang')]
        brands_homepage = request.env['website.brand.homepage'].search(query, order='sequence')

        data = []
        for brand_homepage in brands_homepage:
            query_product = [
                ('active', '=', True),
                ('type', '=', 'product'),
                ('product_rating', '>', 0),
                ('x_manufacture', '=', brand_homepage.manufacture_id.id)
            ]
            products = request.env['product.template'].search(query_product, order='product_rating desc', limit=12)

            categories = []
            # for product in products:
            #     for c in product.public_categ_ids:
            #         categories.append(c.name)
            # categories = list(dict.fromkeys(categories))
            # product_categories = request.env['product.template'].read_group([
            #     ('public_categ_ids', '!=', False),
            #     ('x_manufacture', '=', brand_homepage.manufacture_id.id)
            # ], fields=['public_categ_ids'], groupby=['public_categ_ids'], limit=10)
            # for product_category in product_categories:
            #     for c in product_category.public_categ_ids:
            #         categories.append(c.name)
            # categories = list(dict.fromkeys(categories))

            commerce_categories = request.env['product.public.category'].search([
                ('product_tmpl_ids.x_manufacture', '=', brand_homepage.manufacture_id.id)
            ], limit=8)

            data.append({
                'id': brand_homepage.id,
                'sequence': brand_homepage.sequence,
                'manufacture_id': brand_homepage.manufacture_id.id,
                'name': brand_homepage.manufacture_id.x_name,
                'image': base_url + 'api/image/website.brand.homepage/image/' + str(
                    brand_homepage.id) if brand_homepage.image else '',
                'url': brand_homepage.url,
                'categories': [y.name for y in commerce_categories],
                'products': [request.env['product.template'].api_single_response(x) for x in products]
            })

        return self.response(data)