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 | |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame^] | 17 | page_exception_list = ['404', 'blog', 'products', 'login-page'] |
| 18 | |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 19 | # used by web.py |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 20 | def load_from_web_cache(page_name, comments, template): |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 21 | """ |
| 22 | * search for page in cache |
| 23 | * if html exists, return |
| 24 | * if not, build html, store it in cache, return |
| 25 | """ |
| 26 | import webnotes |
| 27 | import conf |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 28 | |
| 29 | res = webnotes.conn.sql("""\ |
| 30 | select html, doc_type, doc_name from `tabWeb Cache` |
| 31 | where name = %s""", page_name) |
| 32 | |
| 33 | # if page doesn't exist, raise exception |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame^] | 34 | if not res and page_name not in page_exception_list + ['index']: |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 35 | raise Exception, "Page %s not found" % page_name |
| 36 | |
| 37 | html, doc_type, doc_name = res and res[0] or (None, None, None) |
| 38 | auto_cache_clear = hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0 |
| 39 | if not html or auto_cache_clear: |
| 40 | comments += "\n\npage load status: fresh" |
| 41 | html = load_into_web_cache(page_name, template, doc_type, doc_name) |
| 42 | else: |
| 43 | comments += "\n\npage load status: cached" |
| 44 | |
| 45 | from webnotes.utils import cstr |
| 46 | html += """\n<!-- %s -->""" % cstr(comments) |
Anand Doshi | bba4db1 | 2012-06-27 12:06:45 +0530 | [diff] [blame] | 47 | |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 48 | return html |
| 49 | |
| 50 | def load_into_web_cache(page_name, template, doc_type, doc_name): |
| 51 | """build html and store it in web cache""" |
| 52 | import webnotes |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 53 | |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 54 | args = prepare_args(page_name, doc_type, doc_name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 55 | |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 56 | # decide template and update args |
Anand Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 57 | if doc_type == 'Web Page': |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 58 | template = 'web_page.html' |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 59 | else: |
Anand Doshi | 10bcf5e | 2012-06-26 18:54:10 +0530 | [diff] [blame] | 60 | args.update({ 'insert_code': 1 }) |
Anand Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 61 | if doc_type == 'Blog': |
| 62 | template = 'blog/blog.html' |
| 63 | elif doc_type == 'Item': |
| 64 | template = 'product/product.html' |
| 65 | elif page_name == 'blog': |
Anand Doshi | e47def8 | 2012-07-09 15:56:12 +0530 | [diff] [blame] | 66 | template = 'blog/blog_list.html' |
| 67 | elif page_name == 'products': |
| 68 | template = 'product/product_list.html' |
| 69 | elif page_name == 'login-page': |
| 70 | template = 'login/login.html' |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 71 | |
| 72 | html = build_html(args, template) |
| 73 | |
| 74 | # save html in web cache |
| 75 | webnotes.conn.begin() |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame^] | 76 | if page_name in page_exception_list + ['index']: |
| 77 | clear_web_cache(doc_type, doc_name, page_name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 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): |
Anand Doshi | 51fc20b | 2012-07-11 18:56:13 +0530 | [diff] [blame] | 84 | if page_name == 'index': |
| 85 | page_name, doc_type, doc_name = get_index_page() |
| 86 | |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame^] | 87 | if page_name in page_exception_list: |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 88 | args = { |
| 89 | 'name': page_name, |
| 90 | } |
| 91 | else: |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 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 | |
Anand Doshi | f7765a8 | 2012-07-11 19:05:27 +0530 | [diff] [blame] | 122 | 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext', |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 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') |
Anand Doshi | 51fc20b | 2012-07-11 18:56:13 +0530 | [diff] [blame] | 132 | if not page_name: |
| 133 | page_name = 'login-page' |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 134 | return page_name, doc_type, doc_name |
| 135 | |
| 136 | # cache management |
| 137 | def clear_web_cache(doc_type, doc_name, page_name): |
| 138 | """ |
| 139 | * check if a record corresponding to (type, name) exists |
| 140 | * if exists, just clear html column |
| 141 | * if does not exist, create a record for (type, name) |
| 142 | * if a record like (some other type, name) exists, raise exception that the page name is not unique |
| 143 | """ |
| 144 | import webnotes |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame^] | 145 | res = webnotes.conn.get_value('Web Cache', page_name, ['doc_type', 'name']) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 146 | if not res: |
| 147 | import webnotes.model.doc |
| 148 | d = webnotes.model.doc.Document('Web Cache') |
| 149 | d.name = page_name |
| 150 | d.doc_type = doc_type |
| 151 | d.doc_name = doc_name |
| 152 | d.html = None |
| 153 | d.save() |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame^] | 154 | elif not doc_type or res[0] == doc_type: |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 155 | webnotes.conn.set_value('Web Cache', page_name, 'html', None) |
| 156 | else: |
| 157 | webnotes.msgprint("""Page with name "%s" already exists as a %s. |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame^] | 158 | Please save it with another name.""" % (page_name, res[0]), raise_exception=1) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 159 | |
| 160 | def clear_all_web_cache(): |
| 161 | import webnotes |
| 162 | webnotes.conn.sql("update `tabWeb Cache` set html = NULL") |
| 163 | |
| 164 | def delete_web_cache(page_name): |
| 165 | """ |
| 166 | delete entry of page_name from Web Cache |
| 167 | used when: |
| 168 | * web page is deleted |
| 169 | * blog is un-published |
| 170 | """ |
| 171 | import webnotes |
| 172 | webnotes.conn.sql("""\ |
| 173 | delete from `tabWeb Cache` |
| 174 | where name=%s""", page_name) |
| 175 | |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame^] | 176 | def rebuild_web_cache(): |
| 177 | """build web cache entries""" |
| 178 | import webnotes |
| 179 | from webnotes.model.doclist import DocList |
| 180 | save_list = [ |
| 181 | { |
| 182 | 'doctype': 'Web Page', |
| 183 | 'query': """select name from `tabWeb Page` where docstatus=0""" |
| 184 | }, |
| 185 | { |
| 186 | 'doctype': 'Blog', |
| 187 | 'query': """\ |
| 188 | select name from `tabBlog` |
| 189 | where docstatus = 0 and ifnull(published, 0) = 1""" |
| 190 | }, |
| 191 | { |
| 192 | 'doctype': 'Item', |
| 193 | 'query': """\ |
| 194 | select name from `tabItem` |
| 195 | where docstatus = 0 and ifnull(show_in_website, 0) = 1""" |
| 196 | } |
| 197 | ] |
| 198 | |
| 199 | for s in save_list: |
| 200 | for p in webnotes.conn.sql(s['query'], as_dict=1): |
| 201 | DocList(s['doctype'], p['name']).save() |