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 | |
Anand Doshi | 5d9fc72 | 2012-06-21 11:48:51 +0530 | [diff] [blame^] | 18 | def 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 | |
| 25 | def respond(): |
| 26 | html = get_html() |
| 27 | print "Content-Type: text/html" |
| 28 | print |
| 29 | print html.encode('utf-8') |
| 30 | |
| 31 | def 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 Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 59 | def get_outer_env(): |
Anand Doshi | 5d9fc72 | 2012-06-21 11:48:51 +0530 | [diff] [blame^] | 60 | """env dict for outer template""" |
Rushabh Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 61 | 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 Doshi | 5d9fc72 | 2012-06-21 11:48:51 +0530 | [diff] [blame^] | 74 | |
| 75 | def 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 Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 80 | |
Anand Doshi | 5d9fc72 | 2012-06-21 11:48:51 +0530 | [diff] [blame^] | 81 | html = jenv.get_template(template).render(args) |
| 82 | html += "\n<!-- %s -->" % cstr(comments) |
| 83 | return html |
| 84 | |
Rushabh Mehta | 40182ba | 2012-06-19 14:15:13 +0530 | [diff] [blame] | 85 | def get_web_page_name(page_name): |
Rushabh Mehta | 96bf0b7 | 2012-06-15 17:29:03 +0530 | [diff] [blame] | 86 | """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 Doshi | 5d9fc72 | 2012-06-21 11:48:51 +0530 | [diff] [blame^] | 89 | |
Rushabh Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 90 | |
| 91 | if __name__=="__main__": |
Anand Doshi | 5d9fc72 | 2012-06-21 11:48:51 +0530 | [diff] [blame^] | 92 | init() |
| 93 | respond() |