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)""" |
Rushabh Mehta | 40182ba | 2012-06-19 14:15:13 +0530 | [diff] [blame^] | 20 | |
| 21 | # TODO: Cache this in cache item |
| 22 | |
Rushabh Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 23 | 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 Mehta | 40182ba | 2012-06-19 14:15:13 +0530 | [diff] [blame^] | 37 | def get_web_page_name(page_name): |
Rushabh Mehta | 96bf0b7 | 2012-06-15 17:29:03 +0530 | [diff] [blame] | 38 | """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 Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 42 | def 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 Mehta | 40182ba | 2012-06-19 14:15:13 +0530 | [diff] [blame^] | 53 | |
| 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 Mehta | 96bf0b7 | 2012-06-15 17:29:03 +0530 | [diff] [blame] | 69 | |
Rushabh Mehta | 96bf0b7 | 2012-06-15 17:29:03 +0530 | [diff] [blame] | 70 | page = Document('Web Page', page_name) |
| 71 | page.fields.update(get_outer_env()) |
Rushabh Mehta | 40182ba | 2012-06-19 14:15:13 +0530 | [diff] [blame^] | 72 | 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 Mehta | bd7f723 | 2012-06-15 14:00:06 +0530 | [diff] [blame] | 78 | |
| 79 | if __name__=="__main__": |
| 80 | print "Content-Type: text/html" |
| 81 | print |
| 82 | print get_html().encode('utf-8') |