Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

144 righe
4.1 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 update_controller_context(context, controller):
  28. module = frappe.get_module(controller)
  29. if module:
  30. # get config fields
  31. for prop in ("base_template_path", "template", "no_cache", "no_sitemap",
  32. "condition_field"):
  33. if hasattr(module, prop):
  34. context[prop] = getattr(module, prop)
  35. if hasattr(module, "get_context"):
  36. ret = module.get_context(context)
  37. if ret:
  38. context.update(ret)
  39. if hasattr(module, "get_children"):
  40. context.children = module.get_children(context)
  41. def build_context(context):
  42. """get_context method of doc or module is supposed to render
  43. content templates and push it into context"""
  44. context = frappe._dict(context)
  45. if not "url_prefix" in context:
  46. context.url_prefix = ""
  47. if context.url_prefix and context.url_prefix[-1]!='/':
  48. context.url_prefix += '/'
  49. context.update(get_website_settings())
  50. context.update(frappe.local.conf.get("website_context") or {})
  51. # provide doc
  52. if context.doc:
  53. context.update(context.doc.as_dict())
  54. context.update(context.doc.website)
  55. if hasattr(context.doc, "get_context"):
  56. ret = context.doc.get_context(context)
  57. if ret:
  58. context.update(ret)
  59. for prop in ("no_cache", "no_sitemap"):
  60. if not prop in context:
  61. context[prop] = getattr(context.doc, prop, False)
  62. elif context.controller:
  63. # controller based context
  64. update_controller_context(context, context.controller)
  65. # controller context extensions
  66. context_controller_hooks = frappe.get_hooks("extend_website_page_controller_context") or {}
  67. for controller, extension in context_controller_hooks.items():
  68. if isinstance(extension, list):
  69. for ext in extension:
  70. if controller == context.controller:
  71. update_controller_context(context, ext)
  72. else:
  73. update_controller_context(context, extension)
  74. add_metatags(context)
  75. if context.show_sidebar:
  76. add_sidebar_data(context)
  77. # determine templates to be used
  78. if not context.base_template_path:
  79. app_base = frappe.get_hooks("base_template")
  80. context.base_template_path = app_base[0] if app_base else "templates/base.html"
  81. return context
  82. def add_sidebar_data(context):
  83. from frappe.utils.user import get_fullname_and_avatar
  84. import frappe.www.list
  85. if not context.sidebar_items:
  86. sidebar_items = json.loads(frappe.cache().get_value('portal_menu_items') or '[]')
  87. if not sidebar_items:
  88. sidebar_items = frappe.get_all('Portal Menu Item',
  89. fields=['title', 'route', 'reference_doctype', 'show_always'],
  90. filters={'enabled': 1, 'parent': 'Portal Settings'}, order_by='idx asc')
  91. frappe.cache().set_value('portal_menu_items', json.dumps(sidebar_items))
  92. context.sidebar_items = sidebar_items
  93. info = get_fullname_and_avatar(frappe.session.user)
  94. context["fullname"] = info.fullname
  95. context["user_image"] = info.avatar
  96. context["user"] = info.name
  97. def add_metatags(context):
  98. tags = context.get("metatags")
  99. if tags:
  100. if not "twitter:card" in tags:
  101. tags["twitter:card"] = "summary"
  102. if not "og:type" in tags:
  103. tags["og:type"] = "article"
  104. if tags.get("name"):
  105. tags["og:title"] = tags["twitter:title"] = tags["name"]
  106. if tags.get("description"):
  107. tags["og:description"] = tags["twitter:description"] = tags["description"]
  108. if tags.get("image"):
  109. tags["og:image"] = tags["twitter:image:src"] = tags["image"]