Explorar el Código

Add helper metrhod to get all BlogIndexPage's child BlogPage's tags

David Ray hace 8 años
padre
commit
c4d05b43c6
Se han modificado 1 ficheros con 20 adiciones y 1 borrados
  1. 20 1
      bakerydemo/blog/models.py

+ 20 - 1
bakerydemo/blog/models.py

@@ -138,10 +138,29 @@ class BlogIndexPage(BasePageFieldsMixin, RoutablePageMixin, Page):
                 messages.add_message(request, messages.INFO, msg)
             return redirect(self.url)
 
-        blogs = BlogPage.objects.filter(tags=tag).live().descendant_of(self)
+        blogs = self.blogs(tag=tag)
 
         context = {
             'tag': tag,
             'blogs': blogs
         }
         return render(request, 'blog/blog_index_page.html', context)
+
+    def blogs(self, tag=None):
+        """
+        Return the child BlogPage objects for this BlogPageIndex. Optional
+        filter by tag
+        """
+        blogs = BlogPage.objects.live().descendant_of(self)
+        if tag:
+            blogs = blogs.filter(tags=tag)
+        return blogs
+
+    def get_child_tags(self):
+        """
+        Returns the list of Tags for child pages.
+        """
+        tags = [x.get_tags for x in self.blogs()]
+        tags = [item for sublist in tags for item in sublist]
+        tags = sorted(set(tags))
+        return tags