-
Notifications
You must be signed in to change notification settings - Fork 12
/
tree.py
48 lines (41 loc) · 1.3 KB
/
tree.py
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
# -*- coding: utf-8 -*-
from trytond.pool import PoolMeta
from trytond.model import fields
__all__ = ['Node']
__metaclass__ = PoolMeta
class Node:
__name__ = "product.tree_node"
product_as_menu_children = fields.Boolean('Product as menu children?')
def get_menu_item(self, max_depth):
"""
Return dictionary with serialized node for menu item
{
title: <display name>,
link: <url>,
record: <instance of record> # if type_ is `record`
}
"""
res = {
'record': self,
'title': self.name,
'link': self.get_absolute_url(),
'image': self.image,
'image_url': self.image and self.image.url,
}
if max_depth > 0:
res['children'] = self.get_children(max_depth=max_depth - 1)
return res
def get_children(self, max_depth):
"""
Return serialized menu_item for current treenode
"""
if self.product_as_menu_children:
return [
child.get_menu_item(max_depth=max_depth - 1)
for child in self.get_products()
]
else:
return [
child.get_menu_item(max_depth=max_depth - 1)
for child in self.children
]