blob: 751d76206a8dc3a1e14e06b30d4c08c070aefac8 [file] [log] [blame]
Rushabh Mehtabd7f7232012-06-15 14:00:06 +05301#!/usr/bin/env python
2
3"""
4return a dynamic page from website templates
Rushabh Mehta96bf0b72012-06-15 17:29:03 +05305
6all html pages except login-page.html get generated here
Rushabh Mehtabd7f7232012-06-15 14:00:06 +05307"""
8
9import cgi, cgitb, os, sys
10cgitb.enable()
11
12# import libs
13sys.path.append('..')
14import conf
15sys.path.append('../lib/py')
16sys.path.append(conf.modules_path)
17
Anand Doshi5d9fc722012-06-21 11:48:51 +053018def init():
19 import webnotes
20 webnotes.form = cgi.FieldStorage(keep_blank_values=True)
21 for key in webnotes.form.keys():
22 webnotes.form_dict[key] = webnotes.form.getvalue(key)
23 webnotes.connect()
24
25def respond():
26 html = get_html()
27 print "Content-Type: text/html"
28 print
29 print html.encode('utf-8')
30
31def get_html():
32 import webnotes
33 from webnotes.model.doc import Document
34 # Get web page
35 outer_env_dict = get_outer_env()
36 try:
37 if 'page' in webnotes.form_dict:
38 page_name = webnotes.form_dict['page']
39 if page_name.endswith('.html'):
40 page_name = page_name[:-5]
41
42 if page_name.startswith('blog'):
43 raise Exception
44 #page_name =
45 else:
46 page_name = get_web_page_name(page_name)
47 else:
48 from webnotes.cms import get_home_page
49 page_name = get_home_page('Guest')
50
51 page = Document('Web Page', page_name)
52 page.fields.update(outer_env_dict)
53
54 return build_html(page.fields, "page: %s" % page_name)
55
56 except Exception, e:
57 return build_html(outer_env_dict, "error: %s" % webnotes.getTraceback(), '404.html')
58
Rushabh Mehtabd7f7232012-06-15 14:00:06 +053059def get_outer_env():
Anand Doshi5d9fc722012-06-21 11:48:51 +053060 """env dict for outer template"""
Rushabh Mehtabd7f7232012-06-15 14:00:06 +053061 import webnotes
62 return {
63 'top_bar_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
64 where parent='Website Settings' and parentfield='top_bar_items'
65 order by idx asc""", as_dict=1),
66
67 'footer_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
68 where parent='Website Settings' and parentfield='footer_items'
69 order by idx asc""", as_dict=1),
70
71 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html'),
72 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
73 }
Anand Doshi5d9fc722012-06-21 11:48:51 +053074
75def build_html(args, comments, template='page.html'):
76 """build html using jinja2 templates"""
77 from webnotes.utils import cstr
78 from jinja2 import Environment, FileSystemLoader
79 jenv = Environment(loader = FileSystemLoader('../erpnext/website/templates'))
Rushabh Mehtabd7f7232012-06-15 14:00:06 +053080
Anand Doshi5d9fc722012-06-21 11:48:51 +053081 html = jenv.get_template(template).render(args)
82 html += "\n<!-- %s -->" % cstr(comments)
83 return html
84
Rushabh Mehta40182ba2012-06-19 14:15:13 +053085def get_web_page_name(page_name):
Rushabh Mehta96bf0b72012-06-15 17:29:03 +053086 """get page by shortname"""
87 import webnotes
88 return webnotes.conn.sql("""select name from `tabWeb Page` where page_name=%s""", page_name)[0][0]
Anand Doshi5d9fc722012-06-21 11:48:51 +053089
Rushabh Mehtabd7f7232012-06-15 14:00:06 +053090
91if __name__=="__main__":
Anand Doshi5d9fc722012-06-21 11:48:51 +053092 init()
93 respond()