Rushabh Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | return a dynamic page from website templates |
| 5 | """ |
| 6 | |
| 7 | import cgi, cgitb, os, sys |
| 8 | cgitb.enable() |
| 9 | |
| 10 | # import libs |
| 11 | sys.path.append('..') |
| 12 | import conf |
| 13 | sys.path.append('../lib/py') |
| 14 | sys.path.append(conf.modules_path) |
| 15 | |
| 16 | def get_outer_env(): |
| 17 | """env for outer (cache this)""" |
| 18 | import webnotes |
| 19 | return { |
| 20 | 'top_bar_items': webnotes.conn.sql("""select * from `tabTop Bar Item` |
| 21 | where parent='Website Settings' and parentfield='top_bar_items' |
| 22 | order by idx asc""", as_dict=1), |
| 23 | |
| 24 | 'footer_items': webnotes.conn.sql("""select * from `tabTop Bar Item` |
| 25 | where parent='Website Settings' and parentfield='footer_items' |
| 26 | order by idx asc""", as_dict=1), |
| 27 | |
| 28 | 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html'), |
| 29 | 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'), |
| 30 | } |
| 31 | |
| 32 | def get_html(): |
| 33 | import webnotes |
| 34 | from jinja2 import Environment, FileSystemLoader |
| 35 | from webnotes.model.doc import Document |
| 36 | |
| 37 | jenv = Environment(loader = FileSystemLoader('../erpnext/website/templates')) |
| 38 | |
| 39 | webnotes.form = cgi.FieldStorage(keep_blank_values=True) |
| 40 | for key in webnotes.form.keys(): |
| 41 | webnotes.form_dict[key] = webnotes.form.getvalue(key) |
| 42 | webnotes.connect() |
| 43 | |
| 44 | if 'page' in webnotes.form_dict: |
| 45 | try: |
| 46 | page = Document('Page', webnotes.form_dict['page']) |
| 47 | page.fields.update(get_outer_env()) |
| 48 | return jenv.get_template('page.html').render(page.fields) |
| 49 | except Exception, e: |
| 50 | return jenv.get_template('404.html').render(get_outer_env()) |
| 51 | else: |
| 52 | return jenv.get_template('404.html').render(get_outer_env()) |
| 53 | |
| 54 | if __name__=="__main__": |
| 55 | print "Content-Type: text/html" |
| 56 | print |
| 57 | print get_html().encode('utf-8') |