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 |
Rushabh Mehta | 96bf0b7 | 2012-06-15 17:29:03 +0530 | [diff] [blame^] | 5 | |
| 6 | all html pages except login-page.html get generated here |
Rushabh Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 7 | """ |
| 8 | |
| 9 | import cgi, cgitb, os, sys |
| 10 | cgitb.enable() |
| 11 | |
| 12 | # import libs |
| 13 | sys.path.append('..') |
| 14 | import conf |
| 15 | sys.path.append('../lib/py') |
| 16 | sys.path.append(conf.modules_path) |
| 17 | |
| 18 | def 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 Mehta | 96bf0b7 | 2012-06-15 17:29:03 +0530 | [diff] [blame^] | 34 | def 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 Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 39 | def 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 Mehta | 96bf0b7 | 2012-06-15 17:29:03 +0530 | [diff] [blame^] | 52 | 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 Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 63 | else: |
| 64 | return jenv.get_template('404.html').render(get_outer_env()) |
| 65 | |
| 66 | if __name__=="__main__": |
| 67 | print "Content-Type: text/html" |
| 68 | print |
| 69 | print get_html().encode('utf-8') |