def save(self, force_insert=False, **kwargs): from rvcms.cms.utils import clean_ranks # Calculate level old_level = self.level if self.parent: self.level = self.parent.level + 1 else: self.level = 0 if self.pk: new_parent = self.parent old_parent = MenuItem.objects.get(pk=self.pk).parent if old_parent != new_parent: # If so, we need to recalculate the new ranks for the item and its siblings (both old and new ones). if new_parent: clean_ranks( new_parent.children()) # Clean ranks for new siblings self.rank = new_parent.children().count() super(MenuItem, self).save( force_insert, **kwargs ) # Save menu item in DB. It has now officially changed parent. if old_parent: clean_ranks( old_parent.children()) # Clean ranks for old siblings else: super(MenuItem, self).save(force_insert, **kwargs) # Save menu item in DB else: # Saving the menu item for the first time (i.e creating the object) if not self.has_siblings(): # No siblings - initial rank is 0. self.rank = 0 else: # Has siblings - initial rank is highest sibling rank plus 1. siblings = self.siblings().order_by('-rank') self.rank = siblings[0].rank + 1 super(MenuItem, self).save(force_insert, **kwargs) # If level has changed, force children to refresh their own level if old_level != self.level: for child in self.children(): child.save( ) # Just saving is enough, it'll refresh its level correctly.
def save(self, force_insert=False, **kwargs): from rvcms.cms.utils import clean_ranks # Calculate level old_level = self.level if self.parent: self.level = self.parent.level + 1 else: self.level = 0 if self.pk: new_parent = self.parent old_parent = MenuItem.objects.get(pk=self.pk).parent if old_parent != new_parent: # If so, we need to recalculate the new ranks for the item and its siblings (both old and new ones). if new_parent: clean_ranks(new_parent.children()) # Clean ranks for new siblings self.rank = new_parent.children().count() super(MenuItem, self).save(force_insert, **kwargs) # Save menu item in DB. It has now officially changed parent. if old_parent: clean_ranks(old_parent.children()) # Clean ranks for old siblings else: super(MenuItem, self).save(force_insert, **kwargs) # Save menu item in DB else: # Saving the menu item for the first time (i.e creating the object) if not self.has_siblings(): # No siblings - initial rank is 0. self.rank = 0 else: # Has siblings - initial rank is highest sibling rank plus 1. siblings = self.siblings().order_by('-rank') self.rank = siblings[0].rank + 1 super(MenuItem, self).save(force_insert, **kwargs) # If level has changed, force children to refresh their own level if old_level != self.level: for child in self.children(): child.save() # Just saving is enough, it'll refresh its level correctly.
def delete(self): from rvcms.cms.utils import clean_ranks old_parent = self.parent super(MenuItem, self).delete() if old_parent: clean_ranks(old_parent.children())