blob: c7c1df210959d48b5775cab5433b12e2e03a1927 [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)"""
Rushabh Mehta40182ba2012-06-19 14:15:13 +053020
21 # TODO: Cache this in cache item
22
Rushabh Mehtabd7f7232012-06-15 14:00:06 +053023 import webnotes
24 return {
25 'top_bar_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
26 where parent='Website Settings' and parentfield='top_bar_items'
27 order by idx asc""", as_dict=1),
28
29 'footer_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
30 where parent='Website Settings' and parentfield='footer_items'
31 order by idx asc""", as_dict=1),
32
33 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html'),
34 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
35 }
36
Rushabh Mehta40182ba2012-06-19 14:15:13 +053037def get_web_page_name(page_name):
Rushabh Mehta96bf0b72012-06-15 17:29:03 +053038 """get page by shortname"""
39 import webnotes
40 return webnotes.conn.sql("""select name from `tabWeb Page` where page_name=%s""", page_name)[0][0]
41
Rushabh Mehtabd7f7232012-06-15 14:00:06 +053042def get_html():
43 import webnotes
44 from jinja2 import Environment, FileSystemLoader
45 from webnotes.model.doc import Document
46
47 jenv = Environment(loader = FileSystemLoader('../erpnext/website/templates'))
48
49 webnotes.form = cgi.FieldStorage(keep_blank_values=True)
50 for key in webnotes.form.keys():
51 webnotes.form_dict[key] = webnotes.form.getvalue(key)
52 webnotes.connect()
Rushabh Mehta40182ba2012-06-19 14:15:13 +053053
54 # Get web page
55 try:
56 if 'page' in webnotes.form_dict:
57 page_name = webnotes.form_dict['page']
58 if page_name.endswith('.html'):
59 page_name = page_name[:-5]
60
61 if page_name.startswith('blog'):
62 pass
63 # page_name =
64 else:
65 page_name = get_web_page_name(page_name)
66 else:
67 from webnotes.cms import get_home_page
68 page_name = get_home_page('Guest')
Rushabh Mehta96bf0b72012-06-15 17:29:03 +053069
Rushabh Mehta96bf0b72012-06-15 17:29:03 +053070 page = Document('Web Page', page_name)
71 page.fields.update(get_outer_env())
Rushabh Mehta40182ba2012-06-19 14:15:13 +053072 return jenv.get_template('page.html').render(page.fields) + \
73 ('\n<!-- page: %s -->' % page_name)
74
75 except Exception, e:
76 return jenv.get_template('404.html').render(get_outer_env()) + \
77 ('\n<!-- error: %s -->' % webnotes.getTraceback())
Rushabh Mehtabd7f7232012-06-15 14:00:06 +053078
79if __name__=="__main__":
80 print "Content-Type: text/html"
81 print
82 print get_html().encode('utf-8')