From e89fd7a2af9adbb6641cf2e61b2742fbe5d0824b Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Sun, 10 Apr 2022 16:22:56 +0530 Subject: [PATCH 1/2] fix: sitemap condition --- frappe/www/sitemap.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/frappe/www/sitemap.py b/frappe/www/sitemap.py index d2fbdc434d..5f1ed96c6d 100644 --- a/frappe/www/sitemap.py +++ b/frappe/www/sitemap.py @@ -49,7 +49,10 @@ def get_public_pages_from_doctypes(): controller = get_controller(doctype) meta = frappe.get_meta(doctype) condition_field = meta.is_published_field or controller.website.condition_field - + + if not condition_field: + continue + try: res = frappe.db.get_all(doctype, ['route', 'name', 'modified'], { condition_field: 1 }) for r in res: From 93ee8b2e385a9cb37c02ee81b8e833cdcb48f4d8 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Tue, 12 Apr 2022 13:19:33 +0530 Subject: [PATCH 2/2] refactor(minor): Sitemap --- frappe/www/sitemap.py | 68 +++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/frappe/www/sitemap.py b/frappe/www/sitemap.py index 5f1ed96c6d..988c299909 100644 --- a/frappe/www/sitemap.py +++ b/frappe/www/sitemap.py @@ -1,4 +1,4 @@ -# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors +# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors # License: MIT. See LICENSE from urllib.parse import quote @@ -11,55 +11,65 @@ from frappe.website.router import get_pages no_cache = 1 base_template_path = "www/sitemap.xml" + def get_context(context): """generate the sitemap XML""" - - # the site might be accessible from multiple host_names - # for e.g gadgets.erpnext.com and gadgetsinternational.com - # so it should be picked from the request - host = frappe.utils.get_host_name_from_request() - links = [] + for route, page in get_pages().items(): if page.sitemap: - links.append({ - "loc": get_url(quote(page.name.encode("utf-8"))), - "lastmod": nowdate() - }) + links.append( + {"loc": get_url(quote(page.name.encode("utf-8"))), "lastmod": nowdate()} + ) for route, data in get_public_pages_from_doctypes().items(): - links.append({ - "loc": get_url(quote((route or "").encode("utf-8"))), - "lastmod": get_datetime(data.get("modified")).strftime("%Y-%m-%d") - }) + links.append( + { + "loc": get_url(quote((route or "").encode("utf-8"))), + "lastmod": f"{data['modified']:%Y-%m-%d}", + } + ) + + return {"links": links} - return {"links":links} def get_public_pages_from_doctypes(): - '''Returns pages from doctypes that are publicly accessible''' + """Returns pages from doctypes that are publicly accessible""" def get_sitemap_routes(): routes = {} - doctypes_with_web_view = [d.name for d in frappe.db.get_all('DocType', { - 'has_web_view': 1, - 'allow_guest_to_view': 1 - })] + doctypes_with_web_view = frappe.get_all( + "DocType", + filters={"has_web_view": True, "allow_guest_to_view": True}, + pluck="name", + ) for doctype in doctypes_with_web_view: controller = get_controller(doctype) meta = frappe.get_meta(doctype) - condition_field = meta.is_published_field or controller.website.condition_field - + condition_field = ( + meta.is_published_field or controller.website.condition_field + ) + if not condition_field: continue - - try: - res = frappe.db.get_all(doctype, ['route', 'name', 'modified'], { condition_field: 1 }) - for r in res: - routes[r.route] = {"doctype": doctype, "name": r.name, "modified": r.modified} + try: + res = frappe.get_all( + doctype, + fields=["route", "name", "modified"], + filters={condition_field: True}, + ) except Exception as e: - if not frappe.db.is_missing_column(e): raise e + if not frappe.db.is_missing_column(e): + raise e + + for r in res: + routes[r.route] = { + "doctype": doctype, + "name": r.name, + "modified": r.modified, + } return routes