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