Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +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 | 486f9df | 2012-07-19 13:40:31 +0530 | [diff] [blame] | 17 | from __future__ import unicode_literals |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 18 | |
| 19 | import os |
| 20 | import conf |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 21 | import webnotes |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 22 | from webnotes.utils import cstr |
| 23 | |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 24 | page_map = { |
| 25 | 'Web Page': webnotes._dict({ |
| 26 | "template": 'html/web_page.html', |
| 27 | "condition_field": "published" |
| 28 | }), |
| 29 | 'Blog': webnotes._dict({ |
| 30 | "template": 'html/blog_page.html', |
| 31 | "condition_field": "published", |
| 32 | }), |
| 33 | 'Item': webnotes._dict({ |
| 34 | "template": 'html/product_page.html', |
| 35 | "condition_field": "show_in_website", |
| 36 | }) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 37 | } |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 38 | |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 39 | def render(page_name): |
| 40 | """render html page""" |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 41 | try: |
| 42 | if page_name: |
| 43 | html = get_html(page_name) |
| 44 | else: |
| 45 | html = get_html('index') |
| 46 | except Exception, e: |
| 47 | html = get_html('404') |
| 48 | |
| 49 | from webnotes.handler import eprint, print_zip |
| 50 | eprint("Content-Type: text/html") |
| 51 | print_zip(html) |
| 52 | |
| 53 | def get_html(page_name): |
| 54 | """get page html""" |
| 55 | page_name = scrub_page_name(page_name) |
| 56 | comments = get_comments(page_name) |
| 57 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 58 | html = '' |
| 59 | |
| 60 | # load from cache, if auto cache clear is falsy |
| 61 | if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0): |
| 62 | html = webnotes.cache().get_value("page:" + page_name) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 63 | |
Rushabh Mehta | 7c24a42 | 2012-12-07 13:29:32 +0530 | [diff] [blame] | 64 | if html: |
| 65 | comments += "\nload status: cache" |
| 66 | else: |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 67 | html = load_into_cache(page_name) |
Rushabh Mehta | b5c3970 | 2012-12-07 12:54:08 +0530 | [diff] [blame] | 68 | comments += "\nload status: fresh" |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 69 | |
| 70 | # insert comments |
| 71 | html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) |
| 72 | |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 73 | return html |
| 74 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 75 | def get_comments(page_name): |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 76 | if page_name == '404': |
| 77 | comments = """error: %s""" % webnotes.getTraceback() |
| 78 | else: |
| 79 | comments = """page: %s""" % page_name |
| 80 | |
| 81 | return comments |
| 82 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 83 | def scrub_page_name(page_name): |
| 84 | if page_name.endswith('.html'): |
| 85 | page_name = page_name[:-5] |
| 86 | |
| 87 | return page_name |
| 88 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 89 | def page_name(title): |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 90 | """make page name from title""" |
| 91 | import re |
| 92 | name = title.lower() |
| 93 | name = re.sub('[~!@#$%^&*()<>,."\']', '', name) |
| 94 | return '-'.join(name.split()[:4]) |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 95 | |
| 96 | def update_page_name(doc, title): |
| 97 | """set page_name and check if it is unique""" |
Rushabh Mehta | a2e4cb0 | 2012-12-06 17:09:38 +0530 | [diff] [blame] | 98 | webnotes.conn.set(doc, "page_name", page_name(title)) |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 99 | |
| 100 | res = webnotes.conn.sql("""\ |
| 101 | select count(*) from `tab%s` |
| 102 | where page_name=%s and name!=%s""" % (doc.doctype, '%s', '%s'), |
| 103 | (doc.page_name, doc.name)) |
| 104 | if res and res[0][0] > 0: |
| 105 | webnotes.msgprint("""A %s with the same title already exists. |
| 106 | Please change the title of %s and save again.""" |
| 107 | % (doc.doctype, doc.name), raise_exception=1) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 108 | |
| 109 | delete_page_cache(doc.page_name) |
| 110 | |
| 111 | def load_into_cache(page_name): |
| 112 | args = prepare_args(page_name) |
| 113 | html = build_html(args) |
| 114 | webnotes.cache().set_value("page:" + page_name, html) |
| 115 | return html |
| 116 | |
| 117 | def build_html(args): |
| 118 | from jinja2 import Environment, FileSystemLoader |
| 119 | |
| 120 | templates_path = os.path.join(os.path.dirname(conf.__file__), |
| 121 | 'app', 'website', 'templates') |
| 122 | |
| 123 | jenv = Environment(loader = FileSystemLoader(templates_path)) |
| 124 | html = jenv.get_template(args['template']).render(args) |
| 125 | |
| 126 | return html |
| 127 | |
| 128 | def prepare_args(page_name): |
| 129 | if page_name == 'index': |
| 130 | page_name = get_home_page() |
| 131 | |
| 132 | if page_name in get_template_pages(): |
| 133 | args = { |
| 134 | 'template': 'pages/%s.html' % page_name, |
| 135 | 'name': page_name, |
| 136 | } |
| 137 | else: |
| 138 | args = get_doc_fields(page_name) |
| 139 | |
| 140 | args.update(get_outer_env()) |
| 141 | |
| 142 | return args |
| 143 | |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 144 | def get_template_pages(): |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 145 | pages_path = os.path.join(os.path.dirname(conf.__file__), 'app', |
| 146 | 'website', 'templates', 'pages') |
| 147 | page_list = [] |
| 148 | for page in os.listdir(pages_path): |
| 149 | page_list.append(scrub_page_name(page)) |
| 150 | |
| 151 | return page_list |
| 152 | |
| 153 | def get_doc_fields(page_name): |
| 154 | doc_type, doc_name = get_source_doc(page_name) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 155 | obj = webnotes.get_obj(doc_type, doc_name) |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 156 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 157 | if hasattr(obj, 'prepare_template_args'): |
| 158 | obj.prepare_template_args() |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 159 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 160 | args = obj.doc.fields |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 161 | args['template'] = page_map[doc_type].template |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 162 | |
| 163 | return args |
| 164 | |
| 165 | def get_source_doc(page_name): |
| 166 | """get source doc for the given page name""" |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 167 | for doctype in page_map: |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 168 | name = webnotes.conn.sql("""select name from `tab%s` where |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 169 | page_name=%s and ifnull(%s, 0)=1""" % (doctype, "%s", |
| 170 | page_map[doctype].condition_field), page_name) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 171 | if name: |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 172 | return doctype, name[0][0] |
| 173 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 174 | return None, None |
| 175 | |
| 176 | def get_outer_env(): |
| 177 | all_top_items = webnotes.conn.sql("""\ |
| 178 | select * from `tabTop Bar Item` |
| 179 | where parent='Website Settings' and parentfield='top_bar_items' |
| 180 | order by idx asc""", as_dict=1) |
| 181 | |
| 182 | top_items = [d for d in all_top_items if not d['parent_label']] |
| 183 | |
| 184 | # attach child items to top bar |
| 185 | for d in all_top_items: |
| 186 | if d['parent_label']: |
| 187 | for t in top_items: |
| 188 | if t['label']==d['parent_label']: |
| 189 | if not 'child_items' in t: |
| 190 | t['child_items'] = [] |
| 191 | t['child_items'].append(d) |
| 192 | break |
| 193 | |
| 194 | return { |
| 195 | 'top_bar_items': top_items, |
| 196 | |
| 197 | 'footer_items': webnotes.conn.sql("""\ |
| 198 | select * from `tabTop Bar Item` |
| 199 | where parent='Website Settings' and parentfield='footer_items' |
| 200 | order by idx asc""", as_dict=1), |
| 201 | |
| 202 | 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext', |
| 203 | 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'), |
| 204 | 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon') |
| 205 | } |
| 206 | |
| 207 | def get_home_page(): |
| 208 | doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page') |
| 209 | if doc_name: |
| 210 | page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name') |
| 211 | else: |
| 212 | page_name = 'login' |
| 213 | |
| 214 | return page_name |
| 215 | |
Rushabh Mehta | 98b99bd | 2012-12-07 12:55:06 +0530 | [diff] [blame] | 216 | def clear_cache(page_name=None): |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 217 | if page_name: |
| 218 | delete_page_cache(page_name) |
| 219 | else: |
| 220 | webnotes.cache().delete_keys("page:") |
| 221 | |
| 222 | def delete_page_cache(page_name): |
| 223 | webnotes.cache().delete_value("page:" + page_name) |