|
34 | 34 | logger = getLogger(__name__)
|
35 | 35 |
|
36 | 36 |
|
| 37 | +@python_2_unicode_compatible |
| 38 | +class TreeNode(MP_Node): |
| 39 | + |
| 40 | + parent = models.ForeignKey( |
| 41 | + 'self', |
| 42 | + on_delete=models.CASCADE, |
| 43 | + blank=True, |
| 44 | + null=True, |
| 45 | + related_name='children', |
| 46 | + db_index=True, |
| 47 | + ) |
| 48 | + site = models.ForeignKey( |
| 49 | + Site, |
| 50 | + on_delete=models.CASCADE, |
| 51 | + verbose_name=_("site"), |
| 52 | + related_name='djangocms_nodes', |
| 53 | + db_index=True, |
| 54 | + ) |
| 55 | + |
| 56 | + objects = PageNodeManager() |
| 57 | + |
| 58 | + class Meta: |
| 59 | + app_label = 'cms' |
| 60 | + ordering = ('path',) |
| 61 | + default_permissions = [] |
| 62 | + |
| 63 | + def __str__(self): |
| 64 | + return self.path |
| 65 | + |
| 66 | + @cached_property |
| 67 | + def item(self): |
| 68 | + return self.get_item() |
| 69 | + |
| 70 | + def get_item(self): |
| 71 | + # Paving the way... |
| 72 | + return Page.objects.get(node=self, publisher_is_draft=True) |
| 73 | + |
| 74 | + @property |
| 75 | + def is_branch(self): |
| 76 | + return bool(self.numchild) |
| 77 | + |
| 78 | + def get_ancestor_paths(self): |
| 79 | + paths = frozenset( |
| 80 | + self.path[0:pos] |
| 81 | + for pos in range(0, len(self.path), self.steplen)[1:] |
| 82 | + ) |
| 83 | + return paths |
| 84 | + |
| 85 | + def add_child(self, **kwargs): |
| 86 | + if len(kwargs) == 1 and 'instance' in kwargs: |
| 87 | + kwargs['instance'].parent = self |
| 88 | + else: |
| 89 | + kwargs['parent'] = self |
| 90 | + return super(TreeNode, self).add_child(**kwargs) |
| 91 | + |
| 92 | + def add_sibling(self, pos=None, *args, **kwargs): |
| 93 | + if len(kwargs) == 1 and 'instance' in kwargs: |
| 94 | + kwargs['instance'].parent_id = self.parent_id |
| 95 | + else: |
| 96 | + kwargs['parent_id'] = self.parent_id |
| 97 | + return super(TreeNode, self).add_sibling(*args, **kwargs) |
| 98 | + |
| 99 | + def update(self, **data): |
| 100 | + cls = self.__class__ |
| 101 | + cls.objects.filter(pk=self.pk).update(**data) |
| 102 | + |
| 103 | + for field, value in data.items(): |
| 104 | + setattr(self, field, value) |
| 105 | + return |
| 106 | + |
| 107 | + def get_cached_ancestors(self): |
| 108 | + if self._has_cached_hierarchy(): |
| 109 | + return self._ancestors |
| 110 | + return [] |
| 111 | + |
| 112 | + def get_cached_descendants(self): |
| 113 | + if self._has_cached_hierarchy(): |
| 114 | + return self._descendants |
| 115 | + return [] |
| 116 | + |
| 117 | + def _reload(self): |
| 118 | + """ |
| 119 | + Reload a page node from the database |
| 120 | + """ |
| 121 | + return self.__class__.objects.get(pk=self.pk) |
| 122 | + |
| 123 | + def _has_cached_hierarchy(self): |
| 124 | + return hasattr(self, '_descendants') and hasattr(self, '_ancestors') |
| 125 | + |
| 126 | + def _set_hierarchy(self, nodes, ancestors=None): |
| 127 | + if self.is_branch: |
| 128 | + self._descendants = [node for node in nodes |
| 129 | + if node.path.startswith(self.path) |
| 130 | + and node.depth > self.depth] |
| 131 | + else: |
| 132 | + self._descendants = [] |
| 133 | + |
| 134 | + if self.is_root(): |
| 135 | + self._ancestors = [] |
| 136 | + else: |
| 137 | + self._ancestors = ancestors |
| 138 | + |
| 139 | + children = (node for node in self._descendants |
| 140 | + if node.depth == self.depth + 1) |
| 141 | + |
| 142 | + for child in children: |
| 143 | + child._set_hierarchy(self._descendants, ancestors=([self] + self._ancestors)) |
| 144 | + |
| 145 | + |
37 | 146 | @python_2_unicode_compatible
|
38 | 147 | class Page(models.Model):
|
39 | 148 | """
|
@@ -1534,112 +1643,3 @@ def get_root_page(cls, site):
|
1534 | 1643 |
|
1535 | 1644 | def is_potential_home(self):
|
1536 | 1645 | return False
|
1537 |
| - |
1538 |
| - |
1539 |
| -@python_2_unicode_compatible |
1540 |
| -class TreeNode(MP_Node): |
1541 |
| - |
1542 |
| - parent = models.ForeignKey( |
1543 |
| - 'self', |
1544 |
| - on_delete=models.CASCADE, |
1545 |
| - blank=True, |
1546 |
| - null=True, |
1547 |
| - related_name='children', |
1548 |
| - db_index=True, |
1549 |
| - ) |
1550 |
| - site = models.ForeignKey( |
1551 |
| - Site, |
1552 |
| - on_delete=models.CASCADE, |
1553 |
| - verbose_name=_("site"), |
1554 |
| - related_name='djangocms_nodes', |
1555 |
| - db_index=True, |
1556 |
| - ) |
1557 |
| - |
1558 |
| - objects = PageNodeManager() |
1559 |
| - |
1560 |
| - class Meta: |
1561 |
| - app_label = 'cms' |
1562 |
| - ordering = ('path',) |
1563 |
| - default_permissions = [] |
1564 |
| - |
1565 |
| - def __str__(self): |
1566 |
| - return self.path |
1567 |
| - |
1568 |
| - @cached_property |
1569 |
| - def item(self): |
1570 |
| - return self.get_item() |
1571 |
| - |
1572 |
| - def get_item(self): |
1573 |
| - # Paving the way... |
1574 |
| - return Page.objects.get(node=self, publisher_is_draft=True) |
1575 |
| - |
1576 |
| - @property |
1577 |
| - def is_branch(self): |
1578 |
| - return bool(self.numchild) |
1579 |
| - |
1580 |
| - def get_ancestor_paths(self): |
1581 |
| - paths = frozenset( |
1582 |
| - self.path[0:pos] |
1583 |
| - for pos in range(0, len(self.path), self.steplen)[1:] |
1584 |
| - ) |
1585 |
| - return paths |
1586 |
| - |
1587 |
| - def add_child(self, **kwargs): |
1588 |
| - if len(kwargs) == 1 and 'instance' in kwargs: |
1589 |
| - kwargs['instance'].parent = self |
1590 |
| - else: |
1591 |
| - kwargs['parent'] = self |
1592 |
| - return super(TreeNode, self).add_child(**kwargs) |
1593 |
| - |
1594 |
| - def add_sibling(self, pos=None, *args, **kwargs): |
1595 |
| - if len(kwargs) == 1 and 'instance' in kwargs: |
1596 |
| - kwargs['instance'].parent_id = self.parent_id |
1597 |
| - else: |
1598 |
| - kwargs['parent_id'] = self.parent_id |
1599 |
| - return super(TreeNode, self).add_sibling(*args, **kwargs) |
1600 |
| - |
1601 |
| - def update(self, **data): |
1602 |
| - cls = self.__class__ |
1603 |
| - cls.objects.filter(pk=self.pk).update(**data) |
1604 |
| - |
1605 |
| - for field, value in data.items(): |
1606 |
| - setattr(self, field, value) |
1607 |
| - return |
1608 |
| - |
1609 |
| - def get_cached_ancestors(self): |
1610 |
| - if self._has_cached_hierarchy(): |
1611 |
| - return self._ancestors |
1612 |
| - return [] |
1613 |
| - |
1614 |
| - def get_cached_descendants(self): |
1615 |
| - if self._has_cached_hierarchy(): |
1616 |
| - return self._descendants |
1617 |
| - return [] |
1618 |
| - |
1619 |
| - def _reload(self): |
1620 |
| - """ |
1621 |
| - Reload a page node from the database |
1622 |
| - """ |
1623 |
| - return self.__class__.objects.get(pk=self.pk) |
1624 |
| - |
1625 |
| - def _has_cached_hierarchy(self): |
1626 |
| - return hasattr(self, '_descendants') and hasattr(self, '_ancestors') |
1627 |
| - |
1628 |
| - def _set_hierarchy(self, nodes, ancestors=None): |
1629 |
| - if self.is_branch: |
1630 |
| - self._descendants = [node for node in nodes |
1631 |
| - if node.path.startswith(self.path) |
1632 |
| - and node.depth > self.depth] |
1633 |
| - else: |
1634 |
| - self._descendants = [] |
1635 |
| - |
1636 |
| - if self.is_root(): |
1637 |
| - self._ancestors = [] |
1638 |
| - else: |
1639 |
| - self._ancestors = ancestors |
1640 |
| - |
1641 |
| - children = (node for node in self._descendants |
1642 |
| - if node.depth == self.depth + 1) |
1643 |
| - |
1644 |
| - for child in children: |
1645 |
| - child._set_hierarchy(self._descendants, ancestors=([self] + self._ancestors)) |
0 commit comments