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 | |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 23 | page_map = { |
| 24 | 'Web Page': webnotes._dict({ |
| 25 | "template": 'html/web_page.html', |
| 26 | "condition_field": "published" |
| 27 | }), |
| 28 | 'Blog': webnotes._dict({ |
| 29 | "template": 'html/blog_page.html', |
| 30 | "condition_field": "published", |
| 31 | }), |
| 32 | 'Item': webnotes._dict({ |
| 33 | "template": 'html/product_page.html', |
| 34 | "condition_field": "show_in_website", |
Rushabh Mehta | 173a0fd | 2012-12-14 16:39:27 +0530 | [diff] [blame] | 35 | }), |
| 36 | 'Item Group': webnotes._dict({ |
| 37 | "template": "html/product_group.html", |
| 38 | "condition_field": "show_in_website" |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 39 | }) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 40 | } |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 41 | |
Rushabh Mehta | 1b15171 | 2012-12-27 16:50:24 +0530 | [diff] [blame] | 42 | page_settings_map = { |
Rushabh Mehta | d616413 | 2012-12-27 18:40:42 +0530 | [diff] [blame] | 43 | "about": "About Us Settings", |
| 44 | "contact": "Contact Us Settings" |
Rushabh Mehta | 1b15171 | 2012-12-27 16:50:24 +0530 | [diff] [blame] | 45 | } |
| 46 | |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 47 | def render(page_name): |
| 48 | """render html page""" |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 49 | try: |
| 50 | if page_name: |
| 51 | html = get_html(page_name) |
| 52 | else: |
| 53 | html = get_html('index') |
Anand Doshi | c9629a4 | 2013-02-12 12:54:58 +0530 | [diff] [blame] | 54 | except Exception: |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 55 | html = get_html('error') |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 56 | |
| 57 | from webnotes.handler import eprint, print_zip |
| 58 | eprint("Content-Type: text/html") |
| 59 | print_zip(html) |
| 60 | |
| 61 | def get_html(page_name): |
| 62 | """get page html""" |
| 63 | page_name = scrub_page_name(page_name) |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 64 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 65 | html = '' |
| 66 | |
| 67 | # load from cache, if auto cache clear is falsy |
| 68 | if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0): |
| 69 | html = webnotes.cache().get_value("page:" + page_name) |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 70 | from_cache = True |
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: |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 73 | html = load_into_cache(page_name) |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 74 | from_cache = False |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 75 | |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 76 | if not html: |
| 77 | html = get_html("404") |
| 78 | |
| 79 | if page_name=="error": |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 80 | html = html.replace("%(error)s", webnotes.getTraceback()) |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 81 | else: |
| 82 | comments = "\n\npage:"+page_name+\ |
| 83 | "\nload status: " + (from_cache and "cache" or "fresh") |
| 84 | html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 85 | |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 86 | return html |
Rushabh Mehta | 89c7b41 | 2012-12-06 14:58:44 +0530 | [diff] [blame] | 87 | |
Anand Doshi | 51146c0 | 2012-07-12 18:41:12 +0530 | [diff] [blame] | 88 | def scrub_page_name(page_name): |
| 89 | if page_name.endswith('.html'): |
| 90 | page_name = page_name[:-5] |
| 91 | |
| 92 | return page_name |
| 93 | |
Rushabh Mehta | ab1148c | 2012-01-31 18:01:16 +0530 | [diff] [blame] | 94 | def page_name(title): |
Anand Doshi | 72c945b | 2012-06-22 20:01:07 +0530 | [diff] [blame] | 95 | """make page name from title""" |
| 96 | import re |
| 97 | name = title.lower() |
| 98 | name = re.sub('[~!@#$%^&*()<>,."\']', '', name) |
Nabin Hait | 80c6812 | 2013-01-17 13:24:51 +0530 | [diff] [blame] | 99 | name = re.sub('[:/]', '-', name) |
| 100 | |
| 101 | name = '-'.join(name.split()) |
| 102 | |
| 103 | # replace repeating hyphens |
| 104 | name = re.sub(r"(-)\1+", r"\1", name) |
| 105 | |
| 106 | return name |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 107 | |
| 108 | def update_page_name(doc, title): |
| 109 | """set page_name and check if it is unique""" |
Rushabh Mehta | a2e4cb0 | 2012-12-06 17:09:38 +0530 | [diff] [blame] | 110 | webnotes.conn.set(doc, "page_name", page_name(title)) |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 111 | |
Rushabh Mehta | 1b15171 | 2012-12-27 16:50:24 +0530 | [diff] [blame] | 112 | standard_pages = get_template_pages() |
| 113 | if doc.page_name in standard_pages: |
| 114 | webnotes.conn.sql("""Page Name cannot be one of %s""" % ', '.join(standard_pages)) |
| 115 | |
Rushabh Mehta | df0b00a | 2012-12-06 16:15:38 +0530 | [diff] [blame] | 116 | res = webnotes.conn.sql("""\ |
| 117 | select count(*) from `tab%s` |
| 118 | where page_name=%s and name!=%s""" % (doc.doctype, '%s', '%s'), |
| 119 | (doc.page_name, doc.name)) |
| 120 | if res and res[0][0] > 0: |
| 121 | webnotes.msgprint("""A %s with the same title already exists. |
| 122 | Please change the title of %s and save again.""" |
| 123 | % (doc.doctype, doc.name), raise_exception=1) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 124 | |
| 125 | delete_page_cache(doc.page_name) |
| 126 | |
| 127 | def load_into_cache(page_name): |
| 128 | args = prepare_args(page_name) |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 129 | if not args: |
| 130 | return "" |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 131 | html = build_html(args) |
| 132 | webnotes.cache().set_value("page:" + page_name, html) |
| 133 | return html |
| 134 | |
| 135 | def build_html(args): |
| 136 | from jinja2 import Environment, FileSystemLoader |
| 137 | |
| 138 | templates_path = os.path.join(os.path.dirname(conf.__file__), |
| 139 | 'app', 'website', 'templates') |
| 140 | |
Rushabh Mehta | 7837d80 | 2012-12-25 15:09:14 +0530 | [diff] [blame] | 141 | args["len"] = len |
| 142 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 143 | jenv = Environment(loader = FileSystemLoader(templates_path)) |
| 144 | html = jenv.get_template(args['template']).render(args) |
| 145 | |
| 146 | return html |
| 147 | |
| 148 | def prepare_args(page_name): |
| 149 | if page_name == 'index': |
| 150 | page_name = get_home_page() |
| 151 | |
| 152 | if page_name in get_template_pages(): |
Rushabh Mehta | 1b15171 | 2012-12-27 16:50:24 +0530 | [diff] [blame] | 153 | args = webnotes._dict({ |
Nabin Hait | d68fa3b | 2013-01-01 12:05:13 +0530 | [diff] [blame] | 154 | 'template': 'pages/%s.html' % page_name, |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 155 | 'name': page_name, |
Rushabh Mehta | 1b15171 | 2012-12-27 16:50:24 +0530 | [diff] [blame] | 156 | }) |
| 157 | if page_name in page_settings_map: |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 158 | args.obj = webnotes.bean(page_settings_map[page_name]).obj |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 159 | else: |
| 160 | args = get_doc_fields(page_name) |
| 161 | |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 162 | if not args: |
| 163 | return False |
| 164 | |
Rushabh Mehta | f2b17d9 | 2013-02-21 10:49:37 +0530 | [diff] [blame] | 165 | get_outer_env(page_name, args) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 166 | |
| 167 | return args |
| 168 | |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 169 | def get_template_pages(): |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 170 | pages_path = os.path.join(os.path.dirname(conf.__file__), 'app', |
| 171 | 'website', 'templates', 'pages') |
| 172 | page_list = [] |
| 173 | for page in os.listdir(pages_path): |
| 174 | page_list.append(scrub_page_name(page)) |
| 175 | |
| 176 | return page_list |
| 177 | |
| 178 | def get_doc_fields(page_name): |
| 179 | doc_type, doc_name = get_source_doc(page_name) |
Rushabh Mehta | e109fa4 | 2012-12-19 10:14:59 +0530 | [diff] [blame] | 180 | if not doc_type: |
| 181 | return False |
| 182 | |
Rushabh Mehta | 0e9e848 | 2012-12-17 16:00:34 +0530 | [diff] [blame] | 183 | obj = webnotes.get_obj(doc_type, doc_name, with_children=True) |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 184 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 185 | if hasattr(obj, 'prepare_template_args'): |
| 186 | obj.prepare_template_args() |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 187 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 188 | args = obj.doc.fields |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 189 | args['template'] = page_map[doc_type].template |
Rushabh Mehta | 0e9e848 | 2012-12-17 16:00:34 +0530 | [diff] [blame] | 190 | args['obj'] = obj |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 191 | args['int'] = int |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 192 | |
| 193 | return args |
| 194 | |
| 195 | def get_source_doc(page_name): |
| 196 | """get source doc for the given page name""" |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 197 | for doctype in page_map: |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 198 | name = webnotes.conn.sql("""select name from `tab%s` where |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 199 | page_name=%s and ifnull(%s, 0)=1""" % (doctype, "%s", |
| 200 | page_map[doctype].condition_field), page_name) |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 201 | if name: |
Rushabh Mehta | a494b88 | 2012-12-07 12:44:45 +0530 | [diff] [blame] | 202 | return doctype, name[0][0] |
| 203 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 204 | return None, None |
| 205 | |
Rushabh Mehta | f2b17d9 | 2013-02-21 10:49:37 +0530 | [diff] [blame] | 206 | def get_outer_env(page_name, args): |
Rushabh Mehta | 87e4801 | 2013-02-20 15:02:21 +0530 | [diff] [blame] | 207 | from webnotes.utils import get_request_site_address |
| 208 | from urllib import quote |
| 209 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 210 | all_top_items = webnotes.conn.sql("""\ |
| 211 | select * from `tabTop Bar Item` |
| 212 | where parent='Website Settings' and parentfield='top_bar_items' |
| 213 | order by idx asc""", as_dict=1) |
Rushabh Mehta | 60cc0cc | 2012-12-21 10:52:26 +0530 | [diff] [blame] | 214 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 215 | top_items = [d for d in all_top_items if not d['parent_label']] |
| 216 | |
| 217 | # attach child items to top bar |
| 218 | for d in all_top_items: |
| 219 | if d['parent_label']: |
| 220 | for t in top_items: |
| 221 | if t['label']==d['parent_label']: |
| 222 | if not 'child_items' in t: |
| 223 | t['child_items'] = [] |
| 224 | t['child_items'].append(d) |
| 225 | break |
| 226 | |
Nabin Hait | ab573ce | 2012-12-21 11:01:31 +0530 | [diff] [blame] | 227 | 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] | 228 | # product categories |
| 229 | products = webnotes.conn.sql("""select t1.item_group as label, |
Rushabh Mehta | 5f18398 | 2013-01-01 10:55:58 +0530 | [diff] [blame] | 230 | t2.page_name as url, |
Rushabh Mehta | fc19f25 | 2012-12-20 17:11:51 +0530 | [diff] [blame] | 231 | ifnull(t1.indent,0) as indent |
| 232 | from `tabWebsite Product Category` t1, `tabItem Group` t2 |
| 233 | where t1.item_group = t2.name |
| 234 | 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] | 235 | 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] | 236 | products_item.child_items = products |
| 237 | |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 238 | ret = webnotes._dict({ |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 239 | 'top_bar_items': top_items, |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 240 | 'footer_items': webnotes.conn.sql("""\ |
| 241 | select * from `tabTop Bar Item` |
| 242 | where parent='Website Settings' and parentfield='footer_items' |
| 243 | order by idx asc""", as_dict=1), |
| 244 | |
Rushabh Mehta | 8ff938a | 2013-03-06 11:20:56 +0530 | [diff] [blame] | 245 | 'int':int, |
| 246 | "webnotes": webnotes |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 247 | }) |
| 248 | |
Rushabh Mehta | f2b17d9 | 2013-02-21 10:49:37 +0530 | [diff] [blame] | 249 | args.update(ret) |
| 250 | |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 251 | settings = webnotes.doc("Website Settings", "Website Settings") |
Rushabh Mehta | 87e4801 | 2013-02-20 15:02:21 +0530 | [diff] [blame] | 252 | for k in ["brand_html", "copyright", "address", "top_bar_background", "favicon", |
Rushabh Mehta | f2b17d9 | 2013-02-21 10:49:37 +0530 | [diff] [blame] | 253 | "facebook_share", "google_plus_one", "twitter_share", "linked_in_share", "twitter_share_via"]: |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 254 | if k in settings.fields: |
Rushabh Mehta | f2b17d9 | 2013-02-21 10:49:37 +0530 | [diff] [blame] | 255 | args[k] = settings.fields.get(k) |
Rushabh Mehta | 7edf899 | 2012-12-25 18:18:17 +0530 | [diff] [blame] | 256 | |
Rushabh Mehta | 891ff09 | 2013-02-27 10:51:26 +0530 | [diff] [blame] | 257 | for k in ["facebook_share", "google_plus_one", "twitter_share", "linked_in_share"]: |
| 258 | args[k] = int(args.get(k) or 0) |
| 259 | |
Rushabh Mehta | f2b17d9 | 2013-02-21 10:49:37 +0530 | [diff] [blame] | 260 | if not args.brand_html: |
| 261 | args.brand_html = "ERPNext" |
| 262 | if not args.top_bar_background: |
| 263 | args.top_bar_background = "Black" |
Rushabh Mehta | 87e4801 | 2013-02-20 15:02:21 +0530 | [diff] [blame] | 264 | |
Rushabh Mehta | 1e037fb | 2013-02-21 11:11:21 +0530 | [diff] [blame] | 265 | args.url = quote(str(get_request_site_address(full_address=True)), str("")) |
| 266 | args.encoded_title = quote(str(args.title or ""), str("")) |
Rushabh Mehta | 87e4801 | 2013-02-20 15:02:21 +0530 | [diff] [blame] | 267 | |
Rushabh Mehta | f2b17d9 | 2013-02-21 10:49:37 +0530 | [diff] [blame] | 268 | return args |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 269 | |
| 270 | def get_home_page(): |
| 271 | doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page') |
| 272 | if doc_name: |
| 273 | page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name') |
| 274 | else: |
| 275 | page_name = 'login' |
| 276 | |
| 277 | return page_name |
| 278 | |
Rushabh Mehta | 98b99bd | 2012-12-07 12:55:06 +0530 | [diff] [blame] | 279 | def clear_cache(page_name=None): |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 280 | if page_name: |
| 281 | delete_page_cache(page_name) |
| 282 | else: |
Rushabh Mehta | 28b0b1c | 2013-01-22 11:04:55 +0530 | [diff] [blame] | 283 | cache = webnotes.cache() |
| 284 | for p in get_all_pages(): |
| 285 | cache.delete_value("page:" + p) |
| 286 | |
| 287 | def get_all_pages(): |
| 288 | all_pages = get_template_pages() |
| 289 | all_pages += page_settings_map.keys() |
| 290 | for doctype in page_map: |
| 291 | all_pages += [p[0] for p in webnotes.conn.sql("""select distinct page_name |
| 292 | from `tab%s`""" % doctype) if p[0]] |
| 293 | |
| 294 | return all_pages |
| 295 | |
Rushabh Mehta | 571377a | 2012-12-07 11:00:26 +0530 | [diff] [blame] | 296 | def delete_page_cache(page_name): |
Anand Doshi | 65cef74 | 2012-12-30 19:58:24 +0530 | [diff] [blame] | 297 | if page_name: |
| 298 | webnotes.cache().delete_value("page:" + page_name) |
Rushabh Mehta | 5f1d57e | 2012-12-27 14:05:02 +0530 | [diff] [blame] | 299 | |
| 300 | def url_for_website(url): |
| 301 | if url and not url.lower().startswith("http"): |
| 302 | return "files/" + url |
| 303 | else: |
| 304 | return url |