# Path: lemon/models.py
# class MenuItem(models.Model):
# 
#     name = models.CharField(_('item name'), max_length=50)
#     content_type = models.ForeignKey(ContentType,
#                                      verbose_name=_('content type'))
#     section = models.ForeignKey(MenuSection, related_name='sections',
#                                 verbose_name=_('section'))
#     position = models.PositiveIntegerField(_('position'), default=0)
# 
#     class Meta:
#         ordering = ['position']
#         verbose_name = _('menu item')
#         verbose_name_plural = _('menu items')
# 
#     def __unicode__(self):
#         return self.name
# 
# Path: lemon/settings.py
# CONFIG = {
#     'MARKUP_WIDGET': None,
#     'MENU_LINKS': (),
# }


from django.core.urlresolvers import reverse, NoReverseMatch
from django.template import Library, Variable, Node
from django.template import TemplateSyntaxError, VariableDoesNotExist
from django.template.defaulttags import URLNode
from ..models import MenuItem
from ..settings import CONFIG


register = Library()


class MainMenuItemURLNode(Node):

    def __init__(self, content_type):
        self.content_type = Variable(content_type)

    def render(self, context):
        try:
            content_type = self.content_type.resolve(context)
        except VariableDoesNotExist:
            return ''
        opts = content_type.model_class()._meta
        app_label = opts.app_label
        module_name = opts.module_name
        view_name = 'admin:%s_%s_changelist' % \
            (app_label, module_name)
        try:
            return reverse(view_name)
        except NoReverseMatch:
            return ''


@register.inclusion_tag('lemon/main_menu.html')
def main_menu():
    queryset = MenuItem.objects.select_related('section', 'content_type')
    queryset = queryset.order_by('section__position', 'position')

# Path: lemon/models.py
# class MenuItem(models.Model):
# 
#     name = models.CharField(_('item name'), max_length=50)
#     content_type = models.ForeignKey(ContentType,
#                                      verbose_name=_('content type'))
#     section = models.ForeignKey(MenuSection, related_name='sections',
#                                 verbose_name=_('section'))
#     position = models.PositiveIntegerField(_('position'), default=0)
# 
#     class Meta:
#         ordering = ['position']
#         verbose_name = _('menu item')
#         verbose_name_plural = _('menu items')
# 
#     def __unicode__(self):
#         return self.name
# 
# Path: lemon/settings.py
# CONFIG = {
#     'MARKUP_WIDGET': None,
#     'MENU_LINKS': (),
# }


# Path: lemon/templatetags/main_menu.py
from django.core.urlresolvers import reverse, NoReverseMatch
from django.template import Library, Variable, Node
from django.template import TemplateSyntaxError, VariableDoesNotExist
from django.template.defaulttags import URLNode
from ..models import MenuItem
from ..settings import CONFIG



register = Library()


class MainMenuItemURLNode(Node):

    def __init__(self, content_type):
        self.content_type = Variable(content_type)

    def render(self, context):
        try:
            content_type = self.content_type.resolve(context)
        except VariableDoesNotExist:
            return ''
        opts = content_type.model_class()._meta
        app_label = opts.app_label
        module_name = opts.module_name
        view_name = 'admin:%s_%s_changelist' % \
            (app_label, module_name)
        try:
            return reverse(view_name)
        except NoReverseMatch:
            return ''


@register.inclusion_tag('lemon/main_menu.html')
def main_menu():
    queryset = MenuItem.objects.select_related('section', 'content_type')
    queryset = queryset.order_by('section__position', 'position')

    return {'menu_items': queryset, 'menu_links': CONFIG['MENU_LINKS']}