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.

website_group.py 3.6 KiB

пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. import webnotes
  4. from webnotes.webutils import get_access, can_cache
  5. from webnotes.templates.website_group.post import clear_post_cache
  6. doctype = "Website Group"
  7. no_cache = 1
  8. def get_context(context):
  9. bean = webnotes.bean(context.ref_doctype, context.docname)
  10. group, view = guess_group_view(bean, context)
  11. try:
  12. if not has_access(group, view):
  13. raise webnotes.PermissionError
  14. group_context = get_group_context(group, view, bean, context)
  15. group_context.update(context)
  16. return group_context
  17. except webnotes.DoesNotExistError:
  18. return {
  19. "content": '<div class="alert alert-danger full-page">'
  20. 'The page you are looking for does not exist.</div>'
  21. }
  22. except webnotes.PermissionError:
  23. return {
  24. "content": '<div class="alert alert-danger full-page">'
  25. 'You are not permitted to view this page.</div>'
  26. }
  27. def get_group_context(group, view, bean, context):
  28. cache_key = "website_group_context:{}:{}".format(group, view)
  29. views = get_views(bean.doc.group_type)
  30. view = webnotes._dict(views.get(view))
  31. if can_cache(view.get("no_cache")):
  32. group_context = webnotes.cache().get_value(cache_key)
  33. if group_context:
  34. return group_context
  35. group_context = build_group_context(group, view, bean, views, context)
  36. if can_cache(view.get("no_cache")):
  37. webnotes.cache().set_value(cache_key, group_context)
  38. return group_context
  39. def build_group_context(group, view, bean, views, context):
  40. title = "{} - {}".format(bean.doc.group_title, view.get("label"))
  41. for name, opts in views.iteritems():
  42. opts["url"] = opts["url"].format(pathname=context.pathname, post="")
  43. group_context = webnotes._dict({
  44. "group": bean.doc.fields,
  45. "view": view,
  46. "views": (v[1] for v in sorted(views.iteritems(), key=lambda (k, v): v.get("idx"))),
  47. "title": title
  48. })
  49. handler = get_handler(bean.doc.group_type)
  50. if handler:
  51. group_context.update(handler.get_context(group_context))
  52. return group_context
  53. def guess_group_view(bean, context):
  54. group = context.docname
  55. view = webnotes.form_dict.view
  56. if not view:
  57. for v, opts in get_views(bean.doc.group_type).iteritems():
  58. if opts.get("default"):
  59. view = v
  60. break
  61. return group, view
  62. def get_handler(group_type):
  63. handler = webnotes.get_hooks("website_group_handler:{}".format(group_type))
  64. if handler:
  65. return webnotes.get_module(handler[0])
  66. def get_views(group_type):
  67. from copy import deepcopy
  68. handler = get_handler(group_type)
  69. if handler and hasattr(handler, "get_views"):
  70. return deepcopy(handler.get_views() or {})
  71. return {}
  72. def has_access(group, view):
  73. access = get_access(group)
  74. if view=="settings":
  75. return access.get("admin")
  76. elif view in ("add", "edit"):
  77. return access.get("write")
  78. else:
  79. return access.get("read")
  80. def clear_cache(page_name=None, website_group=None):
  81. if page_name or website_group:
  82. filters = {"page_name": page_name} if page_name else website_group
  83. website_group = webnotes.conn.get_value("Website Group", filters,
  84. ["page_name", "group_type"], as_dict=True)
  85. if not website_group:
  86. return
  87. website_groups = [website_group]
  88. else:
  89. clear_post_cache()
  90. website_groups = webnotes.conn.sql("""select page_name, group_type from `tabWebsite Group`""", as_dict=True)
  91. cache = webnotes.cache()
  92. for group in website_groups:
  93. for view in get_views(group.group_type):
  94. cache.delete_value("website_group_context:{}:{}".format(group.page_name, view))
  95. def clear_event_cache():
  96. for group in webnotes.conn.sql_list("""select name from `tabWebsite Group` where group_type='Event'"""):
  97. clear_unit_views(website_group=group)