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.
 
 
 
 
 
 

129 line
3.9 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import webnotes
  24. import webnotes.model.doc
  25. import webnotes.model.code
  26. conn = webnotes.conn
  27. class Page:
  28. """
  29. A page class helps in loading a Page in the system. On loading
  30. * Page will import Client Script from other pages where specified by `$import(page_name)`
  31. * Execute dynamic HTML if the `content` starts with `#python`
  32. """
  33. def __init__(self, name):
  34. self.name = name
  35. def get_from_files(self, doc):
  36. """
  37. Loads page info from files in module
  38. """
  39. from webnotes.modules import get_module_path, scrub
  40. import os
  41. path = os.path.join(get_module_path(doc.module), 'page', scrub(doc.name))
  42. # script
  43. fpath = os.path.join(path, scrub(doc.name) + '.js')
  44. if os.path.exists(fpath):
  45. with open(fpath, 'r') as f:
  46. doc.fields['__script'] = f.read()
  47. # css
  48. fpath = os.path.join(path, scrub(doc.name) + '.css')
  49. if os.path.exists(fpath):
  50. with open(fpath, 'r') as f:
  51. doc.style = f.read()
  52. # html
  53. fpath = os.path.join(path, scrub(doc.name) + '.html')
  54. if os.path.exists(fpath):
  55. with open(fpath, 'r') as f:
  56. doc.content = f.read()
  57. def load(self):
  58. """
  59. Returns :term:`doclist` of the `Page`
  60. """
  61. doclist = webnotes.model.doc.get('Page', self.name)
  62. doc = doclist[0]
  63. # load from module
  64. if doc.module:
  65. self.get_from_files(doc)
  66. # process
  67. self.process_content(doc)
  68. return doclist
  69. @webnotes.whitelist()
  70. def get(name):
  71. """
  72. Return the :term:`doclist` of the `Page` specified by `name`
  73. """
  74. from webnotes.model.code import get_obj
  75. page = get_obj('Page', name, with_children=1)
  76. page.get_from_files()
  77. return page.doclist
  78. @webnotes.whitelist(allow_guest=True)
  79. def getpage():
  80. """
  81. Load the page from `webnotes.form` and send it via `webnotes.response`
  82. """
  83. page = webnotes.form_dict.get('name')
  84. doclist = get(page)
  85. if has_permission(doclist):
  86. # load translations
  87. if webnotes.lang != "en":
  88. from webnotes.modules import get_doc_path
  89. from webnotes.translate import get_lang_data
  90. d = doclist[0]
  91. messages = get_lang_data(get_doc_path(d.module, d.doctype, d.name),
  92. webnotes.lang, 'js')
  93. webnotes.response["__messages"] = messages
  94. webnotes.response['docs'] = doclist
  95. else:
  96. webnotes.response['403'] = 1
  97. raise webnotes.PermissionError, 'No read permission for Page %s' % \
  98. (doclist[0].title or page, )
  99. def has_permission(page_doclist):
  100. if webnotes.user.name == "Administrator" or "System Manager" in webnotes.user.get_roles():
  101. return True
  102. page_roles = [d.role for d in page_doclist if d.fields.get("doctype")=="Page Role"]
  103. if webnotes.user.name == "Guest" and not (page_roles and "Guest" in page_roles):
  104. return False
  105. elif page_roles and not (set(page_roles) & set(webnotes.user.get_roles())):
  106. return False
  107. return True