8000 Fixed #6340 -- Reorder models to support dumpdata (#6341) · django-cms/django-cms@c5780a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit c5780a0

Browse files
heppstuxczpython
authored andcommitted
Fixed #6340 -- Reorder models to support dumpdata (#6341)
1 parent 0b930e5 commit c5780a0

File tree

2 files changed

+115
-110
lines changed

2 files changed

+115
-110
lines changed

cms/models/pagemodel.py

Lines changed: 109 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,115 @@
3434
logger = getLogger(__name__)
3535

3636

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+
37146
@python_2_unicode_compatible
38147
class Page(models.Model):
39148
"""
@@ -1534,112 +1643,3 @@ def get_root_page(cls, site):
15341643

15351644
def is_potential_home(self):
15361645
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))

cms/tests/test_fixture_loading.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
from django.core.management import call_command
1111

12+
from cms.models import CMSPlugin, Page, Placeholder, TreeNode
1213
from cms.test_utils.fixtures.navextenders import NavextendersFixture
1314
from cms.test_utils.testcases import CMSTestCase
14-
from cms.models import Page, Placeholder, CMSPlugin
1515

1616

1717
class FixtureTestCase(NavextendersFixture, CMSTestCase):
@@ -27,20 +27,25 @@ def test_fixture_load(self):
2727
call_command('dumpdata' BBD9 , 'cms', indent=3, stdout=output)
2828
original_ph = Placeholder.objects.count()
2929
original_pages = Page.objects.count()
30+
original_tree_nodes = TreeNode.objects.count()
3031
original_plugins = CMSPlugin.objects.count()
3132
Page.objects.all().delete()
3233
Placeholder.objects.all().delete()
34+
TreeNode.objects.all().delete()
3335
output.seek(0)
3436
with codecs.open(dump[1], 'w', 'utf-8') as dumpfile:
3537
dumpfile.write(output.read())
3638

39+
self.assertEqual(0, TreeNode.objects.count())
3740
self.assertEqual(0, Page.objects.count())
3841
self.assertEqual(0, Placeholder.objects.count())
3942
# Transaction disable, otherwise the connection it the test would be
4043
# isolated from the data loaded in the different command connection
4144
call_command('loaddata', dump[1], commit=False, stdout=output)
4245
self.assertEqual(10, Page.objects.count())
4346
self.assertEqual(original_pages, Page.objects.count())
47+
self.assertEqual(5, TreeNode.objects.count())
48+
self.assertEqual(original_tree_nodes, TreeNode.objects.count())
4449
# Placeholder number may differ if signals does not correctly handle
4550
# load data command
4651
self.assertEqual(original_ph, Placeholder.objects.count())

0 commit comments

Comments
 (0)
0