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.
 
 
 
 
 
 

130 lines
3.6 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. import webnotes
  23. import webnotes.model.doc
  24. import webnotes.model.code
  25. conn = webnotes.conn
  26. class Page:
  27. """
  28. A page class helps in loading a Page in the system. On loading
  29. * Page will import Client Script from other pages where specified by `$import(page_name)`
  30. * Execute dynamic HTML if the `content` starts with `#python`
  31. """
  32. def __init__(self, name):
  33. self.name = name
  34. def get_from_files(self, doc):
  35. """
  36. Loads page info from files in module
  37. """
  38. from webnotes.modules import get_module_path, scrub
  39. import os
  40. path = os.path.join(get_module_path(doc.module), 'page', scrub(doc.name))
  41. # script
  42. fpath = os.path.join(path, scrub(doc.name) + '.js')
  43. if os.path.exists(fpath):
  44. with open(fpath, 'r') as f:
  45. doc.fields['__script'] = f.read()
  46. # css
  47. fpath = os.path.join(path, scrub(doc.name) + '.css')
  48. if os.path.exists(fpath):
  49. with open(fpath, 'r') as f:
  50. doc.style = f.read()
  51. # html
  52. fpath = os.path.join(path, scrub(doc.name) + '.html')
  53. if os.path.exists(fpath):
  54. with open(fpath, 'r') as f:
  55. doc.content = f.read()
  56. def load(self):
  57. """
  58. Returns :term:`doclist` of the `Page`
  59. """
  60. doclist = webnotes.model.doc.get('Page', self.name)
  61. doc = doclist[0]
  62. # load from module
  63. if doc.module:
  64. self.get_from_files(doc)
  65. # process
  66. self.process_content(doc)
  67. return doclist
  68. @webnotes.whitelist()
  69. def get(name):
  70. """
  71. Return the :term:`doclist` of the `Page` specified by `name`
  72. """
  73. from webnotes.model.code import get_obj
  74. page = get_obj('Page', name)
  75. page.get_from_files()
  76. return page.doclist
  77. @webnotes.whitelist(allow_guest=True)
  78. def getpage():
  79. """
  80. Load the page from `webnotes.form` and send it via `webnotes.response`
  81. """
  82. doclist = get(webnotes.form.getvalue('name'))
  83. # send
  84. webnotes.response['docs'] = doclist
  85. def get_page_path(page_name, module):
  86. """get path of the page html file"""
  87. import os
  88. import conf
  89. from webnotes.modules import scrub
  90. return os.path.join(conf.modules_path, 'erpnext', scrub(module), \
  91. 'page', scrub(page_name), scrub(page_name) + '.html')
  92. def get_page_html(page_name):
  93. """get html of page, called from webnotes.cms.index"""
  94. p = webnotes.conn.sql("""select module, content from tabPage where name=%s""", \
  95. page_name, as_dict=1)
  96. if not p:
  97. return ''
  98. else:
  99. import os
  100. p=p[0]
  101. path = get_page_path(page_name, p['module'])
  102. if os.path.exists(path):
  103. with open(path, 'r') as f:
  104. return f.read()
  105. else:
  106. return p['content']