Skip to content

Commit d615685

Browse files
author
bosd
committed
[IMP] sale_restricted_qty: use class attribute to eliminate repetitive overrides
Address PR OCA#3959 review comments: - Rename mixin to sale.product.restricted.qty.mixin - Move ensure_one() to beginning of _get_* methods - Add _sale_restricted_qty_parent_field class attribute with callable @api.depends to avoid 24+ method overrides per child model
1 parent bf30cba commit d615685

File tree

4 files changed

+171
-412
lines changed

4 files changed

+171
-412
lines changed

sale_restricted_qty/models/product_category.py

Lines changed: 3 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -2,141 +2,11 @@
22
# Copyright 2024 CorporateHub
33
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
44

5-
from odoo import api, models
5+
from odoo import models
66

77

88
class ProductCategory(models.Model):
99
_name = "product.category"
10-
_inherit = ["product.category", "product.restricted.qty.mixin"]
10+
_inherit = ["product.category", "sale.product.restricted.qty.mixin"]
1111

12-
@api.depends("parent_id.is_sale_min_qty_set")
13-
def _compute_is_sale_inherited_min_qty_set(self):
14-
return super()._compute_is_sale_inherited_min_qty_set()
15-
16-
def _get_is_sale_inherited_min_qty_set(self):
17-
if not self.parent_id:
18-
return super()._get_is_sale_inherited_min_qty_set()
19-
20-
self.ensure_one()
21-
return self.parent_id.is_sale_min_qty_set
22-
23-
@api.depends("parent_id.sale_min_qty")
24-
def _compute_sale_inherited_min_qty(self):
25-
return super()._compute_sale_inherited_min_qty()
26-
27-
def _get_sale_inherited_min_qty(self):
28-
if not self.parent_id:
29-
return super()._get_sale_inherited_min_qty()
30-
31-
self.ensure_one()
32-
return self.parent_id.sale_min_qty
33-
34-
@api.depends("parent_id.is_sale_restrict_min_qty_set")
35-
def _compute_is_sale_inherited_restrict_min_qty_set(self):
36-
return super()._compute_is_sale_inherited_restrict_min_qty_set()
37-
38-
def _get_is_sale_inherited_restrict_min_qty_set(self):
39-
if not self.parent_id:
40-
return super()._get_is_sale_inherited_restrict_min_qty_set()
41-
42-
self.ensure_one()
43-
return self.parent_id.is_sale_restrict_min_qty_set
44-
45-
@api.depends("parent_id.sale_restrict_min_qty")
46-
def _compute_sale_inherited_restrict_min_qty(self):
47-
return super()._compute_sale_inherited_restrict_min_qty()
48-
49-
def _get_sale_inherited_restrict_min_qty(self):
50-
if not self.parent_id:
51-
return super()._get_sale_inherited_restrict_min_qty()
52-
53-
self.ensure_one()
54-
return self.parent_id.sale_restrict_min_qty
55-
56-
@api.depends("parent_id.is_sale_max_qty_set")
57-
def _compute_is_sale_inherited_max_qty_set(self):
58-
return super()._compute_is_sale_inherited_max_qty_set()
59-
60-
def _get_is_sale_inherited_max_qty_set(self):
61-
if not self.parent_id:
62-
return super()._get_is_sale_inherited_max_qty_set()
63-
64-
self.ensure_one()
65-
return self.parent_id.is_sale_max_qty_set
66-
67-
@api.depends("parent_id.sale_max_qty")
68-
def _compute_sale_inherited_max_qty(self):
69-
return super()._compute_sale_inherited_max_qty()
70-
71-
def _get_sale_inherited_max_qty(self):
72-
if not self.parent_id:
73-
return super()._get_sale_inherited_max_qty()
74-
75-
self.ensure_one()
76-
return self.parent_id.sale_max_qty
77-
78-
@api.depends("parent_id.is_sale_restrict_max_qty_set")
79-
def _compute_is_sale_inherited_restrict_max_qty_set(self):
80-
return super()._compute_is_sale_inherited_restrict_max_qty_set()
81-
82-
def _get_is_sale_inherited_restrict_max_qty_set(self):
83-
if not self.parent_id:
84-
return super()._get_is_sale_inherited_restrict_max_qty_set()
85-
86-
self.ensure_one()
87-
return self.parent_id.is_sale_restrict_max_qty_set
88-
89-
@api.depends("parent_id.sale_restrict_max_qty")
90-
def _compute_sale_inherited_restrict_max_qty(self):
91-
return super()._compute_sale_inherited_restrict_max_qty()
92-
93-
def _get_sale_inherited_restrict_max_qty(self):
94-
if not self.parent_id:
95-
return super()._get_sale_inherited_restrict_max_qty()
96-
97-
self.ensure_one()
98-
return self.parent_id.sale_restrict_max_qty
99-
100-
@api.depends("parent_id.is_sale_multiple_of_qty_set")
101-
def _compute_is_sale_inherited_multiple_of_qty_set(self):
102-
return super()._compute_is_sale_inherited_multiple_of_qty_set()
103-
104-
def _get_is_sale_inherited_multiple_of_qty_set(self):
105-
if not self.parent_id:
106-
return super()._get_is_sale_inherited_multiple_of_qty_set()
107-
108-
self.ensure_one()
109-
return self.parent_id.is_sale_multiple_of_qty_set
110-
111-
@api.depends("parent_id.sale_multiple_of_qty")
112-
def _compute_sale_inherited_multiple_of_qty(self):
113-
return super()._compute_sale_inherited_multiple_of_qty()
114-
115-
def _get_sale_inherited_multiple_of_qty(self):
116-
if not self.parent_id:
117-
return super()._get_sale_inherited_multiple_of_qty()
118-
119-
self.ensure_one()
120-
return self.parent_id.sale_multiple_of_qty
121-
122-
@api.depends("parent_id.is_sale_restrict_multiple_of_qty_set")
123-
def _compute_is_sale_inherited_restrict_multiple_of_qty_set(self):
124-
return super()._compute_is_sale_inherited_restrict_multiple_of_qty_set()
125-
126-
def _get_is_sale_inherited_restrict_multiple_of_qty_set(self):
127-
if not self.parent_id:
128-
return super()._get_is_sale_inherited_restrict_multiple_of_qty_set()
129-
130-
self.ensure_one()
131-
return self.parent_id.is_sale_restrict_multiple_of_qty_set
132-
133-
@api.depends("parent_id.sale_restrict_multiple_of_qty")
134-
def _compute_sale_inherited_restrict_multiple_of_qty(self):
135-
return super()._compute_sale_inherited_restrict_multiple_of_qty()
136-
137-
def _get_sale_inherited_restrict_multiple_of_qty(self):
138-
if not self.parent_id:
139-
return super()._get_sale_inherited_restrict_multiple_of_qty()
140-
141-
self.ensure_one()
142-
return self.parent_id.sale_restrict_multiple_of_qty
12+
_sale_restricted_qty_parent_field = "parent_id"

