blob: 8909bf2f820ed53abd8520faff4f462d97e36850 [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
18def get_outer_env():
19 """env for outer (cache this)"""
20 import webnotes
21 return {
22 'top_bar_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
23 where parent='Website Settings' and parentfield='top_bar_items'
24 order by idx asc""", as_dict=1),
25
26 'footer_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
27 where parent='Website Settings' and parentfield='footer_items'
28 order by idx asc""", as_dict=1),
29
30 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html'),
31 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
32 }
33
Rushabh Mehta96bf0b72012-06-15 17:29:03 +053034def get_page_by_short_name(page_name):
35 """get page by shortname"""
36 import webnotes
37 return webnotes.conn.sql("""select name from `tabWeb Page` where page_name=%s""", page_name)[0][0]
38
Rushabh Mehtabd7f7232012-06-15 14:00:06 +053039def get_html():
40 import webnotes
41 from jinja2 import Environment, FileSystemLoader
42 from webnotes.model.doc import Document
43
44 jenv = Environment(loader = FileSystemLoader('../erpnext/website/templates'))
45
46 webnotes.form = cgi.FieldStorage(keep_blank_values=True)
47 for key in webnotes.form.keys():
48 webnotes.form_dict[key] = webnotes.form.getvalue(key)
49 webnotes.connect()
50
51 if 'page' in webnotes.form_dict:
Rushabh Mehta96bf0b72012-06-15 17:29:03 +053052 page_name = webnotes.form_dict['page']
53 if page_name.endswith('.html'):
54 page_name = page_name[:-5]
55
56 #try:
57 page_name = get_page_by_short_name(page_name)
58 page = Document('Web Page', page_name)
59 page.fields.update(get_outer_env())
60 return jenv.get_template('page.html').render(page.fields)
61 #except Exception, e:
62 # return jenv.get_template('404.html').render(get_outer_env())
Rushabh Mehtabd7f7232012-06-15 14:00:06 +053063 else:
64 return jenv.get_template('404.html').render(get_outer_env())
65
66if __name__=="__main__":
67 print "Content-Type: text/html"
68 print
69 print get_html().encode('utf-8')