Ejemplo n.º 1
0
    def test_refresh_menu_from_sitemap__update_page(self):
        from navigation.models import Menu, MenuItem, Sitemap
        from django.contrib.flatpages.models import FlatPage
        from navigation.utils import refresh_menu_from_sitemap

        sitemap = Sitemap.objects.get(slug='flatpages')
        menu = Menu.objects.get(sitemap=sitemap)

        # initialize
        refresh_menu_from_sitemap(menu, sitemap)

        # modify
        page = FlatPage.objects.get(title="Birds")
        page.title = "Nice Birds"
        page.url = "/nice-birds/"
        page.save()

        # refresh
        refresh_menu_from_sitemap(menu, sitemap)

        # check
        self.assertEquals(0, MenuItem.objects.filter(title='Birds').count())
        self.assertEquals(1,
                          MenuItem.objects.filter(title='Nice Birds').count())
        self.assertEquals(0, MenuItem.objects.filter(url='/birds/').count())
        self.assertEquals(1,
                          MenuItem.objects.filter(url='/nice-birds/').count())
Ejemplo n.º 2
0
    def test_refresh_menu_from_sitemap__empty_menu(self):
        from navigation.models import Menu, MenuItem, Sitemap
        from navigation.utils import refresh_menu_from_sitemap
        from mock import MagicMock

        sitemap = Sitemap.objects.get(slug='flatpages')
        menu = Menu.objects.get(sitemap=sitemap)

        # initialize
        items = [
            {
                'uuid': 'home',
                'title': 'Home',
                'location': '/',
                'parent': None
            },
            {
                'uuid': 'child',
                'title': 'Child',
                'location': '/child/',
                'parent': '/'
            },
        ]
        sitemap.get_items = MagicMock(return_value=items)
        refresh_menu_from_sitemap(menu, sitemap)

        # modify
        items[1]['parent'] = None
        sitemap.get_items = MagicMock(return_value=[])

        # refresh
        refresh_menu_from_sitemap(menu, sitemap)

        # check
        self.assertEquals(0, MenuItem.objects.filter(title='Child').count())
Ejemplo n.º 3
0
    def test_refresh_menu_from_sitemap__remove_some_pages(self):
        ''' Menu item knows what page it belongs to. It should be deleted
        when its page is deleted. '''
        from navigation.models import Menu, MenuItem, Sitemap
        from navigation.utils import refresh_menu_from_sitemap
        from mock import MagicMock

        sitemap = Sitemap.objects.get(slug='flatpages')
        menu = Menu.objects.get(sitemap=sitemap)

        # initialize
        items_4 = [
            {
                'uuid': 'home',
                'title': 'Home',
                'location': '/',
                'parent': None
            },
            {
                'uuid': 'child',
                'title': 'Child',
                'location': '/child/',
                'parent': '/'
            },
            {
                'uuid': 'grandchild',
                'title': 'Grandchild',
                'location': '/child/grandchild/',
                'parent': '/child/'
            },
            {
                'uuid': 'grandgrandchild',
                'title': 'Grandchild',
                'location': '/child/grandchild/grandgrandchild/',
                'parent': '/child/grandchild/'
            },
        ]
        sitemap.get_items = MagicMock(return_value=items_4)
        refresh_menu_from_sitemap(menu, sitemap)

        # check
        self.assertEquals(4, len(menu.list_all_items()))

        # unattache sitemap from menu
        menu.sitemap = None
        menu.save()

        # remove page
        items_1 = [
            {
                'uuid': 'child',
                'title': 'Child',
                'location': '/'
            },
        ]
        sitemap.get_items = MagicMock(return_value=items_1)
        refresh_menu_from_sitemap(menu, sitemap)

        # check
        self.assertEquals(1, len(menu.list_all_items()))
Ejemplo n.º 4
0
    def test_refresh_menu_from_sitemap__empty_menu(self):        
        from navigation.models import Menu, MenuItem, Sitemap
        from navigation.utils import refresh_menu_from_sitemap
        from mock import MagicMock

        sitemap = Sitemap.objects.get(slug='flatpages')
        menu = Menu.objects.get(sitemap=sitemap)
        
        # initialize
        items = [
            {'uuid' : 'home', 'title': 'Home', 'location':'/', 'parent': None},
            {'uuid' : 'child', 'title': 'Child', 'location':'/child/', 'parent': '/'},
            ]
        sitemap.get_items = MagicMock(return_value=items)
        refresh_menu_from_sitemap(menu, sitemap)
        
        # modify
        items[1]['parent'] = None
        sitemap.get_items = MagicMock(return_value=[])
        
        # refresh
        refresh_menu_from_sitemap(menu, sitemap)
        
        # check
        self.assertEquals(0, MenuItem.objects.filter(title='Child').count())
