Example #1
0
def test_base_menu_entry():
    entry = BaseMenuEntry()

    # test Resolver
    assert entry.url is None

    admin_url = ("shuup_admin:dashboard", (), {})
    entry._url = admin_url
    assert entry.url == "/sa/"
    assert entry.original_url == admin_url

    entry._url = "shuup_admin:dashboard"
    assert entry.url == "/sa/"

    entry._url = "/sa/"
    assert entry.url == "/sa/"

    with pytest.raises(TypeError):
        entry._url = 1
        assert not entry.url

    # test iterator
    assert entry.has_entries is False
    entry.entries = [
        BaseMenuEntry(),
        BaseMenuEntry(),
    ]
    assert entry.has_entries is True
    assert len(list(e for e in entry)) == 2
Example #2
0
def customize_menu(entries, user):  # noqa (C901)
    """
    Merge system menu with customized admin menu
    """
    customized_admin_menu = configuration.get(None, "admin_menu_user_{}".format(user.pk))
    if customized_admin_menu:

        def unset_mismatched(menu):
            """
            find and remove unmatched entries from customized menu tree
            it can be when menu entry was removed from system
            """
            indexes = []
            for index, entry in enumerate(menu.get('entries', [])):
                unset_mismatched(entry)
                if isinstance(entry, dict):
                    indexes.append(index)
            for index in indexes[::-1]:
                del menu['entries'][index]

        def find_entry(menu, entry):
            """
            find recursively entry in menu
            """
            if menu['id'] == entry.id:
                return menu
            for node in menu.get('entries', []):
                n = find_entry(node, entry)
                if n:
                    return n

        def assign_entry(customized_menu, entry):
            """
            Find and replace customized entry with system menu entry
            set entry name, hidden flag from customized menu entry
            """
            custom_entries = customized_menu.get('entries', [])
            for index, node in enumerate(custom_entries):
                if node['id'] == entry.id:
                    custom_entry = custom_entries[index]
                    entry.name = custom_entry['name']
                    entry.is_hidden = custom_entry['is_hidden']
                    entry.entries = custom_entry.get('entries', [])
                    entry.ordering = index
                    custom_entries[index] = entry
                    return custom_entries[index]
                else:
                    return_entry = assign_entry(custom_entries[index], entry)
                    if return_entry:
                        return return_entry

        def transform_menu(customized_menu, menu):
            """
            Recursively sort system menu entries and assign it to the customized menu
            """
            indexes = []
            for index, entry in enumerate(menu.entries):
                transform_menu(customized_menu, entry)
                custom_entry = assign_entry(customized_menu, entry)
                if not custom_entry:
                    parent_menu = find_entry(customized_menu, menu)
                    if parent_menu:
                        parent_menu.get('entries', []).append(entry)
                        indexes.append(index)
                else:
                    indexes.append(index)
            # remove assigned entries from system menu
            for index in indexes[::-1]:
                del menu.entries[index]
            return menu

        customized_menu = {
            'id': 'root',
            'name': 'root',
            'entries': customized_admin_menu,
        }
        system_menu = BaseMenuEntry()
        system_menu.identifier = 'root'
        system_menu.entries = entries
        transform_menu(customized_menu, system_menu)
        unset_mismatched(customized_menu)

        return customized_menu['entries'] + system_menu['entries']
    else:
        return entries
Example #3
0
def customize_menu(entries, request):  # noqa (C901)
    """
    Merge system menu with customized admin menu
    """
    customized_admin_menu = configuration.get(
        None, CUSTOM_ADMIN_MENU_USER_PREFIX.format(request.user.pk))
    if not customized_admin_menu:
        supplier = get_supplier(request)
        if supplier:
            customized_admin_menu = configuration.get(
                None, CUSTOM_ADMIN_MENU_SUPPLIER_KEY)
        elif getattr(request.user, "is_superuser", False):
            customized_admin_menu = configuration.get(
                None, CUSTOM_ADMIN_MENU_SUPERUSER_KEY)
        else:
            customized_admin_menu = configuration.get(
                None, CUSTOM_ADMIN_MENU_STAFF_KEY)

    if customized_admin_menu and isinstance(customized_admin_menu, dict):
        """
        If menu configuration is stored to dict try to find right configuration with
        current language and fallback to first stored configuration
        """
        customized_admin_menu = customized_admin_menu.get(
            get_language(),
            customized_admin_menu.get(first(customized_admin_menu)))

    if customized_admin_menu:

        def unset_mismatched(menu):
            """
            find and remove unmatched entries from customized menu tree
            it can be when menu entry was removed from system
            """
            indexes = []
            for index, entry in enumerate(menu.get('entries', [])):
                unset_mismatched(entry)
                if isinstance(entry, dict):
                    indexes.append(index)
            for index in indexes[::-1]:
                del menu['entries'][index]

        def find_entry(menu, entry):
            """
            find recursively entry in menu
            """
            if menu['id'] == entry.id:
                return menu
            for node in menu.get('entries', []):
                n = find_entry(node, entry)
                if n:
                    return n

        def assign_entry(customized_menu, entry):
            """
            Find and replace customized entry with system menu entry
            set entry name, hidden flag from customized menu entry
            """
            custom_entries = customized_menu.get('entries', [])
            for index, node in enumerate(custom_entries):
                if node['id'] == entry.id:
                    custom_entry = custom_entries[index]
                    entry.name = custom_entry['name']
                    entry.is_hidden = custom_entry['is_hidden']
                    entry.entries = custom_entry.get('entries', [])
                    entry.ordering = index
                    custom_entries[index] = entry
                    return custom_entries[index]
                else:
                    return_entry = assign_entry(custom_entries[index], entry)
                    if return_entry:
                        return return_entry

        def transform_menu(customized_menu, menu):
            """
            Recursively sort system menu entries and assign it to the customized menu
            """
            indexes = []
            for index, entry in enumerate(menu.entries):
                transform_menu(customized_menu, entry)
                custom_entry = assign_entry(customized_menu, entry)
                if not custom_entry:
                    parent_menu = find_entry(customized_menu, menu)
                    if parent_menu:
                        parent_menu.get('entries', []).append(entry)
                        indexes.append(index)
                else:
                    indexes.append(index)
            # remove assigned entries from system menu
            for index in indexes[::-1]:
                del menu.entries[index]
            return menu

        customized_menu = {
            'id': 'root',
            'name': 'root',
            'entries': customized_admin_menu,
        }
        system_menu = BaseMenuEntry()
        system_menu.identifier = 'root'
        system_menu.entries = entries
        transform_menu(customized_menu, system_menu)
        unset_mismatched(customized_menu)

        return customized_menu['entries'] + system_menu['entries']
    else:
        return entries