You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

99 line
3.4 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. from frappe.website.utils import scrub_relative_urls, get_home_page, can_cache
  6. def get_sitemap_options(path):
  7. sitemap_options = None
  8. cache_key = "sitemap_options:{}".format(path)
  9. if can_cache():
  10. sitemap_options = frappe.cache().get_value(cache_key)
  11. if not sitemap_options:
  12. sitemap_options = build_sitemap_options(path)
  13. if can_cache(sitemap_options.no_cache):
  14. frappe.cache().set_value(cache_key, sitemap_options)
  15. return frappe._dict(sitemap_options)
  16. def build_sitemap_options(path):
  17. sitemap_options = frappe._dict(frappe.get_doc("Website Route", path).as_dict())
  18. home_page = get_home_page()
  19. if sitemap_options.controller:
  20. module = frappe.get_module(sitemap_options.controller)
  21. # get sitemap config fields too
  22. for prop in ("base_template_path", "template", "no_cache", "no_sitemap",
  23. "condition_field"):
  24. if hasattr(module, prop):
  25. sitemap_options[prop] = getattr(module, prop)
  26. sitemap_options.doctype = sitemap_options.ref_doctype
  27. sitemap_options.title = sitemap_options.page_title
  28. sitemap_options.pathname = sitemap_options.name
  29. # establish hierarchy
  30. sitemap_options.parents = frappe.db.sql("""select name, page_title from
  31. `tabWebsite Route`
  32. where lft < %s and rgt > %s
  33. order by lft asc""", (sitemap_options.lft, sitemap_options.rgt), as_dict=True)
  34. if not sitemap_options.no_sidebar:
  35. sitemap_options.children = get_route_children(sitemap_options.pathname, home_page)
  36. if not sitemap_options.children and sitemap_options.parent_website_route \
  37. and sitemap_options.parent_website_route!=home_page:
  38. sitemap_options.children = get_route_children(sitemap_options.parent_website_route, home_page)
  39. # determine templates to be used
  40. if not sitemap_options.base_template_path:
  41. app_base = frappe.get_hooks("base_template")
  42. sitemap_options.base_template_path = app_base[0] if app_base else "templates/base.html"
  43. return sitemap_options
  44. def get_route_children(pathname, home_page=None):
  45. if not home_page:
  46. home_page = get_home_page()
  47. if pathname==home_page or not pathname:
  48. children = frappe.db.sql("""select url as name, label as page_title,
  49. 1 as public_read from `tabTop Bar Item` where parentfield='sidebar_items' order by idx""",
  50. as_dict=True)
  51. else:
  52. children = frappe.db.sql("""select * from `tabWebsite Route`
  53. where ifnull(parent_website_route,'')=%s
  54. and public_read=1
  55. order by idx, page_title asc""", pathname, as_dict=True)
  56. if children:
  57. # if children are from generator and sort order is specified, then get that condition
  58. module = frappe.get_module(children[0].controller)
  59. if hasattr(module, "sort_by"):
  60. children = frappe.db.sql("""select t1.* from
  61. `tabWebsite Route` t1, `tab{ref_doctype}` t2
  62. where ifnull(t1.parent_website_route,'')=%s
  63. and t1.public_read=1
  64. and t1.docname = t2.name
  65. order by {sort_by}""".format(
  66. ref_doctype = children[0].ref_doctype,
  67. sort_by = module.sort_by),
  68. pathname, as_dict=True)
  69. children = [frappe.get_doc("Website Route", pathname)] + children
  70. return children
  71. def get_next(route):
  72. siblings = get_route_children(frappe.db.get_value("Website Route",
  73. route, "parent_website_route"))
  74. for i, r in enumerate(siblings):
  75. if i < len(siblings) - 1:
  76. if route==r.name:
  77. return siblings[i+1]