# Path: nuit/utils.py
# def user_can_see_view(view, user):
#     '''
#     Can the user see this view?
#     '''
#     try:
#         can_view = all([test_view(test, view, user) for test in get_user_tests(view.func)])
#     except PermissionDenied:
#         return False
# 
#     return can_view


from distutils.version import StrictVersion
from django import template, get_version
from django.contrib.messages import constants
from django.core.urlresolvers import resolve, reverse, NoReverseMatch, Resolver404
from django.template.loader import get_template
from django.template.base import token_kwargs, FilterExpression
from django.template.loader_tags import do_extends, ExtendsNode
from django.template.defaultfilters import slugify
from django.utils.html import format_html
from six.moves.urllib.parse import urlparse
from six import iteritems as _iteritems
from ..utils import user_can_see_view
import asteval

@register.simple_tag(takes_context=True)
def menu_item(context, link, name, id=None, current=False, unavailable=False, always_display=False):
    '''
    Renders an HTML anchor element in a list element.
    '''
    # pylint: disable=W0622
    if not id:
        id = slugify(name)
    classes = []
    if current:
        classes.append('current')
    if unavailable:
        classes.append('unavailable')

    try:
        url = reverse(link)
    except NoReverseMatch:
        url = link
        display = True

    parsed_url = urlparse(url)

    if not parsed_url.netloc and not always_display:
        user = context['request'].user if 'request' in context else None
        try:
            view = resolve(parsed_url.path)
        except Resolver404:
            display = False
        else:

# Path: nuit/utils.py
# def user_can_see_view(view, user):
#     '''
#     Can the user see this view?
#     '''
#     try:
#         can_view = all([test_view(test, view, user) for test in get_user_tests(view.func)])
#     except PermissionDenied:
#         return False
# 
#     return can_view


# Path: nuit/templatetags/nuit.py
from distutils.version import StrictVersion
from django import template, get_version
from django.contrib.messages import constants
from django.core.urlresolvers import resolve, reverse, NoReverseMatch, Resolver404
from django.template.loader import get_template
from django.template.base import token_kwargs, FilterExpression
from django.template.loader_tags import do_extends, ExtendsNode
from django.template.defaultfilters import slugify
from django.utils.html import format_html
from six.moves.urllib.parse import urlparse
from six import iteritems as _iteritems
from ..utils import user_can_see_view
import asteval


@register.simple_tag(takes_context=True)
def menu_item(context, link, name, id=None, current=False, unavailable=False, always_display=False):
    '''
    Renders an HTML anchor element in a list element.
    '''
    # pylint: disable=W0622
    if not id:
        id = slugify(name)
    classes = []
    if current:
        classes.append('current')
    if unavailable:
        classes.append('unavailable')

    try:
        url = reverse(link)
    except NoReverseMatch:
        url = link
        display = True

    parsed_url = urlparse(url)

    if not parsed_url.netloc and not always_display:
        user = context['request'].user if 'request' in context else None
        try:
            view = resolve(parsed_url.path)
        except Resolver404:
            display = False
        else:

            display = user_can_see_view(view, user)