Ejemplo n.º 5
0
 def test_refresh_menu_from_sitemap__update_page(self):
     from navigation.models import Menu, MenuItem, Sitemap
     from django.contrib.flatpages.models import FlatPage
     from navigation.utils import refresh_menu_from_sitemap
     
     sitemap = Sitemap.objects.get(slug='flatpages')
     menu = Menu.objects.get(sitemap=sitemap)
     
     # initialize
     refresh_menu_from_sitemap(menu, sitemap)
     
     # modify
     page = FlatPage.objects.get(title="Birds")
     page.title = "Nice Birds"
     page.url = "/nice-birds/"
     page.save()
     
     # refresh
     refresh_menu_from_sitemap(menu, sitemap)
     
     # check
     self.assertEquals(0, MenuItem.objects.filter(title='Birds').count())
     self.assertEquals(1, MenuItem.objects.filter(title='Nice Birds').count())
     self.assertEquals(0, MenuItem.objects.filter(url='/birds/').count())
     self.assertEquals(1, MenuItem.objects.filter(url='/nice-birds/').count())
Ejemplo n.º 6
0
 def test_refresh_menu_from_sitemap__remove_all_pages(self):
     ''' Menu item knows what page it belongs to. It should be deleted
     when its page is deleted. '''
     from navigation.models import Menu, MenuItem, Sitemap
     from navigation.utils import refresh_menu_from_sitemap
     from mock import MagicMock
     
     sitemap = Sitemap.objects.get(slug='flatpages')
     menu = Menu.objects.get(sitemap=sitemap)
     
     # initialize
     items_4 = [
         {'uuid' : 'home', 'title': 'Home', 'location':'/', 'parent': None},
         {'uuid' : 'child', 'title': 'Child', 'location':'/child/', 'parent': '/'},
         {'uuid' : 'grandchild', 'title': 'Grandchild', 'location':'/child/grandchild/', 'parent': '/child/'},
         {'uuid' : 'grandgrandchild', 'title': 'Grandchild', 'location':'/child/grandchild/grandgrandchild/', 'parent': '/child/grandchild/'},
         ]
     sitemap.get_items = MagicMock(return_value=items_4)
     refresh_menu_from_sitemap(menu, sitemap)
     
     # check
     self.assertEquals(4, len(menu.list_all_items()))
     
     # unattache sitemap from menu
     menu.sitemap = None
     menu.save()
     
     # remove pages
     sitemap.get_items = MagicMock(return_value=[])
     refresh_menu_from_sitemap(menu, sitemap)
     
     # check
     self.assertEquals(0, len(menu.list_all_items()))
     
Ejemplo n.º 7
0
    def test_refresh_menu_from_sitemap__with_flatpages(self):
        from navigation.models import Menu, Sitemap
        from navigation.utils import refresh_menu_from_sitemap
        
        sitemap = Sitemap.objects.get(slug='flatpages')
        menu = Menu.objects.get(sitemap=sitemap)
        
        # refresh
        refresh_menu_from_sitemap(menu, sitemap)

        # check if items were added
        self.assertEquals(6, len(menu.list_all_items()) )
        self.assertEquals(3, len(menu.list_top_items()) )
        
        # check if item was initialized
        birds_item = filter(lambda i: i.sitemap_item_title == 'Birds', menu.list_all_items())[0]
        self.assertEquals('Birds', birds_item.title)
        self.assertEquals('/birds/', birds_item.url)
        self.assertEquals('enabled', birds_item.sitemap_item_status)
        self.assertIsNotNone(birds_item.sitemap_item_id)
Ejemplo n.º 8
0
    def test_refresh_menu_from_sitemap__with_flatpages(self):
        from navigation.models import Menu, Sitemap
        from navigation.utils import refresh_menu_from_sitemap

        sitemap = Sitemap.objects.get(slug='flatpages')
        menu = Menu.objects.get(sitemap=sitemap)

        # refresh
        refresh_menu_from_sitemap(menu, sitemap)

        # check if items were added
        self.assertEquals(6, len(menu.list_all_items()))
        self.assertEquals(3, len(menu.list_top_items()))

        # check if item was initialized
        birds_item = filter(lambda i: i.sitemap_item_title == 'Birds',
                            menu.list_all_items())[0]
        self.assertEquals('Birds', birds_item.title)
        self.assertEquals('/birds/', birds_item.url)
        self.assertEquals('enabled', birds_item.sitemap_item_status)
        self.assertIsNotNone(birds_item.sitemap_item_id)
def get_navigation_breadcrumbs(current_path, menu=None, sitemap=None):
	
	
	# get or create menu for sitemap as needed
	if menu == None and sitemap:
		sitemap = Sitemap.current_objects.get(slug=sitemap)
		try:
			menu = Menu.current_objects.filter(sitemap=sitemap).get()
		except ObjectDoesNotExist:
			# create menu
			menu = Menu()
			menu.name = unicode(sitemap)
			menu.site = sitemap.site
			menu.sitemap = sitemap
			menu.save()
			
			from navigation.utils import refresh_menu_from_sitemap
			refresh_menu_from_sitemap(menu, sitemap)
		
	
	# create breadcrumbs from menu
	items = get_breadcrumbs_from_menu(current_path, menu)
	
	if not items:
		return None
	
	# add home item if needed
	from urlparse import urlparse
	top_url = urlparse(items[-1].url)
	
		
	if top_url.path != '' and top_url.path != '/':
		home_item = MenuItem()
		home_item.title = pgettext('navigation', 'Home')
		home_item.url = '/'
		items.append(home_item)
		
	items.reverse()
	return items