25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

website_group.py 2.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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
  5. doctype = "Website Group"
  6. no_cache = 1
  7. def get_context(controller, page_options):
  8. group, view = guess_group_view(controller, page_options)
  9. try:
  10. if not has_access(group, view):
  11. raise webnotes.PermissionError
  12. context = get_initial_context(group, view, controller)
  13. context["access"] = get_access(group)
  14. context["content"] = get_content(context)
  15. return context
  16. except webnotes.DoesNotExistError:
  17. return {
  18. "content": '<div class="alert alert-danger full-page">'
  19. 'The page you are looking for does not exist.</div>'
  20. }
  21. except webnotes.PermissionError:
  22. return {
  23. "content": '<div class="alert alert-danger full-page">'
  24. 'You are not permitted to view this page.</div>'
  25. }
  26. def get_initial_context(group, view, controller):
  27. def _get_initial_context():
  28. if controller:
  29. group = controller.doc
  30. else:
  31. group = webnotes.doc("Website Group", group)
  32. # move all this to webutils
  33. parents = webnotes.conn.sql("""select name, unit_title from tabUnit
  34. where lft < %s and rgt > %s order by lft asc""", (unit.lft, unit.rgt), as_dict=1)
  35. # update title
  36. title = unit.unit_title
  37. views = get_views(unit)
  38. view_options = views.get(view, {})
  39. if view_options:
  40. title += " - " + view_options["label"]
  41. views = sorted([opts for v, opts in views.items()], key=lambda d: d.get("idx"))
  42. context = {
  43. "name": unit.name,
  44. "public_read": unit.public_read,
  45. "title": "Aam Aadmi Party: " + title,
  46. "unit_title": title,
  47. "public_write": unit.public_write,
  48. "parents": parents,
  49. "children": get_child_unit_items(unit.name, public_read=1),
  50. "unit": unit.fields,
  51. "view": view,
  52. "views": views,
  53. "view_options": view_options
  54. }
  55. return context
  56. if webnotes.conf.get("disable_website_cache"):
  57. return _get_unit_context(unit, view)
  58. return webnotes.cache().get_value("unit_context:{unit}:{view}".format(unit=unit.lower(), view=view),
  59. lambda:_get_unit_context(unit, view))
  60. def get_content(context):
  61. pass
  62. def guess_group_view(controller, page_options):
  63. group = page_options.docname
  64. view = None
  65. pathname = webnotes.request.path[1:]
  66. if "/" in pathname:
  67. view = pathname.split("/", 1)[1]
  68. if not view:
  69. get_views = webnotes.get_hooks("website_group_views:{}".controller.doc.group_type)
  70. if get_views:
  71. for v, opts in webnotes.get_attr(get_views)(group).items():
  72. if opts.get("default"):
  73. view = v
  74. break
  75. return group, view
  76. def has_access(group, view):
  77. access = get_access(group)
  78. if view=="settings":
  79. return access.get("admin")
  80. elif view in ("add", "edit"):
  81. return access.get("write")
  82. else:
  83. return access.get("read")