web pages don't render into Page and Page does not get written as HTML. Welcome Jinja2 Templates

This commit is contained in:
Rushabh Mehta 2012-06-15 17:29:03 +05:30
parent cc53afcef3
commit 96bf0b7f53
5 changed files with 55 additions and 70 deletions

View File

@ -8,21 +8,11 @@
<div class="layout-main">
{% endif %}
{{ doc.main_section_html }}
{% if doc.next_page_html %}
{{ doc.next_page_html }}
{% endif %}
</div>
{% if doc.layout and doc.layout.startswith('Two column') %}
<div class="layout-side-section">
{{ doc.side_section_html }}
{% if doc.see_also %}
<div class="info-box">
<h4>See Also</h4>
{{ doc.see_also }}
</div>
{% endif %}
</div>
{% endif %}
<div style="clear: both"></div>

View File

@ -28,60 +28,29 @@ class DocType:
def validate(self):
"""make page for this product"""
from jinja2 import Template
from webnotes.utils import global_date_format
from webnotes.model.code import get_obj
import os
# we need the name for the templates
if self.doc.name.startswith('New Web Page'):
self.autoname()
if self.doc.page_name:
webnotes.conn.sql("""delete from tabPage where name=%s""", self.doc.page_name)
p = website.utils.add_page(self.doc.name)
self.doc.page_name = p.name
# page name updates with the title
self.doc.page_name = website.utils.page_name(self.doc.title)
self.doc.updated = global_date_format(self.doc.modified)
# markdown
website.utils.markdown(self.doc, ['head_section','main_section', 'side_section'])
# make page layout
with open(os.path.join(os.path.dirname(__file__), 'template.html'), 'r') as f:
p.content = Template(f.read()).render(doc=self.doc)
p.title = self.doc.title
p.web_page = 'Yes'
self.doc.content = Template(f.read()).render(doc=self.doc)
if self.doc.insert_code:
p.script = self.doc.javascript
if self.doc.insert_style:
p.style = self.doc.css
p.save()
get_obj(doc=p).write_cms_page()
website.utils.add_guest_access_to_page(p.name)
self.cleanup_temp()
self.if_home_clear_cache()
def add_page_links(self):
"""add links for next_page and see_also"""
if self.doc.next_page:
self.doc.next_page_html = """<div class="info-box round" style="text-align: right">
<b>Next:</b>
<a href="#!%(name)s">%(title)s</a></div>""" % {"name":self.doc.next_page, \
"title": webnotes.conn.get_value("Page", self.doc.next_page, "title")}
self.doc.see_also = ''
for d in self.doclist:
if d.doctype=='Related Page':
tmp = {"page":d.page, "title":webnotes.conn.get_value('Page', d.page, 'title')}
self.doc.see_also += """<div><a href="#!%(page)s">%(title)s</a></div>""" % tmp
def cleanup_temp(self):
"""cleanup temp fields"""
fl = ['main_section_html', 'side_section_html', 'see_also', \
'next_page_html', 'head_section_html', 'updated']
fl = ['main_section_html', 'side_section_html', \
'head_section_html']
for f in fl:
if f in self.doc.fields:
del self.doc.fields[f]

View File

@ -5,7 +5,7 @@
{
'creation': '2012-04-02 16:02:43',
'docstatus': 0,
'modified': '2012-05-02 15:24:31',
'modified': '2012-06-15 17:18:59',
'modified_by': u'Administrator',
'owner': u'Administrator'
},
@ -86,6 +86,17 @@
'reqd': 1
},
# DocField
{
'colour': u'White:FFF',
'description': u'Page url name (auto-generated) ',
'doctype': u'DocField',
'fieldname': u'page_name',
'fieldtype': u'Data',
'label': u'Page Name',
'permlevel': 1
},
# DocField
{
'doctype': u'DocField',
@ -101,7 +112,7 @@
'fieldname': u'layout',
'fieldtype': u'Select',
'label': u'Layout',
'options': u'Two column with header\nTwo column\nSingle column',
'options': u'Single column\nTwo column\nTwo column with header',
'permlevel': 0
},
@ -152,6 +163,17 @@
'permlevel': 0
},
# DocField
{
'colour': u'White:FFF',
'doctype': u'DocField',
'fieldname': u'content',
'fieldtype': u'Code',
'hidden': 1,
'label': u'Content',
'permlevel': 0
},
# DocField
{
'colour': u'White:FFF',
@ -176,6 +198,7 @@
# DocField
{
'colour': u'White:FFF',
'description': u'Add code as &lt;script&gt;',
'doctype': u'DocField',
'fieldname': u'insert_code',
'fieldtype': u'Check',
@ -212,15 +235,6 @@
'permlevel': 0
},
# DocField
{
'doctype': u'DocField',
'fieldname': u'page_name',
'fieldtype': u'Data',
'label': u'Page Name',
'permlevel': 1
},
# DocField
{
'doctype': u'DocField',

View File

@ -4,7 +4,7 @@
{% block header %}
{{ super() }}
{% if script %}
{% if insert_code %}
<script>
window.page_name = "{{ name }}";
@ -12,7 +12,7 @@
var _page = new wn.views.Page(window.page_name);
// page script
{{ script }}
{{ insert_code }}
// trigger onload
_page.trigger('onload');
@ -22,8 +22,8 @@
});
</script>
{% endif %}
{% if style %}
<style>{{ style }}</style>
{% if insert_style %}
<style>{{ insert_style }}</style>
{% endif %}
{% endblock %}

View File

@ -2,6 +2,8 @@
"""
return a dynamic page from website templates
all html pages except login-page.html get generated here
"""
import cgi, cgitb, os, sys
@ -29,6 +31,11 @@ def get_outer_env():
'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
}
def get_page_by_short_name(page_name):
"""get page by shortname"""
import webnotes
return webnotes.conn.sql("""select name from `tabWeb Page` where page_name=%s""", page_name)[0][0]
def get_html():
import webnotes
from jinja2 import Environment, FileSystemLoader
@ -42,12 +49,17 @@ def get_html():
webnotes.connect()
if 'page' in webnotes.form_dict:
try:
page = Document('Page', webnotes.form_dict['page'])
page.fields.update(get_outer_env())
return jenv.get_template('page.html').render(page.fields)
except Exception, e:
return jenv.get_template('404.html').render(get_outer_env())
page_name = webnotes.form_dict['page']
if page_name.endswith('.html'):
page_name = page_name[:-5]
#try:
page_name = get_page_by_short_name(page_name)
page = Document('Web Page', page_name)
page.fields.update(get_outer_env())
return jenv.get_template('page.html').render(page.fields)
#except Exception, e:
# return jenv.get_template('404.html').render(get_outer_env())
else:
return jenv.get_template('404.html').render(get_outer_env())