# Path: cmsplugin_shop/models.py
# class Node(models.Model):
#     parent      = TreeForeignKey('self', verbose_name=_('category'), blank=True, null=True,
#                     related_name='children', limit_choices_to={'product':None})
#     name        = models.CharField(_('name'), max_length=250)
#     slug        = models.SlugField(_('slug'), max_length=250, db_index=True, unique=False)
#     summary     = HTMLField(_('summary'), blank=True, default='')
#     description = HTMLField(_('description'), blank=True, default='')
#     photo       = FilerImageField(verbose_name='Fotka', null=True, blank=True, on_delete=models.SET_NULL)
#     page_title  = models.CharField(_('page title'), max_length=250, blank=True, null=True,
#                     help_text=_('overwrite the title (html title tag)'))
#     menu_title  = models.CharField(_('menu title'), max_length=250, blank=True, null=True,
#                     help_text=_('overwrite the title in the menu'))
#     meta_desc   = models.TextField(_('meta description'), blank=True, default='',
#                     help_text=_('the text displayed in search engines'))
#     active      = models.BooleanField(default=False, verbose_name=_('active'))
# 
#     class Meta:
#         ordering            = ('tree_id', 'lft')
#         unique_together     = [('parent', 'slug')]
#         verbose_name        = _('tree node')
#         verbose_name_plural = _('tree nodes')
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         path = '/'.join([n.slug for n in self.get_ancestors()]+[self.slug])
#         return reverse('Catalog:catalog', args=(path,))
# 
#     def get_edit_url(self):
#         return reverse('admin:{}_{}_change'.format(self._meta.app_label, self._meta.model_name), args=(self.id,))
# 
#     def get_name(self):
#         return self.name
# 
#     def get_page_title(self):
#         return self.page_title or self.name
# 
#     def get_menu_title(self):
#         return self.menu_title or self.name
# 
#     def save(self, *args, **kwargs):
#         errors = self._perform_unique_checks([(Node, ('parent', 'slug'))])
#         if errors:
#             raise ValidationError(errors)
#         super(Node, self).save(*args, **kwargs)
# 
# class Category(Node):
#     class Meta:
#         ordering            = ('tree_id', 'lft')
#         verbose_name        = _('category')
#         verbose_name_plural = _('categories')


from cms.menu_bases import CMSAttachMenu
from django.db.models.signals import post_delete, post_save
from django.utils.translation import ugettext_lazy as _
from menus.base import NavigationNode
from menus.menu_pool import menu_pool
from .models import Node, Category




class CatalogMenu(CMSAttachMenu):
    name = _('Catalog')

    def get_nodes(self, request):
        """
        This method is used to build the menu tree.
        """
        if request.toolbar.use_draft:
            qs = Node.objects.order_by('tree_id', 'lft')
        else:
            qs = Node.objects.filter(active=True).order_by('tree_id', 'lft')
        return [
            NavigationNode(
                node.get_menu_title(),
                node.get_absolute_url(),
                node.id,
                node.parent and node.parent.id or None,
            ) for node in qs
        ]


menu_pool.register_menu(CatalogMenu)

def invalidate_menu_cache(sender, **kwargs):
    menu_pool.clear()


# Path: cmsplugin_shop/models.py
# class Node(models.Model):
#     parent      = TreeForeignKey('self', verbose_name=_('category'), blank=True, null=True,
#                     related_name='children', limit_choices_to={'product':None})
#     name        = models.CharField(_('name'), max_length=250)
#     slug        = models.SlugField(_('slug'), max_length=250, db_index=True, unique=False)
#     summary     = HTMLField(_('summary'), blank=True, default='')
#     description = HTMLField(_('description'), blank=True, default='')
#     photo       = FilerImageField(verbose_name='Fotka', null=True, blank=True, on_delete=models.SET_NULL)
#     page_title  = models.CharField(_('page title'), max_length=250, blank=True, null=True,
#                     help_text=_('overwrite the title (html title tag)'))
#     menu_title  = models.CharField(_('menu title'), max_length=250, blank=True, null=True,
#                     help_text=_('overwrite the title in the menu'))
#     meta_desc   = models.TextField(_('meta description'), blank=True, default='',
#                     help_text=_('the text displayed in search engines'))
#     active      = models.BooleanField(default=False, verbose_name=_('active'))
# 
#     class Meta:
#         ordering            = ('tree_id', 'lft')
#         unique_together     = [('parent', 'slug')]
#         verbose_name        = _('tree node')
#         verbose_name_plural = _('tree nodes')
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         path = '/'.join([n.slug for n in self.get_ancestors()]+[self.slug])
#         return reverse('Catalog:catalog', args=(path,))
# 
#     def get_edit_url(self):
#         return reverse('admin:{}_{}_change'.format(self._meta.app_label, self._meta.model_name), args=(self.id,))
# 
#     def get_name(self):
#         return self.name
# 
#     def get_page_title(self):
#         return self.page_title or self.name
# 
#     def get_menu_title(self):
#         return self.menu_title or self.name
# 
#     def save(self, *args, **kwargs):
#         errors = self._perform_unique_checks([(Node, ('parent', 'slug'))])
#         if errors:
#             raise ValidationError(errors)
#         super(Node, self).save(*args, **kwargs)
# 
# class Category(Node):
#     class Meta:
#         ordering            = ('tree_id', 'lft')
#         verbose_name        = _('category')
#         verbose_name_plural = _('categories')


# Path: cmsplugin_shop/menu.py
from cms.menu_bases import CMSAttachMenu
from django.db.models.signals import post_delete, post_save
from django.utils.translation import ugettext_lazy as _
from menus.base import NavigationNode
from menus.menu_pool import menu_pool
from .models import Node, Category





class CatalogMenu(CMSAttachMenu):
    name = _('Catalog')

    def get_nodes(self, request):
        """
        This method is used to build the menu tree.
        """
        if request.toolbar.use_draft:
            qs = Node.objects.order_by('tree_id', 'lft')
        else:
            qs = Node.objects.filter(active=True).order_by('tree_id', 'lft')
        return [
            NavigationNode(
                node.get_menu_title(),
                node.get_absolute_url(),
                node.id,
                node.parent and node.parent.id or None,
            ) for node in qs
        ]


menu_pool.register_menu(CatalogMenu)

def invalidate_menu_cache(sender, **kwargs):
    menu_pool.clear()


post_save.connect(invalidate_menu_cache, sender=Category)