models.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. from itertools import count, zip_longest
  4. from odoo import api, fields, models, Command
  5. class A(models.Model):
  6. _name = 'test_testing_utilities.a'
  7. _description = 'Testing Utilities A'
  8. f1 = fields.Char(required=True)
  9. f2 = fields.Integer(default=42)
  10. f3 = fields.Integer()
  11. f4 = fields.Integer(compute='_compute_f4')
  12. f5 = fields.Integer()
  13. f6 = fields.Integer()
  14. @api.onchange('f2')
  15. def _on_change_f2(self):
  16. self.f3 = int(self.f2 / 2)
  17. self.f5 = self.f2
  18. self.f6 = self.f2
  19. @api.depends('f1', 'f2')
  20. def _compute_f4(self):
  21. for r in self:
  22. r.f4 = r.f2 / (int(r.f1) or 1)
  23. class B(models.Model):
  24. _name = 'test_testing_utilities.readonly'
  25. _description = 'Testing Utilities Readonly'
  26. f1 = fields.Integer(default=1, readonly=True)
  27. f2 = fields.Integer(compute='_compute_f2')
  28. @api.depends('f1')
  29. def _compute_f2(self):
  30. for r in self:
  31. r.f2 = 2 * r.f1
  32. class C(models.Model):
  33. _name = 'test_testing_utilities.c'
  34. _description = 'Testing Utilities C'
  35. name = fields.Char("name", required=True)
  36. f2 = fields.Many2one('test_testing_utilities.m2o')
  37. @api.onchange('f2')
  38. def _on_change_f2(self):
  39. self.name = self.f2.name
  40. class M2O(models.Model):
  41. _name = 'test_testing_utilities.m2o'
  42. _description = 'Testing Utilities Many To One'
  43. name = fields.Char(required=True)
  44. class M2Onchange(models.Model):
  45. _name = 'test_testing_utilities.d'
  46. _description = 'Testing Utilities D'
  47. # used to check that defaults & onchange to m2o work
  48. f = fields.Many2one(
  49. 'test_testing_utilities.m2o',
  50. required=True,
  51. default=lambda self: self.env['test_testing_utilities.m2o'].search(
  52. [], limit=1
  53. )
  54. )
  55. f2 = fields.Char()
  56. @api.onchange('f2')
  57. def _on_change_f2(self):
  58. self.f = self.env['test_testing_utilities.m2o'].search([
  59. ('name', 'ilike', self.f2),
  60. ], limit=1) if self.f2 else False
  61. class M2MChange(models.Model):
  62. _name = 'test_testing_utilities.e'
  63. _description = 'Testing Utilities E'
  64. m2m = fields.Many2many('test_testing_utilities.sub2')
  65. count = fields.Integer(compute='_m2m_count', inverse='_set_count')
  66. @api.depends('m2m')
  67. def _m2m_count(self):
  68. for r in self:
  69. r.count = len(r.m2m)
  70. def _set_count(self):
  71. for r in self:
  72. r.write({
  73. 'm2m': [
  74. Command.create({'name': str(n)})
  75. for n, v in zip_longest(range(r.count), r.m2m or [])
  76. if v is None
  77. ]
  78. })
  79. class M2MSub(models.Model):
  80. _name = 'test_testing_utilities.sub2'
  81. _description = 'Testing Utilities Subtraction 2'
  82. name = fields.Char()
  83. m2o_ids = fields.Many2many('test_testing_utilities.m2o')
  84. class M2MChange2(models.Model):
  85. _name = 'test_testing_utilities.f'
  86. _description = 'Testing Utilities F'
  87. def _get_some(self):
  88. r = self.env['test_testing_utilities.sub2'].search([], limit=2)
  89. return r
  90. m2m = fields.Many2many(
  91. 'test_testing_utilities.sub2',
  92. default=_get_some,
  93. )
  94. m2o = fields.Many2one('test_testing_utilities.sub2')
  95. @api.onchange('m2o')
  96. def _on_change_m2o(self):
  97. self.m2m = self.m2m | self.m2o
  98. class M2MReadonly(models.Model):
  99. _name = 'test_testing_utilities.g'
  100. _description = 'Testing Utilities G'
  101. m2m = fields.Many2many('test_testing_utilities.sub3', readonly=True)
  102. class M2MSub3(models.Model):
  103. _name = 'test_testing_utilities.sub3'
  104. _description = 'Testing Utilities Subtraction 3'
  105. name = fields.Char()
  106. class O2MChange(models.Model):
  107. _name = 'test_testing_utilities.parent'
  108. _description = 'Testing Utilities Parent'
  109. value = fields.Integer(default=1)
  110. v = fields.Integer()
  111. subs = fields.One2many('test_testing_utilities.sub', 'parent_id')
  112. @api.onchange('value', 'subs')
  113. def _onchange_values(self):
  114. self.v = self.value + sum(s.value for s in self.subs)
  115. class O2MSub(models.Model):
  116. _name = 'test_testing_utilities.sub'
  117. _description = 'Testing Utilities Subtraction'
  118. name = fields.Char(compute='_compute_name')
  119. value = fields.Integer(default=2)
  120. v = fields.Integer()
  121. parent_id = fields.Many2one('test_testing_utilities.parent')
  122. has_parent = fields.Boolean()
  123. @api.onchange('value')
  124. def _onchange_value(self):
  125. self.v = self.value
  126. @api.depends('v')
  127. def _compute_name(self):
  128. for r in self:
  129. r.name = str(r.v)
  130. @api.onchange('has_parent')
  131. def _onchange_has_parent(self):
  132. if self.has_parent:
  133. self.value = self.parent_id.value
  134. class O2MDefault(models.Model):
  135. _name = 'test_testing_utilities.default'
  136. _description = 'Testing Utilities Default'
  137. def _default_subs(self):
  138. return [
  139. Command.create({'v': 5})
  140. ]
  141. value = fields.Integer(default=1)
  142. v = fields.Integer()
  143. subs = fields.One2many('test_testing_utilities.sub3', 'parent_id', default=_default_subs)
  144. class O2MSub3(models.Model):
  145. _name = 'test_testing_utilities.sub3'
  146. _description = 'Testing Utilities Subtraction 3'
  147. name = fields.Char(compute='_compute_name')
  148. value = fields.Integer(default=2)
  149. v = fields.Integer(default=6)
  150. parent_id = fields.Many2one('test_testing_utilities.default')
  151. @api.onchange('value')
  152. def _onchange_value(self):
  153. self.v = self.value
  154. @api.depends('v')
  155. def _compute_name(self):
  156. for r in self:
  157. r.name = str(r.v)
  158. class O2MRecursive(models.Model):
  159. _name = _description = 'test_testing_utilities.recursive'
  160. one_to_many_id = fields.Many2one('test_testing_utilities.recursive', readonly=True)
  161. many_to_one_ids = fields.One2many('test_testing_utilities.recursive', 'one_to_many_id', readonly=True)
  162. class O2MOnchangeParent(models.Model):
  163. _name = 'test_testing_utilities.onchange_parent'
  164. _description = 'Testing Utilities Onchange Parent'
  165. line_ids = fields.One2many('test_testing_utilities.onchange_line', 'parent')
  166. @api.onchange('line_ids')
  167. def _onchange_line_ids(self):
  168. for line in self.line_ids.filtered(lambda l: l.flag):
  169. self.env['test_testing_utilities.onchange_line'].new({'parent': self.id})
  170. class M2OOnchangeLine(models.Model):
  171. _name = 'test_testing_utilities.onchange_line'
  172. _description = 'Testing Utilities Onchange Line'
  173. parent = fields.Many2one('test_testing_utilities.onchange_parent')
  174. dummy = fields.Float()
  175. flag = fields.Boolean(store=False)
  176. @api.onchange('dummy')
  177. def _onchange_flag(self):
  178. self.flag = True
  179. class O2MChangeCount(models.Model):
  180. _name = 'test_testing_utilities.onchange_count'
  181. _description = _name
  182. count = fields.Integer()
  183. line_ids = fields.One2many('test_testing_utilities.onchange_count_sub', 'parent')
  184. @api.onchange('count')
  185. def _onchange_count(self):
  186. Sub = self.env['test_testing_utilities.onchange_count_sub']
  187. recs = Sub
  188. for i in range(self.count):
  189. recs |= Sub.new({'name': str(i)})
  190. self.line_ids = recs
  191. class O2MChangeSub(models.Model):
  192. _name = 'test_testing_utilities.onchange_count_sub'
  193. _description = _name
  194. parent = fields.Many2one('test_testing_utilities.onchange_count')
  195. name = fields.Char()
  196. class O2MReadonlySubfield(models.Model):
  197. _name = 'o2m_readonly_subfield_parent'
  198. _description = _name
  199. line_ids = fields.One2many('o2m_readonly_subfield_child', 'parent_id')
  200. class O2MReadonlySubfieldChild(models.Model):
  201. _name = _description = 'o2m_readonly_subfield_child'
  202. name = fields.Char()
  203. parent_id = fields.Many2one('o2m_readonly_subfield_parent')
  204. f = fields.Integer(compute='_compute_f', inverse='_inverse_f', readonly=True)
  205. @api.depends('name')
  206. def _compute_f(self):
  207. for r in self:
  208. r.f = len(r.name) if r.name else 0
  209. def _inverse_f(self):
  210. raise AssertionError("Inverse of f should not be called")
  211. class ReqBool(models.Model):
  212. _name = _description = 'test_testing_utilities.req_bool'
  213. f_bool = fields.Boolean(required=True)
  214. class O2MChangesParent(models.Model):
  215. _name = _description = 'o2m_changes_parent'
  216. name = fields.Char()
  217. line_ids = fields.One2many('o2m_changes_children', 'parent_id')
  218. @api.onchange('name')
  219. def _onchange_name(self):
  220. for line in self.line_ids:
  221. line.line_ids = [Command.delete(l.id) for l in line.line_ids] + [
  222. Command.create({'v': 0, 'vv': 0})
  223. ]
  224. class O2MChangesChildren(models.Model):
  225. _name = _description = 'o2m_changes_children'
  226. name = fields.Char()
  227. v = fields.Integer()
  228. line_ids = fields.One2many('o2m_changes_children.lines', 'parent_id')
  229. parent_id = fields.Many2one('o2m_changes_parent')
  230. @api.onchange('v')
  231. def _onchange_v(self):
  232. for record in self:
  233. for line in record.line_ids:
  234. line.v = record.v
  235. class O2MChangesChildrenLines(models.Model):
  236. _name = _description = 'o2m_changes_children.lines'
  237. parent_id = fields.Many2one('o2m_changes_children')
  238. v = fields.Integer()
  239. vv = fields.Integer()
  240. class ResConfigTest(models.Model):
  241. _inherit = 'res.config.settings'
  242. _name = 'res.config.test'
  243. _description = 'Config test'
  244. param1 = fields.Integer(
  245. string='Test parameter 1',
  246. config_parameter='resConfigTest.parameter1',
  247. default=1000)
  248. param2 = fields.Many2one(
  249. 'res.config',
  250. config_parameter="resConfigTest.parameter2")