Commits

tehfink committed d089253

now working for paged urls, more comments

Comments (0)

Files changed (1)

cmsplugin_photologue/navigation.py

 from django.utils.translation import ugettext_lazy as _
 from cms.utils.navigation import NavigationNode
 from photologue.models import Gallery, Photo
+from photologue.urls import gallery_args, photo_args
 
-from photologue.urls import gallery_args, photo_args
 
 def get_nodes(request):
     """ creates mptt style parent-child list for extending django-cms navigation (menu & breadcrumb); structured on photologue.urls """
         #create the base path of the URL ('gallery/' or 'photo/')
         base_node = NavigationNode(model, reverse('pl-%s-archive' % model,))
         base_node.childrens = []
-        
+
+        #create node for object_list view (photologue uses this view for pagination)
+        #urlpattern to match: url(r'^photo/page/(?P<page>[0-9]+)/$', 'object_list', {'queryset': Photo.objects.filter(is_public=True), 'allow_empty': True, 'paginate_by': 20}, name='pl-photo-list'),
+        #path example: request.META['PATH_INFO'] = u'/galleries/photo/page/2/'  or  u'/galleries/gallery/page/1/'
+        try:
+            #page_path gets the relevant parts of the path for paging
+            page_path = request.META['PATH_INFO'].split('/')[-4:-1]     #eg: '/mygalleries/gallery/page/1/' ->  ['gallery', 'page', '1']
+            #execute only if page_path is a photologue paged url for this model
+            assert len(page_path) == 3 and [model, 'page', str(int(page_path[2]))] == page_path
+            #name the node with a string, reverse to the paged named url, and pass the page value from page_path
+            pages_node = NavigationNode('page %s' % page_path[2], reverse('pl-%s-list' % model, kwargs=dict(page=page_path[2])))
+            #modify url used in href, this doesn't work because NavigationNode uses the passed url for get_absolute_url
+#            pages_node.get_absolute_url = lambda: reverse('pl-%s-list' % model, kwargs=dict(page=1))
+            #attach the node to the main list
+            base_node.childrens.append(pages_node)
+        except:
+            #fail silently if not applicable
+            pass
+
+        #create NavigationNodes for the queryset
         for item in queryset:
             #get the date for this query object; 'date_added' called 'date published' in photologue admin
             date = item.date_added
                 
             if not item.title_slug in slug_done:
                 slug_done.append(item.title_slug)
-#                item_node = NavigationNode(item.title, item.get_absolute_url())
                 #one node child for the day node
                 item_node = NavigationNode(item.title, reverse('pl-%s-detail' % model, kwargs=dict(year=date.year, month=date.strftime('%b').lower(), day=date.day, slug=item.title_slug)))
                 day_node.childrens.append(item_node)
                 item_node = NavigationNode(item.title, reverse('pl-%s' % model, kwargs=dict(slug=item.title_slug)))
                 base_node.childrens.append(item_node)
 
-#how to determine page, if it is calculated in the view?
-#            if not item.page in page_done:
-#                page_done.append(item.page)
-#                item_node = NavigationNode(item.title, item.get_absolute_url())
-#                #one node child for the day node
-#                page_node = NavigationNode(item.title, reverse('pl-%s-list' % model, kwargs=dict(page=1)))
-#                base_node.childrens.append(item_node)
-
         res.append(base_node)
     return res