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