This is an automated email from the ASF dual-hosted git repository.

dill0wn pushed a commit to branch dw/8421
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 346a45ab604d3658c55bb9d2f4178ba3aee85a48
Author: Dillon Walls <[email protected]>
AuthorDate: Wed Mar 23 00:32:04 2022 +0000

    [#8421] SEO - omit /blogs/ from sitemap if no posts
---
 ForgeBlog/forgeblog/main.py           | 23 +++++++++++++++++++++++
 ForgeBlog/forgeblog/tests/test_app.py | 23 +++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/ForgeBlog/forgeblog/main.py b/ForgeBlog/forgeblog/main.py
index 417c669..246e99e 100644
--- a/ForgeBlog/forgeblog/main.py
+++ b/ForgeBlog/forgeblog/main.py
@@ -155,6 +155,29 @@ class ForgeBlogApp(Application):
             return [
                 SitemapEntry(menu_id, '.')[self.sidebar_menu()]]
 
+    def sitemap_xml(self):
+        """
+        Used for generating sitemap.xml.
+        If the root page has default content, omit it from the sitemap.xml.
+        Assumes :attr:`main_menu` will return an entry pointing to the root 
page.
+        :return: a list of :class:`SitemapEntries <allura.app.SitemapEntry>`
+        """
+        if self.should_noindex():
+            return []
+        return self.main_menu()
+
+    def should_noindex(self):
+        if not has_access(self, 'read'):
+            return True
+
+        post = BM.BlogPost.query.get(
+            app_config_id=self.config._id,
+            state='published',
+            deleted=False)
+        if post:
+            return False
+        return True
+
     @property
     def show_discussion(self):
         if 'show_discussion' in self.config.options:
diff --git a/ForgeBlog/forgeblog/tests/test_app.py 
b/ForgeBlog/forgeblog/tests/test_app.py
index a1b706a..98cc59f 100644
--- a/ForgeBlog/forgeblog/tests/test_app.py
+++ b/ForgeBlog/forgeblog/tests/test_app.py
@@ -49,6 +49,29 @@ class TestApp:
         c.project.uninstall_app('blog')
         assert not BM.BlogPost.query.get(title='Test title')
 
+    @td.with_tool('test', 'Blog', 'blog')
+    def test_sitemap_xml(self):
+        assert_equal([], c.app.sitemap_xml())
+        BM.BlogPost.new(
+            title='Blog Title',
+            state='draft',
+            text='This is my first blog Post',
+        )
+        assert_equal([], c.app.sitemap_xml())
+        BM.BlogPost.new(
+            title='Blog Title',
+            state='published',
+            text='This is my first blog Post',
+            deleted=True
+        )
+        assert_equal([], c.app.sitemap_xml())
+        BM.BlogPost.new(
+            title='Blog Title',
+            state='published',
+            text='This is my first blog Post',
+        )
+        assert_equal(1, len(c.app.sitemap_xml()))
+
 
 class TestBulkExport:
 

Reply via email to