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
def save_model(self, request, obj, form, change): super(MenuAdmin, self).save_model(request, obj, form, change) old_items = obj.list_all_items() id_map = {} # update each item if request.POST.get('menuitem-max') is not None: for i in range(0, int(request.POST.get('menuitem-max')) + 1): my_id = request.POST.get('menuitem-%d-id' % (i,)) my_order = request.POST.get('menuitem-%d-order' % (i,)) my_url = request.POST.get('menuitem-%d-url' % (i,)) my_title = request.POST.get('menuitem-%d-title' % (i,)) my_status = request.POST.get('menuitem-%d-status' % (i,)) my_sitemap_item_id = request.POST.get('menuitem-%d-sitemap-item-id' % (i,)) if my_id: try: item = obj.get_item(id=int(my_id)) except Exception as e: item = MenuItem() item.menu = obj item.title = my_title item.url = my_url item.order = my_order item.parent = None item.sitemap_item_id = my_sitemap_item_id item.save() id_map[my_id] = item if item in old_items: old_items.remove(item) # assign parent for i in range(0, int(request.POST.get('menuitem-max')) + 1): my_id = request.POST.get('menuitem-%d-id' % (i,)) parent_id = request.POST.get('menuitem-%s-parent-id' % (i,)) if my_id: item = id_map.get(my_id) item.parent = id_map.get(parent_id) item.save() # remove old items for item in old_items: item.delete()