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.
 
 
 
 
 
 

177 line
4.3 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe, re, os
  5. def delete_page_cache(path):
  6. if not path:
  7. path = ""
  8. cache = frappe.cache()
  9. cache.delete_keys("page:" + path)
  10. cache.delete_keys("page_context:" + path)
  11. cache.delete_keys("sitemap_options:" + path)
  12. def find_first_image(html):
  13. m = re.finditer("""<img[^>]*src\s?=\s?['"]([^'"]*)['"]""", html)
  14. try:
  15. return m.next().groups()[0]
  16. except StopIteration:
  17. return None
  18. def can_cache(no_cache=False):
  19. return not (frappe.conf.disable_website_cache or getattr(frappe.local, "no_cache", False) or no_cache)
  20. def get_comment_list(doctype, name):
  21. return frappe.db.sql("""select
  22. comment, comment_by_fullname, creation, comment_by
  23. from `tabComment` where comment_doctype=%s
  24. and ifnull(comment_type, "Comment")="Comment"
  25. and comment_docname=%s order by creation""", (doctype, name), as_dict=1) or []
  26. def get_home_page():
  27. def _get_home_page():
  28. role_home_page = frappe.get_hooks("role_home_page")
  29. home_page = None
  30. for role in frappe.get_roles():
  31. if role in role_home_page:
  32. home_page = role_home_page[role][-1]
  33. break
  34. if not home_page:
  35. home_page = frappe.get_hooks("home_page")
  36. if home_page:
  37. home_page = home_page[-1]
  38. if not home_page:
  39. home_page = frappe.db.get_value("Website Settings", None, "home_page") or "login"
  40. return home_page
  41. return frappe.cache().get_value("home_page", _get_home_page, user=True)
  42. def is_signup_enabled():
  43. if getattr(frappe.local, "is_signup_enabled", None) is None:
  44. frappe.local.is_signup_enabled = True
  45. if frappe.utils.cint(frappe.db.get_value("Website Settings",
  46. "Website Settings", "disable_signup")):
  47. frappe.local.is_signup_enabled = False
  48. return frappe.local.is_signup_enabled
  49. def cleanup_page_name(title):
  50. """make page name from title"""
  51. name = title.lower()
  52. name = re.sub('[~!@#$%^&*+()<>,."\'\?]', '', name)
  53. name = re.sub('[:/]', '-', name)
  54. name = '-'.join(name.split())
  55. # replace repeating hyphens
  56. name = re.sub(r"(-)\1+", r"\1", name)
  57. return name
  58. def get_shade(color, percent):
  59. color, color_format = detect_color_format(color)
  60. r, g, b, a = color
  61. avg = (float(int(r) + int(g) + int(b)) / 3)
  62. # switch dark and light shades
  63. if avg > 128:
  64. percent = -percent
  65. # stronger diff for darker shades
  66. if percent < 25 and avg < 64:
  67. percent = percent * 2
  68. new_color = []
  69. for channel_value in (r, g, b):
  70. new_color.append(get_shade_for_channel(channel_value, percent))
  71. r, g, b = new_color
  72. return format_color(r, g, b, a, color_format)
  73. def detect_color_format(color):
  74. if color.startswith("rgba"):
  75. color_format = "rgba"
  76. color = [c.strip() for c in color[5:-1].split(",")]
  77. elif color.startswith("rgb"):
  78. color_format = "rgb"
  79. color = [c.strip() for c in color[4:-1].split(",")] + [1]
  80. else:
  81. # assume hex
  82. color_format = "hex"
  83. if color.startswith("#"):
  84. color = color[1:]
  85. if len(color) == 3:
  86. # hex in short form like #fff
  87. color = "{0}{0}{1}{1}{2}{2}".format(*tuple(color))
  88. color = [int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16), 1]
  89. return color, color_format
  90. def get_shade_for_channel(channel_value, percent):
  91. v = int(channel_value) + int(int('ff', 16) * (float(percent)/100))
  92. if v < 0:
  93. v=0
  94. if v > 255:
  95. v=255
  96. return v
  97. def format_color(r, g, b, a, color_format):
  98. if color_format == "rgba":
  99. return "rgba({0}, {1}, {2}, {3})".format(r, g, b, a)
  100. elif color_format == "rgb":
  101. return "rgb({0}, {1}, {2})".format(r, g, b)
  102. else:
  103. # assume hex
  104. return "#{0}{1}{2}".format(convert_to_hex(r), convert_to_hex(g), convert_to_hex(b))
  105. def convert_to_hex(channel_value):
  106. h = hex(channel_value)[2:]
  107. if len(h) < 2:
  108. h = "0" + h
  109. return h
  110. def with_leading_slash(path):
  111. if path and not path.startswith("/"):
  112. path = "/" + path
  113. return path
  114. def get_full_index(doctype="Web Page"):
  115. """Returns full index of the website (on Web Page) upto the n-th level"""
  116. all_routes = []
  117. def get_children(parent):
  118. children = frappe.db.get_all(doctype, ["parent_website_route", "page_name", "title"],
  119. {"parent_website_route": parent}, order_by="idx asc")
  120. for d in children:
  121. d.url = with_leading_slash(os.path.join(d.parent_website_route or "", d.page_name))
  122. if d.url not in all_routes:
  123. d.children = get_children(d.url[1:])
  124. all_routes.append(d.url)
  125. return children
  126. return get_children("")