Skip to content

Instantly share code, notes, and snippets.

@sehrishnaz
Created June 23, 2022 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sehrishnaz/142d8ed964fad7c01718e8936513252e to your computer and use it in GitHub Desktop.
Save sehrishnaz/142d8ed964fad7c01718e8936513252e to your computer and use it in GitHub Desktop.
Dynamically Fold Kanban States (Selection Field) in Odoo8
class some_model(models.Model):
_name = "some.model"
STATES = [('Draft','Draft'),('Submit','Submit'),('Closed','Closed')]
# States that should be folded in Kanban view
# used by the `state_groups` method
FOLDED_STATES = [
'Closed',
]
@api.model
def state_groups(self, present_ids, domain, **kwargs):
folded = {key: (key in self.FOLDED_STATES) for key, _ in self.STATES}
# Need to copy self.STATES list before returning it,
# because odoo modifies the list it gets,
# emptying it in the process. Bad odoo!
return self.STATES[:], folded
_group_by_full = {
'state': state_groups
}
state = fields.Selection(STATES, string="Status", default="Draft")
def _read_group_fill_results(self, cr, uid, domain, groupby,
remaining_groupbys, aggregated_fields,
count_field, read_group_result,
read_group_order=None, context=None):
"""
The method seems to support grouping using m2o fields only,
while we want to group by a simple status field.
Hence the code below - it replaces simple status values
with (value, name) tuples.
"""
if groupby == 'state':
STATES_DICT = dict(self.STATES)
for result in read_group_result:
state = result['state']
result['state'] = (state, STATES_DICT.get(state))
return super(some_model, self)._read_group_fill_results(
cr, uid, domain, groupby, remaining_groupbys, aggregated_fields,
count_field, read_group_result, read_group_order, context
)
@sehrishnaz
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment