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