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 | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 17 | # html generation functions |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame] | 18 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 19 | template_map = { |
| 20 | 'Web Page': 'html/web_page.html', |
| 21 | 'Blog': 'html/blog_page.html', |
| 22 | 'Item': 'html/product_page.html', |
| 23 | } |
| 24 | |
| 25 | def get_html(page_name, comments=''): |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 26 | import conf |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 27 | |
| 28 | html = '' |
| 29 | |
| 30 | # load from cache, if auto cache clear is falsy |
| 31 | if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0): |
| 32 | html = load_from_cache(page_name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 33 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 34 | if not html: |
| 35 | html = load_into_cache(page_name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 36 | comments += "\n\npage load status: fresh" |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 37 | |
| 38 | # insert comments |
| 39 | import webnotes.utils |
| 40 | html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) |
Anand Doshi | bba4db1 | 2012-06-27 12:06:45 +0530 | [diff] [blame] | 41 | |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 42 | return html |
| 43 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 44 | def load_from_cache(page_name): |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 45 | import webnotes |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 46 | |
| 47 | result = search_cache(page_name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 48 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 49 | if not result: |
| 50 | if page_name in get_predefined_pages(): |
| 51 | # if a predefined page doesn't exist, load it into cache |
| 52 | return None |
| 53 | else: |
| 54 | # if page doesn't exist, raise exception |
| 55 | raise Exception, "Page %s not found" % page_name |
| 56 | |
| 57 | return result[0][0] |
| 58 | |
| 59 | def load_into_cache(page_name): |
| 60 | args = prepare_args(page_name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 61 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 62 | html = build_html(args) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 63 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 64 | # create cache entry for predefined pages, if not exists |
| 65 | if page_name in get_predefined_pages(): |
| 66 | create_cache(page_name) |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 67 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 68 | import webnotes |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 69 | webnotes.conn.begin() |
| 70 | webnotes.conn.set_value('Web Cache', page_name, 'html', html) |
| 71 | webnotes.conn.commit() |
| 72 | |
| 73 | return html |
Anand Doshi | 51fc20b | 2012-07-11 18:56:13 +0530 | [diff] [blame] | 74 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 75 | def get_predefined_pages(): |
| 76 | """ |
| 77 | gets a list of predefined pages |
| 78 | they do not exist in `tabWeb Page` |
| 79 | """ |
| 80 | import os |
| 81 | import conf |
| 82 | import website.utils |
| 83 | |
| 84 | pages_path = os.path.join(conf.modules_path, 'website', 'templates', 'pages') |
| 85 | |
| 86 | page_list = [] |
| 87 | |
| 88 | for page in os.listdir(pages_path): |
| 89 | page_list.append(website.utils.scrub_page_name(page)) |
| 90 | |
| 91 | return page_list |
| 92 | |
| 93 | def prepare_args(page_name): |
| 94 | if page_name == 'index': |
| 95 | page_name = get_home_page() |
| 96 | |
| 97 | if page_name in get_predefined_pages(): |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 98 | args = { |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 99 | 'template': 'pages/%s.html' % page_name, |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 100 | 'name': page_name, |
| 101 | } |
| 102 | else: |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 103 | args = get_doc_fields(page_name) |
| 104 | |
| 105 | args.update(get_outer_env()) |
| 106 | |
| 107 | return args |
| 108 | |
| 109 | def get_home_page(): |
| 110 | import webnotes |
| 111 | doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page') |
| 112 | if doc_name: |
| 113 | page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name') |
| 114 | else: |
| 115 | page_name = 'login' |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 116 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 117 | return page_name |
| 118 | |
| 119 | def get_doc_fields(page_name): |
| 120 | import webnotes |
| 121 | doc_type, doc_name = webnotes.conn.get_value('Web Cache', page_name, ['doc_type', 'doc_name']) |
| 122 | |
| 123 | import webnotes.model.code |
| 124 | obj = webnotes.model.code.get_obj(doc_type, doc_name) |
| 125 | |
| 126 | if hasattr(obj, 'prepare_template_args'): |
| 127 | obj.prepare_template_args() |
| 128 | |
| 129 | args = obj.doc.fields |
| 130 | args['template'] = template_map[doc_type] |
Anand Doshi | 8c7e76b | 2012-07-11 18:40:57 +0530 | [diff] [blame] | 131 | |
| 132 | return args |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 133 | |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 134 | def get_outer_env(): |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 135 | """ |
| 136 | env dict for outer template |
| 137 | """ |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 138 | import webnotes |
| 139 | return { |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 140 | 'top_bar_items': webnotes.conn.sql("""\ |
| 141 | select * from `tabTop Bar Item` |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 142 | where parent='Website Settings' and parentfield='top_bar_items' |
| 143 | order by idx asc""", as_dict=1), |
| 144 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 145 | 'footer_items': webnotes.conn.sql("""\ |
| 146 | select * from `tabTop Bar Item` |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 147 | where parent='Website Settings' and parentfield='footer_items' |
| 148 | order by idx asc""", as_dict=1), |
| 149 | |
Anand Doshi | f7765a8 | 2012-07-11 19:05:27 +0530 | [diff] [blame] | 150 | 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext', |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 151 | 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'), |
Anand Doshi | 40fce89 | 2012-07-09 20:02:52 +0530 | [diff] [blame] | 152 | 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon') |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 153 | } |
| 154 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 155 | def build_html(args): |
| 156 | """ |
| 157 | build html using jinja2 templates |
| 158 | """ |
| 159 | import os |
| 160 | import conf |
| 161 | templates_path = os.path.join(conf.modules_path, 'website', 'templates') |
| 162 | |
| 163 | from jinja2 import Environment, FileSystemLoader |
| 164 | jenv = Environment(loader = FileSystemLoader(templates_path)) |
| 165 | html = jenv.get_template(args['template']).render(args) |
| 166 | return html |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 167 | |
| 168 | # cache management |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 169 | def search_cache(page_name): |
Anand Doshi | 1a51cc9 | 2012-07-12 19:03:58 +0530 | [diff] [blame] | 170 | if not page_name: return () |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 171 | import webnotes |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 172 | return webnotes.conn.sql("""\ |
| 173 | select html, doc_type, doc_name |
| 174 | from `tabWeb Cache` |
| 175 | where name = %s""", page_name) |
| 176 | |
| 177 | def create_cache(page_name, doc_type=None, doc_name=None): |
| 178 | # check if a record already exists |
| 179 | result = search_cache(page_name) |
| 180 | if result: return |
| 181 | |
| 182 | # create a Web Cache record |
| 183 | import webnotes.model.doc |
| 184 | d = webnotes.model.doc.Document('Web Cache') |
| 185 | d.name = page_name |
| 186 | d.doc_type = doc_type |
| 187 | d.doc_name = doc_name |
| 188 | d.html = None |
| 189 | d.save() |
| 190 | |
| 191 | def clear_cache(page_name, doc_type=None, doc_name=None): |
| 192 | """ |
| 193 | * if no page name, clear whole cache |
| 194 | * if page_name, doc_type and doc_name match, clear cache's copy |
| 195 | * else, raise exception that such a page already exists |
| 196 | """ |
Anand Doshi | 1a51cc9 | 2012-07-12 19:03:58 +0530 | [diff] [blame] | 197 | import webnotes |
| 198 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 199 | if not page_name: |
| 200 | webnotes.conn.sql("""update `tabWeb Cache` set html = ''""") |
| 201 | return |
| 202 | |
| 203 | result = search_cache(page_name) |
Anand Doshi | 1a51cc9 | 2012-07-12 19:03:58 +0530 | [diff] [blame] | 204 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 205 | if not doc_type or (result and result[0][1] == doc_type and result[0][2] == doc_name): |
| 206 | webnotes.conn.set_value('Web Cache', page_name, 'html', '') |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 207 | else: |
| 208 | webnotes.msgprint("""Page with name "%s" already exists as a %s. |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 209 | Please save it with another name.""" % (page_name, result[0][1]), |
| 210 | raise_exception=1) |
| 211 | |
| 212 | def delete_cache(page_name): |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 213 | """ |
| 214 | delete entry of page_name from Web Cache |
| 215 | used when: |
| 216 | * web page is deleted |
| 217 | * blog is un-published |
| 218 | """ |
| 219 | import webnotes |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 220 | webnotes.conn.sql("""delete from `tabWeb Cache` where name=%s""", page_name) |
| 221 | |
| 222 | def refresh_cache(): |
| 223 | """delete and re-create web cache entries""" |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame] | 224 | import webnotes |
Anand Doshi | 8790188 | 2012-07-11 20:04:32 +0530 | [diff] [blame] | 225 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 226 | webnotes.conn.sql("delete from `tabWeb Cache`") |
| 227 | |
| 228 | query_map = { |
| 229 | 'Web Page': """select page_name, name from `tabWeb Page` where docstatus=0""", |
| 230 | 'Blog': """\ |
| 231 | select page_name, name from `tabBlog` |
| 232 | where docstatus = 0 and ifnull(published, 0) = 1""", |
| 233 | 'Item': """\ |
| 234 | select page_name, name from `tabItem` |
| 235 | where docstatus = 0 and ifnull(show_in_website, 0) = 1""", |
| 236 | } |
| 237 | |
| 238 | for dt in query_map: |
| 239 | for result in webnotes.conn.sql(query_map[dt], as_dict=1): |
| 240 | create_cache(result['page_name'], dt, result['name']) |
| 241 | clear_cache(result['page_name'], dt, result['name']) |
| 242 | |
| 243 | for page_name in get_predefined_pages(): |
| 244 | create_cache(page_name, None, None) |
| 245 | clear_cache(page_name, None, None) |