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 |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 20 | |
| 21 | import os |
| 22 | import conf |
| 23 | import webnotes |
| 24 | import website.utils |
Rushabh Mehta | 7643272 | 2012-12-06 16:52:17 +0530 | [diff] [blame] | 25 | from webnotes.utils import cstr |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 26 | |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 27 | template_map = { |
| 28 | 'Web Page': 'html/web_page.html', |
| 29 | 'Blog': 'html/blog_page.html', |
| 30 | 'Item': 'html/product_page.html', |
| 31 | } |
| 32 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 33 | def get_page_html(page_name, comments=''): |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 34 | html = '' |
| 35 | |
| 36 | # load from cache, if auto cache clear is falsy |
| 37 | if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0): |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 38 | html = webnotes.cache().get_value("page:" + page_name) |
| 39 | comments += "\n\npage load status: fresh" |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 40 | |
| 41 | if not html: |
| 42 | html = load_into_cache(page_name) |
Rushabh Mehta | 87192da | 2012-12-06 16:45:00 +0530 | [diff] [blame] | 43 | comments += "\n\npage load status: cache" |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 44 | |
| 45 | # insert comments |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 46 | html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) |
| 47 | |
| 48 | return html |
| 49 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 50 | def load_into_cache(page_name): |
| 51 | templates_path = os.path.join(os.path.dirname(conf.__file__), |
| 52 | 'app', 'website', 'templates') |
| 53 | args = prepare_args(page_name) |
| 54 | |
| 55 | from jinja2 import Environment, FileSystemLoader |
| 56 | jenv = Environment(loader = FileSystemLoader(templates_path)) |
| 57 | html = jenv.get_template(args['template']).render(args) |
| 58 | return html |
| 59 | |
| 60 | html = build_html() |
| 61 | webnotes.cache().set_value("page:" + page_name, html) |
| 62 | return html |
| 63 | |
| 64 | def prepare_args(page_name): |
| 65 | if page_name == 'index': |
| 66 | page_name = get_home_page() |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 67 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 68 | if page_name in get_predefined_pages(): |
| 69 | args = { |
| 70 | 'template': 'pages/%s.html' % page_name, |
| 71 | 'name': page_name, |
| 72 | } |
| 73 | else: |
| 74 | args = get_doc_fields(page_name) |
| 75 | |
| 76 | args.update(get_outer_env()) |
| 77 | |
| 78 | return args |
| 79 | |
| 80 | def load_from_cache(page_name): |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 81 | result = search_cache(page_name) |
| 82 | |
| 83 | if not result: |
| 84 | if page_name in get_predefined_pages(): |
| 85 | # if a predefined page doesn't exist, load it into cache |
| 86 | return None |
| 87 | else: |
| 88 | # if page doesn't exist, raise exception |
| 89 | raise Exception, "Page %s not found" % page_name |
| 90 | |
| 91 | return result[0][0] |
| 92 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 93 | def get_predefined_pages(): |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 94 | pages_path = os.path.join(os.path.dirname(conf.__file__), 'app', |
| 95 | 'website', 'templates', 'pages') |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 96 | page_list = [] |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 97 | for page in os.listdir(pages_path): |
| 98 | page_list.append(website.utils.scrub_page_name(page)) |
| 99 | |
| 100 | return page_list |
| 101 | |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 102 | def get_home_page(): |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 103 | doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page') |
| 104 | if doc_name: |
| 105 | page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name') |
| 106 | else: |
| 107 | page_name = 'login' |
| 108 | |
| 109 | return page_name |
| 110 | |
| 111 | def get_doc_fields(page_name): |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 112 | doc_type, doc_name = get_source_doc(page_name) |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 113 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 114 | obj = webnotes.get_obj(doc_type, doc_name) |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 115 | |
| 116 | if hasattr(obj, 'prepare_template_args'): |
| 117 | obj.prepare_template_args() |
| 118 | |
| 119 | args = obj.doc.fields |
| 120 | args['template'] = template_map[doc_type] |
| 121 | |
| 122 | return args |
| 123 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 124 | def get_source_doc(page_name): |
| 125 | """get source doc for the given page name""" |
| 126 | for doctype in [('Web Page', 'published'), ('Blog', 'published'), |
| 127 | ('Item', 'show_in_website')]: |
| 128 | name = webnotes.conn.sql("""select name from `tab%s` where |
| 129 | page_name=%s and ifnull(`%s`, 0)=1""" % (doctype[0], "%s", doctype[1]), |
| 130 | page_name) |
| 131 | if name: |
| 132 | return doctype[0], name[0][0] |
| 133 | |
| 134 | return None, None |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 135 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 136 | def get_outer_env(): |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 137 | all_top_items = webnotes.conn.sql("""\ |
| 138 | select * from `tabTop Bar Item` |
| 139 | where parent='Website Settings' and parentfield='top_bar_items' |
| 140 | order by idx asc""", as_dict=1) |
| 141 | |
| 142 | top_items = [d for d in all_top_items if not d['parent_label']] |
| 143 | |
| 144 | # attach child items to top bar |
| 145 | for d in all_top_items: |
| 146 | if d['parent_label']: |
| 147 | for t in top_items: |
| 148 | if t['label']==d['parent_label']: |
| 149 | if not 'child_items' in t: |
| 150 | t['child_items'] = [] |
| 151 | t['child_items'].append(d) |
| 152 | break |
| 153 | |
| 154 | return { |
| 155 | 'top_bar_items': top_items, |
| 156 | |
| 157 | 'footer_items': webnotes.conn.sql("""\ |
| 158 | select * from `tabTop Bar Item` |
| 159 | where parent='Website Settings' and parentfield='footer_items' |
| 160 | order by idx asc""", as_dict=1), |
| 161 | |
| 162 | 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext', |
| 163 | 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'), |
| 164 | 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon') |
| 165 | } |
| 166 | |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 167 | def clear_cache(page_name, doc_type=None, doc_name=None): |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 168 | if page_name: |
| 169 | delete_page_cache(page_name) |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 170 | else: |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 171 | webnotes.cache().flush_keys("page:") |
Rushabh Mehta | 2fa2f71 | 2012-09-24 19:13:42 +0530 | [diff] [blame] | 172 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 173 | def delete_page_cache(page_name): |
| 174 | webnotes.cache().delete_value("page:" + page_name) |