sale_restricted_qty/models/product_product.py

Lines changed: 3 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -3,141 +3,11 @@
33
# Copyright 2024 CorporateHub
44
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
55

6-
from odoo import api, models
6+
from odoo import models
77

88

99
class ProductProduct(models.Model):
1010
_name = "product.product"
11-
_inherit = ["product.product", "product.restricted.qty.mixin"]
11+
_inherit = ["product.product", "sale.product.restricted.qty.mixin"]
1212

13-
@api.depends("product_tmpl_id.is_sale_min_qty_set")
14-
def _compute_is_sale_inherited_min_qty_set(self):
15-
return super()._compute_is_sale_inherited_min_qty_set()
16-
17-
def _get_is_sale_inherited_min_qty_set(self):
18-
if not self.product_tmpl_id:
19-
return super()._get_is_sale_inherited_min_qty_set()
20-
21-
self.ensure_one()
22-
return self.product_tmpl_id.is_sale_min_qty_set
23-
24-
@api.depends("product_tmpl_id.sale_min_qty")
25-
def _compute_sale_inherited_min_qty(self):
26-
return super()._compute_sale_inherited_min_qty()
27-
28-
def _get_sale_inherited_min_qty(self):
29-
if not self.product_tmpl_id:
30-
return super()._get_sale_inherited_min_qty()
31-
32-
self.ensure_one()
33-
return self.product_tmpl_id.sale_min_qty
34-
35-
@api.depends("product_tmpl_id.is_sale_restrict_min_qty_set")
36-
def _compute_is_sale_inherited_restrict_min_qty_set(self):
37-
return super()._compute_is_sale_inherited_restrict_min_qty_set()
38-
39-
def _get_is_sale_inherited_restrict_min_qty_set(self):
40-
if not self.product_tmpl_id:
41-
return super()._get_is_sale_inherited_restrict_min_qty_set()
42-
43-
self.ensure_one()
44-
return self.product_tmpl_id.is_sale_restrict_min_qty_set
45-
46-
@api.depends("product_tmpl_id.sale_restrict_min_qty")
47-
def _compute_sale_inherited_restrict_min_qty(self):
48-
return super()._compute_sale_inherited_restrict_min_qty()
49-
50-
def _get_sale_inherited_restrict_min_qty(self):
51-
if not self.product_tmpl_id:
52-
return super()._get_sale_inherited_restrict_min_qty()
53-
54-
self.ensure_one()
55-
return self.product_tmpl_id.sale_restrict_min_qty
56-
57-
@api.depends("product_tmpl_id.is_sale_max_qty_set")
58-
def _compute_is_sale_inherited_max_qty_set(self):
59-
return super()._compute_is_sale_inherited_max_qty_set()
60-
61-
def _get_is_sale_inherited_max_qty_set(self):
62-
if not self.product_tmpl_id:
63-
return super()._get_is_sale_inherited_max_qty_set()
64-
65-
self.ensure_one()
66-
return self.product_tmpl_id.is_sale_max_qty_set
67-
68-
@api.depends("product_tmpl_id.sale_max_qty")
69-
def _compute_sale_inherited_max_qty(self):
70-
return super()._compute_sale_inherited_max_qty()
71-
72-
def _get_sale_inherited_max_qty(self):
73-
if not self.product_tmpl_id:
74-
return super()._get_sale_inherited_max_qty()
75-
76-
self.ensure_one()
77-
return self.product_tmpl_id.sale_max_qty
78-
79-
@api.depends("product_tmpl_id.is_sale_restrict_max_qty_set")
80-
def _compute_is_sale_inherited_restrict_max_qty_set(self):
81-
return super()._compute_is_sale_inherited_restrict_max_qty_set()
82-
83-
def _get_is_sale_inherited_restrict_max_qty_set(self):
84-
if not self.product_tmpl_id:
85-
return super()._get_is_sale_inherited_restrict_max_qty_set()
86-
87-
self.ensure_one()
88-
return self.product_tmpl_id.is_sale_restrict_max_qty_set
89-
90-
@api.depends("product_tmpl_id.sale_restrict_max_qty")
91-
def _compute_sale_inherited_restrict_max_qty(self):
92-
return super()._compute_sale_inherited_restrict_max_qty()
93-
94-
def _get_sale_inherited_restrict_max_qty(self):
95-
if not self.product_tmpl_id:
96-
return super()._get_sale_inherited_restrict_max_qty()
97-
98-
self.ensure_one()
99-
return self.product_tmpl_id.sale_restrict_max_qty
100-
101-
@api.depends("product_tmpl_id.is_sale_multiple_of_qty_set")
102-
def _compute_is_sale_inherited_multiple_of_qty_set(self):
103-
return super()._compute_is_sale_inherited_multiple_of_qty_set()
104-
105-
def _get_is_sale_inherited_multiple_of_qty_set(self):
106-
if not self.product_tmpl_id:
107-
return super()._get_is_sale_inherited_multiple_of_qty_set()
108-
109-
self.ensure_one()
110-
return self.product_tmpl_id.is_sale_multiple_of_qty_set
111-
112-
@api.depends("product_tmpl_id.sale_multiple_of_qty")
113-
def _compute_sale_inherited_multiple_of_qty(self):
114-
return super()._compute_sale_inherited_multiple_of_qty()
115-
116-
def _get_sale_inherited_multiple_of_qty(self):
117-
if not self.product_tmpl_id:
118-
return super()._get_sale_inherited_multiple_of_qty()
119-
120-
self.ensure_one()
121-
return self.product_tmpl_id.sale_multiple_of_qty
122-
123-
@api.depends("product_tmpl_id.is_sale_restrict_multiple_of_qty_set")
124-
def _compute_is_sale_inherited_restrict_multiple_of_qty_set(self):
125-
return super()._compute_is_sale_inherited_restrict_multiple_of_qty_set()
126-
127-
def _get_is_sale_inherited_restrict_multiple_of_qty_set(self):
128-
if not self.product_tmpl_id:
129-
return super()._get_is_sale_inherited_restrict_multiple_of_qty_set()
130-
131-
self.ensure_one()
132-
return self.product_tmpl_id.is_sale_restrict_multiple_of_qty_set
133-
134-
@api.depends("product_tmpl_id.sale_restrict_multiple_of_qty")
135-
def _compute_sale_inherited_restrict_multiple_of_qty(self):
136-
return super()._compute_sale_inherited_restrict_multiple_of_qty()
137-
138-
def _get_sale_inherited_restrict_multiple_of_qty(self):
139-
if not self.product_tmpl_id:
140-
return super()._get_sale_inherited_restrict_multiple_of_qty()
141-
142-
self.ensure_one()
143-
return self.product_tmpl_id.sale_restrict_multiple_of_qty
13+
_sale_restricted_qty_parent_field = "product_tmpl_id"

0 commit comments

Comments
 (0)