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.
 
 
 
 
 
 

120 line
2.9 KiB

  1. import webnotes
  2. import webnotes.model.doc
  3. import webnotes.model.code
  4. conn = webnotes.conn
  5. class Page:
  6. """
  7. A page class helps in loading a Page in the system. On loading
  8. * Page will import Client Script from other pages where specified by `$import(page_name)`
  9. * Execute dynamic HTML if the `content` starts with `#python`
  10. """
  11. def __init__(self, name):
  12. self.name = name
  13. def get_from_files(self, doc, module):
  14. """
  15. Loads page info from files in module
  16. """
  17. # load js
  18. doc.fields['__script'] = module.get_doc_file('page',doc.name,'.js').read() or doc.script
  19. doc.script = None
  20. # load css
  21. css = module.get_doc_file('page',doc.name,'.css').read()
  22. if css: doc.style = css
  23. # html
  24. doc.content = module.get_doc_file('page',doc.name,'.html').read() or doc.content
  25. def get_template(self, template):
  26. """
  27. Returns the page template content
  28. """
  29. ret = '%(content)s'
  30. # load code from template
  31. if template:
  32. from webnotes.modules import Module
  33. ret = Module(webnotes.conn.get_value('Page Template', template, 'module'))\
  34. .get_doc_file('Page Template', template, '.html').read()
  35. if not ret:
  36. ret = webnotes.conn.get_value('Page Template', template, 'template')
  37. return ret
  38. def process_content(self, doc):
  39. """
  40. Put in template and generate dynamic if starts with #!python
  41. """
  42. template = self.get_template(doc.template)
  43. content = ''
  44. # eval content
  45. if doc.content and doc.content.startswith('#!python'):
  46. from webnotes.model.code import execute
  47. content = template % {'content': execute(doc.content).get('content')}
  48. else:
  49. content = template % {'content': doc.content or ''}
  50. doc.__content = content
  51. def load(self):
  52. """
  53. Returns :term:`doclist` of the `Page`
  54. """
  55. from webnotes.modules import Module
  56. doclist = webnotes.model.doc.get('Page', self.name)
  57. doc = doclist[0]
  58. # load from module
  59. if doc.module:
  60. module = Module(doc.module)
  61. self.get_from_files(doc, module)
  62. # process
  63. self.process_content(doc)
  64. # add stylesheet
  65. if doc.stylesheet:
  66. doclist += self.load_stylesheet(doc.stylesheet)
  67. return doclist
  68. def load_stylesheet(self, stylesheet):
  69. import webnotes
  70. # load stylesheet
  71. loaded = eval(webnotes.form_dict.get('stylesheets') or '[]')
  72. if not stylesheet in loaded:
  73. import webnotes.model.doc
  74. from webnotes.modules import Module
  75. # doclist
  76. sslist = webnotes.model.doc.get('Stylesheet', stylesheet)
  77. # stylesheet from file
  78. css = Module(sslist[0].module).get_doc_file('Stylesheet', stylesheet, '.css').read()
  79. if css: sslist[0].stylesheet = css
  80. return sslist
  81. else:
  82. return []
  83. def get(name):
  84. """
  85. Return the :term:`doclist` of the `Page` specified by `name`
  86. """
  87. return Page(name).load()
  88. def getpage():
  89. """
  90. Load the page from `webnotes.form` and send it via `webnotes.response`
  91. """
  92. doclist = get(webnotes.form.getvalue('name'))
  93. # send
  94. webnotes.response['docs'] = doclist