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.
 
 
 
 
 
 

140 satır
3.8 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. doc.script = None
  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 process_content(self, doc):
  58. """
  59. Put in template and generate dynamic if starts with #!python
  60. """
  61. content = ''
  62. # eval content
  63. if doc.content and doc.content.startswith('#!python'):
  64. from webnotes.model.code import execute
  65. doc.__content = execute(doc.content).get('content')
  66. else:
  67. doc.__content = content
  68. def load(self):
  69. """
  70. Returns :term:`doclist` of the `Page`
  71. """
  72. doclist = webnotes.model.doc.get('Page', self.name)
  73. doc = doclist[0]
  74. # load from module
  75. if doc.module:
  76. self.get_from_files(doc)
  77. # process
  78. self.process_content(doc)
  79. return doclist
  80. @webnotes.whitelist()
  81. def get(name):
  82. """
  83. Return the :term:`doclist` of the `Page` specified by `name`
  84. """
  85. return Page(name).load()
  86. @webnotes.whitelist(allow_guest=True)
  87. def getpage():
  88. """
  89. Load the page from `webnotes.form` and send it via `webnotes.response`
  90. """
  91. doclist = get(webnotes.form.getvalue('name'))
  92. # send
  93. webnotes.response['docs'] = doclist
  94. def get_page_path(page_name, module):
  95. """get path of the page html file"""
  96. import os
  97. import conf
  98. from webnotes.modules import scrub
  99. return os.path.join(conf.modules_path, 'erpnext', scrub(module), \
  100. 'page', scrub(page_name), scrub(page_name) + '.html')
  101. def get_page_html(page_name):
  102. """get html of page, called from webnotes.cms.index"""
  103. p = webnotes.conn.sql("""select module, content from tabPage where name=%s""", \
  104. page_name, as_dict=1)
  105. if not p:
  106. return ''
  107. else:
  108. import os
  109. p=p[0]
  110. path = get_page_path(page_name, p['module'])
  111. if os.path.exists(path):
  112. with open(path, 'r') as f:
  113. return f.read()
  114. else:
  115. return p['content']