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 | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame^] | 18 | def load_from_web_cache(page_name, comments, template): |
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 | |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 50 | return html |
| 51 | |
| 52 | def load_into_web_cache(page_name, template, doc_type, doc_name): |
| 53 | """build html and store it in web cache""" |
| 54 | import webnotes |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 55 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame^] | 56 | args = prepare_args(page_name, doc_type, doc_name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 57 | |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 58 | # decide template and update args |
Anand Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 59 | if doc_type == 'Web Page': |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 60 | template = 'web_page.html' |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 61 | else: |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 62 | args.update({ 'insert_code': 1 }) |
Anand Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 63 | if doc_type == 'Blog': |
| 64 | template = 'blog/blog.html' |
| 65 | elif doc_type == 'Item': |
| 66 | template = 'product/product.html' |
| 67 | elif page_name == 'blog': |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 68 | template = 'blog/blog_list.html' |
| 69 | elif page_name == 'products': |
| 70 | template = 'product/product_list.html' |
| 71 | elif page_name == 'login-page': |
| 72 | template = 'login/login.html' |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 73 | |
| 74 | html = build_html(args, template) |
| 75 | |
| 76 | # save html in web cache |
| 77 | webnotes.conn.begin() |
| 78 | webnotes.conn.set_value('Web Cache', page_name, 'html', html) |
| 79 | webnotes.conn.commit() |
| 80 | |
| 81 | return html |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame^] | 82 | |
| 83 | def prepare_args(page_name, doc_type, doc_name, with_outer_env=1): |
| 84 | if page_name in ['404', 'blog', 'products', 'login-page']: |
| 85 | args = { |
| 86 | 'name': page_name, |
| 87 | } |
| 88 | else: |
| 89 | if page_name == 'index': |
| 90 | page_name, doc_type, doc_name = get_index_page() |
| 91 | |
| 92 | from webnotes.model.code import get_obj |
| 93 | obj = get_obj(doc_type, doc_name) |
| 94 | if hasattr(obj, 'prepare_template_args'): |
| 95 | obj.prepare_template_args() |
| 96 | args = obj.doc.fields |
| 97 | |
| 98 | outer_env_dict = with_outer_env and get_outer_env() or {} |
| 99 | args.update(outer_env_dict) |
| 100 | |
| 101 | return args |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 102 | |
| 103 | def build_html(args, template): |
| 104 | """build html using jinja2 templates""" |
| 105 | from jinja2 import Environment, FileSystemLoader |
| 106 | jenv = Environment(loader = FileSystemLoader('../erpnext/website/templates')) |
| 107 | html = jenv.get_template(template).render(args) |
| 108 | return html |
| 109 | |
| 110 | def get_outer_env(): |
| 111 | """env dict for outer template""" |
| 112 | import webnotes |
| 113 | return { |
| 114 | 'top_bar_items': webnotes.conn.sql("""select * from `tabTop Bar Item` |
| 115 | where parent='Website Settings' and parentfield='top_bar_items' |
| 116 | order by idx asc""", as_dict=1), |
| 117 | |
| 118 | 'footer_items': webnotes.conn.sql("""select * from `tabTop Bar Item` |
| 119 | where parent='Website Settings' and parentfield='footer_items' |
| 120 | order by idx asc""", as_dict=1), |
| 121 | |
| 122 | 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html'), |
| 123 | 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'), |
Anand Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 124 | 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon') |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | def get_index_page(): |
| 128 | import webnotes |
| 129 | doc_type = 'Web Page' |
| 130 | doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page') |
| 131 | page_name = webnotes.conn.get_value(doc_type, doc_name, 'page_name') |
| 132 | return page_name, doc_type, doc_name |
| 133 | |
| 134 | # cache management |
| 135 | def clear_web_cache(doc_type, doc_name, page_name): |
| 136 | """ |
| 137 | * check if a record corresponding to (type, name) exists |
| 138 | * if exists, just clear html column |
| 139 | * if does not exist, create a record for (type, name) |
| 140 | * if a record like (some other type, name) exists, raise exception that the page name is not unique |
| 141 | """ |
| 142 | import webnotes |
| 143 | res = webnotes.conn.get_value('Web Cache', page_name, 'doc_type') |
| 144 | if not res: |
| 145 | import webnotes.model.doc |
| 146 | d = webnotes.model.doc.Document('Web Cache') |
| 147 | d.name = page_name |
| 148 | d.doc_type = doc_type |
| 149 | d.doc_name = doc_name |
| 150 | d.html = None |
| 151 | d.save() |
| 152 | elif res == doc_type: |
| 153 | webnotes.conn.set_value('Web Cache', page_name, 'html', None) |
| 154 | else: |
| 155 | webnotes.msgprint("""Page with name "%s" already exists as a %s. |
| 156 | Please save it with another name.""" % (page_name, res), raise_exception=1) |
| 157 | |
| 158 | def clear_all_web_cache(): |
| 159 | import webnotes |
| 160 | webnotes.conn.sql("update `tabWeb Cache` set html = NULL") |
| 161 | |
| 162 | def delete_web_cache(page_name): |
| 163 | """ |
| 164 | delete entry of page_name from Web Cache |
| 165 | used when: |
| 166 | * web page is deleted |
| 167 | * blog is un-published |
| 168 | """ |
| 169 | import webnotes |
| 170 | webnotes.conn.sql("""\ |
| 171 | delete from `tabWeb Cache` |
| 172 | where name=%s""", page_name) |
| 173 | |
| 174 | def build_web_cache(): |
| 175 | """build web cache so that pages can load faster""" |
| 176 | pass |