summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/manufacture.py
blob: 3cb7b70c509721d2df191cd7e8ad5fc5b0f186e2 (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
from .. import controller
from odoo import http
from odoo.http import request
import ast


class Manufacture(controller.Controller):
    prefix = '/api/v1/'
    
    @http.route(prefix + 'manufacture/page/<page>', auth='public', methods=['GET'])
    def get_manufacture(self, **kw):
        if not self.authenticate():
            return self.response(code=401, description='Unauthorized')
        
        manufacture_ids = []
        page = kw.get('page')
        if page == 'flash-sale':
            active_flash_sale = request.env['product.pricelist'].get_active_flash_sale()
            if active_flash_sale:
                manufacture_ids = [x.product_id.x_manufacture.id for x in active_flash_sale.item_ids]
        elif page == 'ready-stock':
            product_templates = request.env['product.template'].search([('virtual_qty', '>', 0)])
            if product_templates:
                manufacture_ids = [x.x_manufacture.id for x in product_templates]
        elif page == 'category':
            category_id = kw.get('category_id')
            if not category_id:
                return self.response(code=400, description='category_id is required')
            product_variants = request.env['product.product'].search([('public_categ_ids', '=', int(category_id))])
            manufacture_ids = [x.x_manufacture.id for x in product_variants]
        elif page == 'promotion':
            promotion_id = kw.get('promotion_id')
            if not promotion_id:
                return self.response(code=400, description='promotion_id is required')
            coupon_program = request.env['coupon.program'].search([('id', '=', promotion_id)])
            product_variants = request.env['product.product'].search(ast.literal_eval(coupon_program.rule_products_domain))
            manufacture_ids = [x.x_manufacture.id for x in product_variants]
        else:
            return self.response(code=400, description='page possible value is flash-sale, ready-stock, category, promotion')
        
        manufactures = request.env['x_manufactures'].search([('id', 'in', manufacture_ids)])
        data = []
        for manufacture in manufactures:
            data.append({
                'id': manufacture.id,
                'name': manufacture.x_name
            })
        return self.response(data)