-
Notifications
You must be signed in to change notification settings - Fork 26
/
tree.py
53 lines (45 loc) · 1.38 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
49
50
51
52
53
# -*- coding: utf-8 -*-
'''
Tree Node
:copyright: (c) 2015 by Openlabs Technologies & Consulting (P) Ltd.
:license: GPLv3, see LICENSE for more details
'''
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,
}
if max_depth:
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
]