Skip to content

Commit 5a32a2f

Browse files
Cria novos métodos para adiciona e remoção de filhos
1 parent 14feae7 commit 5a32a2f

1 file changed

Lines changed: 50 additions & 5 deletions

File tree

opac_schema/v1/models.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# reverse_delete_rule:
1818
PULL,
1919
CASCADE,
20+
NULLIFY,
2021
# signals
2122
signals,
2223
)
@@ -56,7 +57,7 @@ class Pages(Document):
5657
journal = StringField()
5758
description = StringField()
5859
page_type = StringField(
59-
choices=("about", "journal", "free"),
60+
choices=("detail_about", "about", "journal", "free"),
6061
required=False,
6162
help_text="Categoria da página para organização e navegação"
6263
)
@@ -66,7 +67,7 @@ class Pages(Document):
6667
)
6768
parent_page = ReferenceField(
6869
"Pages",
69-
reverse_delete_rule=PULL,
70+
reverse_delete_rule=NULLIFY,
7071
required=False,
7172
help_text="Página pai (opcional). Define hierarquia"
7273
)
@@ -99,18 +100,62 @@ def save(self, *args, **kwargs):
99100
self.created_at = datetime.now()
100101
self.updated_at = datetime.now()
101102
if not self.slug_name:
102-
self.slug_name = slugify(self.name)
103+
self.set_slug()
104+
105+
old_parent, old_children = self.get_pages_parent_and_children()
103106

104107
# salva primeiro para garantir que self tem _id
105108
result = super(Pages, self).save(*args, **kwargs)
109+
110+
self.remove_self_from_old_parent(old_parent)
111+
self.add_self_to_new_parent()
112+
current_children = set(self.child_pages or [])
113+
self.remove_parent_from_removed_children(old_children, current_children)
114+
self.set_parent_for_current_children(current_children)
115+
116+
return result
117+
118+
def get_pages_parent_and_children(self):
119+
"""Retorna documento pai antigo e filhos antigos se existirem."""
120+
old_parent = None
121+
old_children = set()
122+
123+
if self.pk:
124+
try:
125+
old_doc = Pages.objects.get(pk=self.pk)
126+
old_parent = old_doc.parent_page
127+
old_children = set(old_doc.child_pages or [])
128+
except Pages.DoesNotExist:
129+
pass
130+
131+
return old_parent, old_children
106132

107-
# garante que ela esteja em child_pages do pai
133+
def remove_self_from_old_parent(self, old_parent):
134+
if old_parent and old_parent != self.parent_page:
135+
old_parent.update(pull__child_pages=self)
136+
137+
def add_self_to_new_parent(self):
108138
if self.parent_page:
109139
parent = self.parent_page
110140
if parent.id != self.id and self not in (parent.child_pages or []):
111141
parent.update(add_to_set__child_pages=self)
112142

113-
return result
143+
def remove_parent_from_removed_children(self, old_children, current_children):
144+
removed_children = old_children - current_children
145+
for child in removed_children:
146+
if child.parent_page == self:
147+
child.update(unset__parent_page=True)
148+
149+
def set_parent_for_current_children(self, current_children):
150+
for child in current_children:
151+
if child.id != self.id and child.parent_page != self:
152+
child.update(set__parent_page=self)
153+
154+
def set_slug(self):
155+
if not self.parent_page:
156+
self.slug_name = slugify(self.name)
157+
return
158+
self.slug_name = f"{self.parent_page.slug_name}/{slugify(self.name)}"
114159

115160

116161
class UseLicense(EmbeddedDocument):

0 commit comments

Comments
 (0)