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