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