Skip to content

Commit

Permalink
Merge pull request #405 from opps/fix_save_channel_using_parent
Browse files Browse the repository at this point in the history
Recovery create channel using parent
  • Loading branch information
Thiago Avelino committed Dec 5, 2014
2 parents 0d2ae7a + 270d5db commit 86c2332
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions opps/channels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,11 @@ def update_long_slug(self):
else:
self.long_slug = self.slug

def _set_long_slug(self):
if self.parent:
return u"{0}/{1}".format(self.parent.long_slug, self.slug)
return self.slug

def channel_post_save(sender, instance, created, raw, using, update_fields,
*args, **kwargs):

if update_fields is None or 'slug' in update_fields:
instance.update_long_slug()
instance.save(update_fields=['long_slug'])
qs = Channel.objects.filter(
tree_id=instance.tree_id,
lft__gt=instance.lft,
rght__lt=instance.rght)

d = {instance.pk: instance.long_slug}
for pk, slug, parent in qs.values_list("pk", "slug", "parent"):
long_slug = "%s/%s" % (d[parent], slug)
Channel.objects.filter(pk=pk).update(long_slug=long_slug)
d[pk] = long_slug


post_save.connect(channel_post_save, sender=Channel)
def save(self, *args, **kwargs):
self.long_slug = self._set_long_slug()
super(Channel, self).save(*args, **kwargs)

0 comments on commit 86c2332

Please sign in to comment.