Skip to content

Commit 488dc8d

Browse files
author
Marius Schulze
committed
fix: remove orphaned grouper instances and add fallback for str()
Remove a grouper instance if the last Snippet instance connected to it gets deleted. Just in case also check for empty queryset in the name method of a grouper.
1 parent 8ed5eda commit 488dc8d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/djangocms_snippet/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def __str__(self):
3636
@property
3737
def name(self):
3838
snippet_qs = Snippet.admin_manager.filter(snippet_grouper=self)
39+
if not snippet_qs.exists():
40+
empty_grouper = _("Empty Snippet Grouper") # xgettext cannot handle f-strings
41+
return f"{empty_grouper} - {self.pk}"
3942
return snippet_qs.first().name or super().__str__
4043

4144
def snippet(self, show_editable=False): # NOQA: FBT002
@@ -108,6 +111,16 @@ def get_preview_url(self):
108111
args=[self.id],
109112
)
110113

114+
def delete(self, *args, **kwargs):
115+
# to avoid orphaned grouper instances,
116+
# check if this snippet was the only one connected to it
117+
# and if so, delete the grouper instance as well
118+
119+
if not Snippet.admin_manager.filter(snippet_grouper=self.snippet_grouper).exclude(id=self.id).exists():
120+
self.snippet_grouper.delete()
121+
122+
return super().delete(*args, **kwargs)
123+
111124

112125
# Plugin model - just a pointer to Snippet
113126
class SnippetPtr(CMSPlugin):

0 commit comments

Comments
 (0)