feat: implement analytic distribution sanitization and cleaning via analytic_mixin inheritance
This commit is contained in:
parent
86dd8b93ce
commit
d61396fe9c
@ -7,4 +7,5 @@ from . import pos_session
|
|||||||
from . import pos_order
|
from . import pos_order
|
||||||
from . import account_move
|
from . import account_move
|
||||||
from . import res_company
|
from . import res_company
|
||||||
|
from . import analytic_mixin
|
||||||
|
|
||||||
|
|||||||
@ -84,3 +84,13 @@ class AccountMove(models.Model):
|
|||||||
print(f" Parent Move Posted: {parent_move.name}")
|
print(f" Parent Move Posted: {parent_move.name}")
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
class AccountMoveLine(models.Model):
|
||||||
|
_inherit = 'account.move.line'
|
||||||
|
|
||||||
|
def _related_analytic_distribution(self):
|
||||||
|
res = super()._related_analytic_distribution()
|
||||||
|
if res:
|
||||||
|
return self.env['analytic.mixin']._clean_analytic_distribution(res)
|
||||||
|
return res
|
||||||
|
|||||||
37
models/analytic_mixin.py
Normal file
37
models/analytic_mixin.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
from odoo import models, api
|
||||||
|
|
||||||
|
class AnalyticMixin(models.AbstractModel):
|
||||||
|
_inherit = 'analytic.mixin'
|
||||||
|
|
||||||
|
def _sanitize_values(self, vals, decimal_precision):
|
||||||
|
if 'analytic_distribution' in vals:
|
||||||
|
dist = vals.get('analytic_distribution')
|
||||||
|
if dist:
|
||||||
|
if isinstance(dist, str):
|
||||||
|
import json
|
||||||
|
try:
|
||||||
|
dist = json.loads(dist)
|
||||||
|
except Exception:
|
||||||
|
dist = {}
|
||||||
|
vals['analytic_distribution'] = self._clean_analytic_distribution(dist) or False
|
||||||
|
else:
|
||||||
|
vals['analytic_distribution'] = False
|
||||||
|
return super()._sanitize_values(vals, decimal_precision)
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _clean_analytic_distribution(self, dist):
|
||||||
|
if not dist or not isinstance(dist, dict):
|
||||||
|
return {}
|
||||||
|
clean_dist = {}
|
||||||
|
for key, val in dist.items():
|
||||||
|
str_key = str(key)
|
||||||
|
if str_key == '__update__':
|
||||||
|
clean_dist[str_key] = val
|
||||||
|
elif str_key:
|
||||||
|
if all(part.isdigit() for part in str_key.split(',')):
|
||||||
|
try:
|
||||||
|
clean_dist[str_key] = float(val)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
return clean_dist
|
||||||
Loading…
Reference in New Issue
Block a user