district.py
5.85 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import re
from odoo import api, fields, models
from odoo.osv import expression
class District(models.Model):
_name = 'res.district'
_description = 'Kota/Kabupaten'
state_id = fields.Many2one('res.country.state', string='Provinsi', required=True)
typ = fields.Selection([
('kab', 'Kabupaten'),
('kabtif', 'Kabupaten Administratif'),
('kota', 'Kota'),
('kotif', 'Kota Administratif')],
string='Jenis')
code = fields.Char(string="Kode Kota/Kabupaten")
name = fields.Char(string="Nama Kota/Kabupaten", index=True)
display_code = fields.Char(index=True)
# display_code = fields.Char(compute='_compute_display_code') #, store=True, index=True)
display_name = fields.Char(index=True)
# display_name = fields.Char(compute='_compute_display_name') #, store=True, index=True)
sub_district_ids = fields.One2many('res.district.sub', 'district_id', string='Kecamatan')
# address_view_id = fields.Many2one(
# comodel_name='ir.ui.view', string="Input View",
# domain=[('model', '=', 'res.partner'), ('type', '=', 'form')],
# help="Use this field if you want to replace the usual way to encode a complete address. "
# "Note that the address_format field is used to modify the way to display addresses "
# "(in reports for example), while this field is used to modify the input form for "
# "addresses.")
#
# image_url = fields.Char(
# compute="_compute_image_url", string="Flag",
# help="Url of static flag image",
# )
_sql_constraints = [
('code_uniq', 'unique (state_id,code)', 'Kode Kabupaten/Kota Harus Unik !'),
('name_uniq', 'unique (state_id,typ,name)', 'Nama Kabupaten/Kota Harus Unik !'),
]
@api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
if self.env.context.get('state_id'):
args = expression.AND([args, [('state_id', '=', self.env.context.get('state_id'))]])
if operator == 'ilike' and not (name or '').strip():
first_domain = []
domain = []
else:
first_domain = [('code', '=ilike', name)]
domain = [('name', operator, name)]
first_district_ids = self._search(expression.AND([first_domain, args]), limit=limit,
access_rights_uid=name_get_uid) if first_domain else []
return list(first_district_ids) + [
state_id
for state_id in self._search(expression.AND([domain, args]),
limit=limit, access_rights_uid=name_get_uid)
if state_id not in first_district_ids
]
def name_get(self):
result = []
for record in self:
result.append((record.id, "{} {} ({})".format(record.typ, record.name, record.state_id.code)))
return result
def code_get(self):
result = []
for record in self:
result.append((record.id, "{}.{}".format(record.state_id.code, record.code)))
return result
@api.depends('name')
def _compute_display_name(self):
pass
# self.display_name = "{} {}".format(self.typ, self.name)
@api.depends('code')
def _compute_display_code(self):
pass
# self.display_code = "{}.{}".format(self.state_id.code, self.code)
# @api.model_create_multi
# def create(self, vals_list):
# for vals in vals_list:
# if vals.get('code'):
# vals['code'] = vals['code'].upper()
# return super(District, self).create(vals_list)
#
# def write(self, vals):
# if vals.get('code'):
# vals['code'] = vals['code'].upper()
# return super(District, self).write(vals)
#
# def get_address_fields(self):
# self.ensure_one()
# return re.findall(r'\((.+?)\)', self.address_format)
#
# @api.depends('code')
# def _compute_image_url(self):
# for district in self:
# if not district.code:
# district.image_url = False
# else:
# code = self.display_code
# district.image_url = "/base/static/img/district_flags/%s.png" % code
class SubDistrict(models.Model):
_name = 'res.district.sub'
_description = 'Kecamatan'
district_id = fields.Many2one('res.district', string='Kabupaten/Kota', required=True)
code = fields.Char(string="Kode Kecamatan")
name = fields.Char(string="Nama Kecamatan", index=True)
display_code = fields.Char(index=True) # compute='_compute_display_code', store=True,
# display_name = fields.Char(index=True) # compute='_compute_display_name', store=True,
_sql_constraints = [
('code_uniq', 'unique (district_id,code)', 'Kode Kecamatan Harus Unik !'),
('name_uniq', 'unique (district_id,name)', 'Nama Kecamatan Harus Unik !'),
]
class Village(models.Model):
_name = 'res.district.village'
_description = "Desa/Keurahan"
sub_district_id = fields.Many2one('res.district.sub', string='Kecamatan', required=True)
typ = fields.Selection([
('desa', 'Desa'),
('kelurahan', 'Kelurahan')],
string='Jenis')
code = fields.Char(string="Kode Desa/Kelurahan")
name = fields.Char(string="Nama Desa/Kelurahan", index=True)
display_code = fields.Char(index=True) # compute='_compute_display_code', store=True,
display_name = fields.Char(index=True) # compute='_compute_display_name', store=True,
_sql_constraints = [
('village_code_uniq', 'unique (sub_district_id,code)', 'Kode Kelurahan/Desa Harus Unik !'),
('village_name_uniq', 'unique (sub_district_id,typ,name)', 'Nama Kelurahan/Desa Harus Unik !'),
]