No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

128 líneas
3.6 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
  5. import json
  6. from frappe.website.doctype.website_settings.website_settings import get_website_settings
  7. from frappe.website.router import get_page_context
  8. def get_context(path, args=None):
  9. if args and args.source:
  10. context = args
  11. else:
  12. context = get_page_context(path)
  13. if args:
  14. context.update(args)
  15. context = build_context(context)
  16. if hasattr(frappe.local, 'request'):
  17. # for <body data-path=""> (remove leading slash)
  18. # path could be overriden in render.resolve_from_map
  19. context["path"] = frappe.local.request.path[1:]
  20. else:
  21. context["path"] = path
  22. # set using frappe.respond_as_web_page
  23. if hasattr(frappe.local, 'response') and frappe.local.response.get('context'):
  24. context.update(frappe.local.response.context)
  25. # print frappe.as_json(context)
  26. return context
  27. def build_context(context):
  28. """get_context method of doc or module is supposed to render
  29. content templates and push it into context"""
  30. context = frappe._dict(context)
  31. if not "url_prefix" in context:
  32. context.url_prefix = ""
  33. if context.url_prefix and context.url_prefix[-1]!='/':
  34. context.url_prefix += '/'
  35. context.update(get_website_settings())
  36. context.update(frappe.local.conf.get("website_context") or {})
  37. # provide doc
  38. if context.doc:
  39. context.update(context.doc.as_dict())
  40. context.update(context.doc.website)
  41. if hasattr(context.doc, "get_context"):
  42. ret = context.doc.get_context(context)
  43. if ret:
  44. context.update(ret)
  45. for prop in ("no_cache", "no_sitemap"):
  46. if not prop in context:
  47. context[prop] = getattr(context.doc, prop, False)
  48. elif context.controller:
  49. module = frappe.get_module(context.controller)
  50. if module:
  51. # get config fields
  52. for prop in ("base_template_path", "template", "no_cache", "no_sitemap",
  53. "condition_field"):
  54. if hasattr(module, prop):
  55. context[prop] = getattr(module, prop)
  56. if hasattr(module, "get_context"):
  57. ret = module.get_context(context)
  58. if ret:
  59. context.update(ret)
  60. if hasattr(module, "get_children"):
  61. context.children = module.get_children(context)
  62. add_metatags(context)
  63. if context.show_sidebar:
  64. add_sidebar_data(context)
  65. # determine templates to be used
  66. if not context.base_template_path:
  67. app_base = frappe.get_hooks("base_template")
  68. context.base_template_path = app_base[0] if app_base else "templates/base.html"
  69. return context
  70. def add_sidebar_data(context):
  71. from frappe.utils.user import get_fullname_and_avatar
  72. import frappe.www.list
  73. if not context.sidebar_items:
  74. sidebar_items = json.loads(frappe.cache().get_value('portal_menu_items') or '[]')
  75. if not sidebar_items:
  76. sidebar_items = frappe.get_all('Portal Menu Item',
  77. fields=['title', 'route', 'reference_doctype', 'show_always'],
  78. filters={'enabled': 1, 'parent': 'Portal Settings'}, order_by='idx asc')
  79. frappe.cache().set_value('portal_menu_items', json.dumps(sidebar_items))
  80. context.sidebar_items = sidebar_items
  81. info = get_fullname_and_avatar(frappe.session.user)
  82. context["fullname"] = info.fullname
  83. context["user_image"] = info.avatar
  84. def add_metatags(context):
  85. tags = context.get("metatags")
  86. if tags:
  87. if not "twitter:card" in tags:
  88. tags["twitter:card"] = "summary"
  89. if not "og:type" in tags:
  90. tags["og:type"] = "article"
  91. if tags.get("name"):
  92. tags["og:title"] = tags["twitter:title"] = tags["name"]
  93. if tags.get("description"):
  94. tags["og:description"] = tags["twitter:description"] = tags["description"]
  95. if tags.get("image"):
  96. tags["og:image"] = tags["twitter:image:src"] = tags["image"]