def test_submenu(self): """ Test making a simple menu. """ s = Site.objects.get(pk=1) # we always have a site: from django u = User() m = Menu(name="parent", url="/sitemap/", tree_id=s.id, user_id=1) m.save() m2 = Menu(name="main", url="/", tree_id=s.id, parent=m, user_id=1) m2.save() m3 = Menu(name="forum", url="/forum/", tree_id=s.id, parent=m, user_id=1) m3.save() self.assertEqual([m2, m3], m3.submenu(u))
def test_find_main(self): """ Find main page even if main page is not in menu. Also checks empty search. """ s = Site.objects.get(pk=1) # we always have a site: from django m = Menu(name="parent", url="/sitemap/", tree_id=s.id, user_id=1) m.save() m2 = Menu(name="main", url="/", tree_id=s.id, parent=m, user_id=1) m2.save() self.assertEqual(m2, Menu.find_active(s, "/")) self.assertEqual(m2, Menu.find_active(s, ""))
def new_menuitem(request): """Create a new menu item - return html for ajax injection""" tree_id = request.site.id if request.method == 'POST' and request.user.is_superuser: form = MenuForm(request.POST) if form.is_valid(): menu = form.save(commit=False) if not Menu.objects.filter(tree_id=tree_id, url=menu.url): menu.user = request.user menu.insert_at(menu.parent, 'last-child', True) #TODO: invalidate return HttpResponseRedirect(reverse("menu-list")) else: menu = Menu() menu.tree_id = tree_id if request.method == 'GET' and 'parent_id' in request.GET: menu.parent_id = request.GET['parent_id'] form = MenuForm(instance=menu) return render_to_response( 'samklang_menu/new_item.html', {'form': form}, context_instance=RequestContext(request))
def simple_menu(request, default_url="/"): if hasattr(request, 'site'): site = request.site else: site = Site.objects.get(pk=1) # find the closest parent to the active url active_link = Menu.find_active(site, request.path_info) # find the menu closest to the active_link if active_link: menu = active_link.submenu(request.user) else: menu = Menu.objects.get(tree_id=site.id, url="/").submenu(request.user) return html_menu(menu, active_link)
def add_to_menu(request, page_id): if request.user.is_superuser: if request.method == 'GET' and 'parent_id' in request.GET: parent_id = request.GET['parent_id'] else: parent_id = request.site.id parent = Menu.objects.get(pk=parent_id) page = Page.objects.get(pk=page_id) if not Menu.objects.filter(url=page.url): menuitem = Menu() menuitem.url = page.url menuitem.name = page.name menuitem.group = page.group menuitem.user = request.user menuitem.insert_at(parent, 'last-child', True) # TODO: make flash warning to show in redirect return HttpResponseRedirect(reverse("menu-list"))
def __get__(self, request, obj_type=None): if not hasattr(request, '_cached_active'): from samklang_menu.models import Menu request._cached_active = Menu.find_active(request.site, request.path_info) return request._cached_active
def test_find_forum_new(self): """ /forum/new/ does not exist, but /forum/ does. Checks for /forum/new without trailing slash as well. """ s = Site.objects.get(pk=1) # we always have a site: from django m = Menu(name="parent", url="/sitemap/", tree_id=s.id, user_id=1) m.save() m2 = Menu(name="main", url="/", tree_id=s.id, parent=m, user_id=1) m2.save() m3 = Menu(name="forum", url="/forum/", tree_id=s.id, parent=m, user_id=1) m3.save() self.assertEqual(m3, Menu.find_active(s, "/forum/new/")) self.assertEqual(m3, Menu.find_active(s, "/forum/new"))