summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/sub_district.py
blob: 706cc66047f704a4fc76bd4dc6d18b980861368b (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
from .. import controller
from odoo import http
from odoo.http import request

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

    @http.route(prefix + 'sub_district', auth='public', methods=['GET', 'OPTIONS'])
    def get_sub_district(self, **kw):
        if not self.authenticate():
            return self.response(code=401, description='Unauthorized')
        
        parameters = []
        
        name = kw.get('name')
        if name:
            name = '%' + name.replace(' ', '%') + '%'
            parameters.append(('name', 'ilike', name))

        district_id = kw.get('district_id')
        if district_id:
            parameters.append(('kecamatan_id', '=', int(district_id)))

        sub_districts = request.env['vit.kelurahan'].search(parameters)
        data = []
        for sub_district in sub_districts:
            data.append({ 'id': sub_district.id, 'name': sub_district.name })
        
        return self.response(data)