From b2eceae7c927d1b908f8e90e6a911430a7ccfce7 Mon Sep 17 00:00:00 2001 From: Romain Derie Date: Wed, 12 Jun 2019 14:52:59 +0000 Subject: [PATCH] [IMP] website_sale: show shop category final URL in sitemap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit, when an user would visit a subcategory in the ecommerce, he would see a slugified URL, containing all the category hierarchy, which is good for SEO purpose. For example, if `Wheel` is a child of `Car` which is a child of `Vehicule`, then the URL would be `/shop/category/vehicule-car-wheel-3`. But on the sitemap, the parents would not be prefixed to the URL, thus the sitemap would simply show `/shop/category/wheel-3`. This commit ensure the category parents are added to the final URL of the sitemap, as in the ecommerce. task-1889972 closes odoo/odoo#34073 Signed-off-by: Jérémy Kersten (jke) --- addons/website_sale/controllers/main.py | 19 ++++++++++++++--- addons/website_sale/tests/__init__.py | 1 + addons/website_sale/tests/test_sitemap.py | 25 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 addons/website_sale/tests/test_sitemap.py diff --git a/addons/website_sale/controllers/main.py b/addons/website_sale/controllers/main.py index b5083e3f7c6..e6a36a2969e 100644 --- a/addons/website_sale/controllers/main.py +++ b/addons/website_sale/controllers/main.py @@ -10,6 +10,7 @@ from odoo.addons.base.models.ir_qweb_fields import nl2br from odoo.addons.http_routing.models.ir_http import slug from odoo.addons.payment.controllers.portal import PaymentProcessing from odoo.addons.website.controllers.main import QueryURL +from odoo.addons.website.models.ir_http import sitemap_qs2dom from odoo.exceptions import ValidationError from odoo.addons.website.controllers.main import Website from odoo.addons.website_form.controllers.main import WebsiteForm @@ -197,12 +198,24 @@ class WebsiteSale(http.Controller): return expression.AND(domains) + def sitemap_shop(env, rule, qs): + if not qs or qs.lower() in '/shop': + yield {'loc': '/shop'} + + Category = env['product.public.category'] + dom = sitemap_qs2dom(qs, '/shop/category', Category._rec_name) + dom += env['website'].get_current_website().website_domain() + for cat in Category.search(dom): + loc = '/shop/category/%s' % slug(cat) + if not qs or qs.lower() in loc: + yield {'loc': loc} + @http.route([ '''/shop''', '''/shop/page/''', - '''/shop/category/''', - '''/shop/category//page/''' - ], type='http', auth="public", website=True) + '''/shop/category/''', + '''/shop/category//page/''' + ], type='http', auth="public", website=True, sitemap=sitemap_shop) def shop(self, page=0, category=None, search='', ppg=False, **post): add_qty = int(post.get('add_qty', 1)) if category: diff --git a/addons/website_sale/tests/__init__.py b/addons/website_sale/tests/__init__.py index 40b4a8266e4..bfdce9609a3 100644 --- a/addons/website_sale/tests/__init__.py +++ b/addons/website_sale/tests/__init__.py @@ -1,5 +1,6 @@ from . import test_customize from . import test_sale_process +from . import test_sitemap from . import test_website_sale_cart_recovery from . import test_website_sale_mail from . import test_website_sale_pricelist diff --git a/addons/website_sale/tests/test_sitemap.py b/addons/website_sale/tests/test_sitemap.py new file mode 100644 index 00000000000..2baf167ea3d --- /dev/null +++ b/addons/website_sale/tests/test_sitemap.py @@ -0,0 +1,25 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo.tests import HttpCase, tagged + + +@tagged('post_install', '-at_install') +class TestSitemap(HttpCase): + + def setUp(self): + super(TestSitemap, self).setUp() + + self.cats = self.env['product.public.category'].create([{ + 'name': 'Level 0', + }, { + 'name': 'Level 1', + }, { + 'name': 'Level 2', + }]) + self.cats[2].parent_id = self.cats[1].id + self.cats[1].parent_id = self.cats[0].id + + def test_01_shop_route_sitemap(self): + resp = self.url_open('/sitemap.xml') + level2_url = '/shop/category/level-0-level-1-level-2-%s' % self.cats[2].id + self.assertTrue(level2_url in resp.text, "Category entry in sitemap should be prefixed by its parent hierarchy.")