district.py
7.9 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import re
from . import _logger
from odoo import api, fields, models
from odoo.osv import expression
def get_selection_label(self, obj, field_name, field_value):
_logger.info(field_name)
_logger.info(field_value)
return dict(self.env[obj].fields_get(allfields=[field_name])[field_name]['selection'])[field_value]
class CountryState(models.Model):
_inherit = 'res.country.state'
district_ids = fields.One2many('res.district', 'state_id', string='Kota/Kabupaten')
# @api.depends('code')
# def _check_district_code(self):
# _logger.info('Lewat')
# for child in self.district_ids:
# _logger.info(child)
# child.display_code = "{}.{}".format(self.code, child.code)
def name_get(self):
result = []
for record in self:
result.append((record.id, "{} ({})".format(record.name, record.code)))
return result
class District(models.Model):
_name = 'res.district'
_description = 'Kota/Kabupaten'
state_id = fields.Many2one('res.country.state', string='Provinsi', required=True)
# state_code = fields.Many2one('res.country.state', related='state_id.code', store=False)
typ = fields.Selection([
('kab', 'Kabupaten'),
('kabtif', 'Kabupaten Administratif'),
('kota', 'Kota'),
('kotif', 'Kota Administratif')],
string='Jenis', required=True)
code = fields.Char(string="Kode Kota/Kabupaten", required=True)
name = fields.Char(string="Nama Kota/Kabupaten", index=True, required=True)
# display_code = fields.Char(compute='_compute_display_code', store=True, 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
]
# @api.depends('code')
# def _compute_display_code(self):
# self.display_code = "{}.{}".format(self.state_id.code, self.code)
# @api.depends('name')
# def _compute_display_name(self):
# pass
# self.display_name = "{} {}".format(self.typ, self.name)
def name_get(self):
result = []
for record in self:
# result.append((record.id, "{} {} ({})".format(record.typ, record.name, record.code)))
result.append((record.id, "{} {} ({}.{})".format(
get_selection_label(self, self._name, 'typ', record.typ), record.name,
record.state_id.code, record.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.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)
_sql_constraints = [
('code_uniq', 'unique (district_id,code)', 'Kode Kecamatan Harus Unik !'),
('name_uniq', 'unique (district_id,name)', 'Nama Kecamatan Harus Unik !'),
]
def code_get(self):
result = []
for record in self:
result.append((record.id, "{}.{}.{}".format(
record.district_id.state_id.code, record.district_id.code,
record.code)))
return result
def name_get(self):
result = []
for record in self:
result.append((record.id, "{} ({}.{}.{})".format(
record.name, record.district_id.state_id.code,
record.district_id.code, record.code)))
return result
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)
_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 !'),
]
def code_get(self):
result = []
for record in self:
result.append((record.id, "{}.{}.{}".format(
record.sub_district_id.district_id.state_id.code,
record.sub_district_id.district_id.code,
record.sub_district_id.code, record.code)))
return result
def name_get(self):
result = []
for record in self:
result.append((record.id, "{} {} ({}.{}.{}.{})".format(
get_selection_label(self, self._name, 'typ', record.typ), record.name,
record.sub_district_id.district_id.state_id.code,
record.sub_district_id.district_id.code,
record.sub_district_id.code, record.code)))
return result