forked from trytonus/nereid-webshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.py
60 lines (51 loc) · 1.59 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
54
55
56
57
58
59
60
# -*- 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,
}
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
]
@classmethod
def get_template(cls):
"""
Return templates available for node.
Downstream module can add new templates
"""
res = super(Node, cls).get_template()
template = ('catalog/node-list-view.html', 'List View')
if template not in res:
res.append(template)
return res