Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | # used by web.py |
Anand Doshi | bba4db1 | 2012-06-27 12:06:45 +0530 | [diff] [blame] | 18 | def load_from_web_cache(page_name, comments, template): #, script=None): |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 19 | """ |
| 20 | * search for page in cache |
| 21 | * if html exists, return |
| 22 | * if not, build html, store it in cache, return |
| 23 | """ |
| 24 | import webnotes |
| 25 | import conf |
| 26 | |
| 27 | if page_name == 'index': |
| 28 | page_name = get_index_page()[0] |
| 29 | |
| 30 | res = webnotes.conn.sql("""\ |
| 31 | select html, doc_type, doc_name from `tabWeb Cache` |
| 32 | where name = %s""", page_name) |
| 33 | |
| 34 | # if page doesn't exist, raise exception |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame^] | 35 | page_exception_list = ['404', 'index', 'blog', 'products', 'login-page'] |
| 36 | if not res and page_name not in page_exception_list: |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 37 | raise Exception, "Page %s not found" % page_name |
| 38 | |
| 39 | html, doc_type, doc_name = res and res[0] or (None, None, None) |
| 40 | auto_cache_clear = hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0 |
| 41 | if not html or auto_cache_clear: |
| 42 | comments += "\n\npage load status: fresh" |
| 43 | html = load_into_web_cache(page_name, template, doc_type, doc_name) |
| 44 | else: |
| 45 | comments += "\n\npage load status: cached" |
| 46 | |
| 47 | from webnotes.utils import cstr |
| 48 | html += """\n<!-- %s -->""" % cstr(comments) |
Anand Doshi | bba4db1 | 2012-06-27 12:06:45 +0530 | [diff] [blame] | 49 | |
| 50 | # show error in error console |
| 51 | # if script: html += """\n\n<script>\n%s\n</script>""" % cstr(script) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 52 | return html |
| 53 | |
| 54 | def load_into_web_cache(page_name, template, doc_type, doc_name): |
| 55 | """build html and store it in web cache""" |
| 56 | import webnotes |
| 57 | outer_env_dict = get_outer_env() |
| 58 | |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame^] | 59 | if page_name in ['404', 'blog', 'products', 'login-page']: |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 60 | args = outer_env_dict |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 61 | args.update({ |
| 62 | 'name': page_name, |
| 63 | }) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 64 | else: |
| 65 | if page_name == 'index': |
| 66 | page_name, doc_type, doc_name = get_index_page() |
| 67 | |
| 68 | from webnotes.model.code import get_obj |
| 69 | obj = get_obj(doc_type, doc_name) |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 70 | if hasattr(obj, 'get_html'): |
| 71 | obj.get_html() |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 72 | args = obj.doc.fields |
| 73 | args.update(outer_env_dict) |
| 74 | |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 75 | # decide template and update args |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 76 | if doc_type == 'Blog': |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 77 | template = 'blog/blog.html' |
| 78 | args.update({ 'insert_code': 1 }) |
| 79 | elif doc_type == 'Item': |
| 80 | template = 'product/product.html' |
| 81 | args.update({ 'insert_code': 1 }) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 82 | elif doc_type == 'Web Page': |
| 83 | template = 'web_page.html' |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame^] | 84 | else: |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 85 | args.update({ 'insert_code': 1 }) |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame^] | 86 | if page_name == 'blog': |
| 87 | template = 'blog/blog_list.html' |
| 88 | elif page_name == 'products': |
| 89 | template = 'product/product_list.html' |
| 90 | elif page_name == 'login-page': |
| 91 | template = 'login/login.html' |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 92 | |
| 93 | html = build_html(args, template) |
| 94 | |
| 95 | # save html in web cache |
| 96 | webnotes.conn.begin() |
| 97 | webnotes.conn.set_value('Web Cache', page_name, 'html', html) |
| 98 | webnotes.conn.commit() |
| 99 | |
| 100 | return html |
| 101 | |
| 102 | def build_html(args, template): |
| 103 | """build html using jinja2 templates""" |
| 104 | from jinja2 import Environment, FileSystemLoader |
| 105 | jenv = Environment(loader = FileSystemLoader('../erpnext/website/templates')) |
| 106 | html = jenv.get_template(template).render(args) |
| 107 | return html |
| 108 | |
| 109 | def get_outer_env(): |
| 110 | """env dict for outer template""" |
| 111 | import webnotes |
| 112 | return { |
| 113 | 'top_bar_items': webnotes.conn.sql("""select * from `tabTop Bar Item` |
| 114 | where parent='Website Settings' and parentfield='top_bar_items' |
| 115 | order by idx asc""", as_dict=1), |
| 116 | |
| 117 | 'footer_items': webnotes.conn.sql("""select * from `tabTop Bar Item` |
| 118 | where parent='Website Settings' and parentfield='footer_items' |
| 119 | order by idx asc""", as_dict=1), |
| 120 | |
| 121 | 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html'), |
| 122 | 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'), |
| 123 | } |
| 124 | |
| 125 | def get_index_page(): |
| 126 | import webnotes |
| 127 | doc_type = 'Web Page' |
| 128 | doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page') |
| 129 | page_name = webnotes.conn.get_value(doc_type, doc_name, 'page_name') |
| 130 | return page_name, doc_type, doc_name |
| 131 | |
| 132 | # cache management |
| 133 | def clear_web_cache(doc_type, doc_name, page_name): |
| 134 | """ |
| 135 | * check if a record corresponding to (type, name) exists |
| 136 | * if exists, just clear html column |
| 137 | * if does not exist, create a record for (type, name) |
| 138 | * if a record like (some other type, name) exists, raise exception that the page name is not unique |
| 139 | """ |
| 140 | import webnotes |
| 141 | res = webnotes.conn.get_value('Web Cache', page_name, 'doc_type') |
| 142 | if not res: |
| 143 | import webnotes.model.doc |
| 144 | d = webnotes.model.doc.Document('Web Cache') |
| 145 | d.name = page_name |
| 146 | d.doc_type = doc_type |
| 147 | d.doc_name = doc_name |
| 148 | d.html = None |
| 149 | d.save() |
| 150 | elif res == doc_type: |
| 151 | webnotes.conn.set_value('Web Cache', page_name, 'html', None) |
| 152 | else: |
| 153 | webnotes.msgprint("""Page with name "%s" already exists as a %s. |
| 154 | Please save it with another name.""" % (page_name, res), raise_exception=1) |
| 155 | |
| 156 | def clear_all_web_cache(): |
| 157 | import webnotes |
| 158 | webnotes.conn.sql("update `tabWeb Cache` set html = NULL") |
| 159 | |
| 160 | def delete_web_cache(page_name): |
| 161 | """ |
| 162 | delete entry of page_name from Web Cache |
| 163 | used when: |
| 164 | * web page is deleted |
| 165 | * blog is un-published |
| 166 | """ |
| 167 | import webnotes |
| 168 | webnotes.conn.sql("""\ |
| 169 | delete from `tabWeb Cache` |
| 170 | where name=%s""", page_name) |
| 171 | |
| 172 | def build_web_cache(): |
| 173 | """build web cache so that pages can load faster""" |
| 174 | pass |