blob: 1d07c638114c2c5a19897190ec9e1da18850b17b [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05301# 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 Doshi486f9df2012-07-19 13:40:31 +053017from __future__ import unicode_literals
Rushabh Mehta571377a2012-12-07 11:00:26 +053018
19import os
20import conf
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053021import webnotes
Rushabh Mehta571377a2012-12-07 11:00:26 +053022
Rushabh Mehtaa494b882012-12-07 12:44:45 +053023page_map = {
24 'Web Page': webnotes._dict({
25 "template": 'html/web_page.html',
26 "condition_field": "published"
27 }),
Rushabh Mehtab8ec3e72013-03-11 14:29:09 +053028 'Blog Post': webnotes._dict({
Rushabh Mehtaa494b882012-12-07 12:44:45 +053029 "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 Mehta173a0fd2012-12-14 16:39:27 +053035 }),
36 'Item Group': webnotes._dict({
37 "template": "html/product_group.html",
38 "condition_field": "show_in_website"
Rushabh Mehtaa494b882012-12-07 12:44:45 +053039 })
Rushabh Mehta571377a2012-12-07 11:00:26 +053040}
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053041
Rushabh Mehta1b151712012-12-27 16:50:24 +053042page_settings_map = {
Rushabh Mehtad6164132012-12-27 18:40:42 +053043 "about": "About Us Settings",
Rushabh Mehtaa2deb682013-03-08 10:44:25 +053044 "contact": "Contact Us Settings",
Rushabh Mehta1dd33012013-03-08 11:00:18 +053045 "blog": "website.helpers.blog.get_blog_template_args",
46 "writers": "website.helpers.blog.get_writers_args"
Rushabh Mehta1b151712012-12-27 16:50:24 +053047}
48
Rushabh Mehta89c7b412012-12-06 14:58:44 +053049def render(page_name):
50 """render html page"""
Rushabh Mehta89c7b412012-12-06 14:58:44 +053051 try:
52 if page_name:
53 html = get_html(page_name)
54 else:
55 html = get_html('index')
Anand Doshic9629a42013-02-12 12:54:58 +053056 except Exception:
Rushabh Mehtae109fa42012-12-19 10:14:59 +053057 html = get_html('error')
Rushabh Mehta89c7b412012-12-06 14:58:44 +053058
59 from webnotes.handler import eprint, print_zip
60 eprint("Content-Type: text/html")
61 print_zip(html)
62
63def get_html(page_name):
64 """get page html"""
65 page_name = scrub_page_name(page_name)
Rushabh Mehta89c7b412012-12-06 14:58:44 +053066
Rushabh Mehta571377a2012-12-07 11:00:26 +053067 html = ''
68
69 # load from cache, if auto cache clear is falsy
70 if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0):
71 html = webnotes.cache().get_value("page:" + page_name)
Rushabh Mehtae109fa42012-12-19 10:14:59 +053072 from_cache = True
Rushabh Mehta571377a2012-12-07 11:00:26 +053073
Rushabh Mehtae109fa42012-12-19 10:14:59 +053074 if not html:
Rushabh Mehta571377a2012-12-07 11:00:26 +053075 html = load_into_cache(page_name)
Rushabh Mehtae109fa42012-12-19 10:14:59 +053076 from_cache = False
Rushabh Mehta571377a2012-12-07 11:00:26 +053077
Rushabh Mehtae109fa42012-12-19 10:14:59 +053078 if not html:
79 html = get_html("404")
80
81 if page_name=="error":
Rushabh Mehta7edf8992012-12-25 18:18:17 +053082 html = html.replace("%(error)s", webnotes.getTraceback())
Rushabh Mehtae109fa42012-12-19 10:14:59 +053083 else:
84 comments = "\n\npage:"+page_name+\
85 "\nload status: " + (from_cache and "cache" or "fresh")
86 html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
Rushabh Mehta571377a2012-12-07 11:00:26 +053087
Rushabh Mehta89c7b412012-12-06 14:58:44 +053088 return html
Rushabh Mehta89c7b412012-12-06 14:58:44 +053089
Anand Doshi51146c02012-07-12 18:41:12 +053090def scrub_page_name(page_name):
91 if page_name.endswith('.html'):
92 page_name = page_name[:-5]
93
94 return page_name
95
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053096def page_name(title):
Anand Doshi72c945b2012-06-22 20:01:07 +053097 """make page name from title"""
98 import re
99 name = title.lower()
100 name = re.sub('[~!@#$%^&*()<>,."\']', '', name)
Nabin Hait80c68122013-01-17 13:24:51 +0530101 name = re.sub('[:/]', '-', name)
102
103 name = '-'.join(name.split())
104
105 # replace repeating hyphens
106 name = re.sub(r"(-)\1+", r"\1", name)
107
108 return name
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +0530109
110def update_page_name(doc, title):
111 """set page_name and check if it is unique"""
Rushabh Mehtaa2e4cb02012-12-06 17:09:38 +0530112 webnotes.conn.set(doc, "page_name", page_name(title))
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +0530113
Rushabh Mehta1b151712012-12-27 16:50:24 +0530114 standard_pages = get_template_pages()
115 if doc.page_name in standard_pages:
116 webnotes.conn.sql("""Page Name cannot be one of %s""" % ', '.join(standard_pages))
117
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +0530118 res = webnotes.conn.sql("""\
119 select count(*) from `tab%s`
120 where page_name=%s and name!=%s""" % (doc.doctype, '%s', '%s'),
121 (doc.page_name, doc.name))
122 if res and res[0][0] > 0:
123 webnotes.msgprint("""A %s with the same title already exists.
124 Please change the title of %s and save again."""
125 % (doc.doctype, doc.name), raise_exception=1)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530126
127 delete_page_cache(doc.page_name)
128
129def load_into_cache(page_name):
130 args = prepare_args(page_name)
Rushabh Mehtae109fa42012-12-19 10:14:59 +0530131 if not args:
132 return ""
Rushabh Mehta571377a2012-12-07 11:00:26 +0530133 html = build_html(args)
134 webnotes.cache().set_value("page:" + page_name, html)
135 return html
136
137def build_html(args):
138 from jinja2 import Environment, FileSystemLoader
139
140 templates_path = os.path.join(os.path.dirname(conf.__file__),
141 'app', 'website', 'templates')
142
Rushabh Mehta7837d802012-12-25 15:09:14 +0530143 args["len"] = len
144
Rushabh Mehta571377a2012-12-07 11:00:26 +0530145 jenv = Environment(loader = FileSystemLoader(templates_path))
146 html = jenv.get_template(args['template']).render(args)
147
148 return html
149
150def prepare_args(page_name):
151 if page_name == 'index':
152 page_name = get_home_page()
153
154 if page_name in get_template_pages():
Rushabh Mehta1b151712012-12-27 16:50:24 +0530155 args = webnotes._dict({
Nabin Haitd68fa3b2013-01-01 12:05:13 +0530156 'template': 'pages/%s.html' % page_name,
Rushabh Mehta571377a2012-12-07 11:00:26 +0530157 'name': page_name,
Rushabh Mehta1b151712012-12-27 16:50:24 +0530158 })
159 if page_name in page_settings_map:
Rushabh Mehtaa2deb682013-03-08 10:44:25 +0530160 target = page_settings_map[page_name]
161 if "." in target:
162 args.update(webnotes.get_method(target)())
163 else:
164 args.obj = webnotes.bean(page_settings_map[page_name]).obj
Rushabh Mehta571377a2012-12-07 11:00:26 +0530165 else:
166 args = get_doc_fields(page_name)
167
Rushabh Mehtae109fa42012-12-19 10:14:59 +0530168 if not args:
169 return False
170
Rushabh Mehtaf2b17d92013-02-21 10:49:37 +0530171 get_outer_env(page_name, args)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530172
173 return args
174
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530175def get_template_pages():
Rushabh Mehta571377a2012-12-07 11:00:26 +0530176 pages_path = os.path.join(os.path.dirname(conf.__file__), 'app',
177 'website', 'templates', 'pages')
178 page_list = []
179 for page in os.listdir(pages_path):
180 page_list.append(scrub_page_name(page))
181
182 return page_list
183
184def get_doc_fields(page_name):
185 doc_type, doc_name = get_source_doc(page_name)
Rushabh Mehtae109fa42012-12-19 10:14:59 +0530186 if not doc_type:
187 return False
188
Rushabh Mehta0e9e8482012-12-17 16:00:34 +0530189 obj = webnotes.get_obj(doc_type, doc_name, with_children=True)
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530190
Rushabh Mehta571377a2012-12-07 11:00:26 +0530191 if hasattr(obj, 'prepare_template_args'):
192 obj.prepare_template_args()
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530193
Rushabh Mehta571377a2012-12-07 11:00:26 +0530194 args = obj.doc.fields
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530195 args['template'] = page_map[doc_type].template
Rushabh Mehta0e9e8482012-12-17 16:00:34 +0530196 args['obj'] = obj
Rushabh Mehtafc19f252012-12-20 17:11:51 +0530197 args['int'] = int
Rushabh Mehta571377a2012-12-07 11:00:26 +0530198
199 return args
200
201def get_source_doc(page_name):
202 """get source doc for the given page name"""
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530203 for doctype in page_map:
Rushabh Mehta571377a2012-12-07 11:00:26 +0530204 name = webnotes.conn.sql("""select name from `tab%s` where
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530205 page_name=%s and ifnull(%s, 0)=1""" % (doctype, "%s",
206 page_map[doctype].condition_field), page_name)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530207 if name:
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530208 return doctype, name[0][0]
209
Rushabh Mehta571377a2012-12-07 11:00:26 +0530210 return None, None
211
Rushabh Mehtaf2b17d92013-02-21 10:49:37 +0530212def get_outer_env(page_name, args):
Rushabh Mehta87e48012013-02-20 15:02:21 +0530213 from webnotes.utils import get_request_site_address
214 from urllib import quote
215
Rushabh Mehta571377a2012-12-07 11:00:26 +0530216 all_top_items = webnotes.conn.sql("""\
217 select * from `tabTop Bar Item`
218 where parent='Website Settings' and parentfield='top_bar_items'
219 order by idx asc""", as_dict=1)
Rushabh Mehta60cc0cc2012-12-21 10:52:26 +0530220
Rushabh Mehta571377a2012-12-07 11:00:26 +0530221 top_items = [d for d in all_top_items if not d['parent_label']]
222
223 # attach child items to top bar
224 for d in all_top_items:
225 if d['parent_label']:
226 for t in top_items:
227 if t['label']==d['parent_label']:
228 if not 'child_items' in t:
229 t['child_items'] = []
230 t['child_items'].append(d)
231 break
232
Nabin Haitab573ce2012-12-21 11:01:31 +0530233 if top_items and ("products" in [d.url.split(".")[0] for d in top_items if d.url]):
Rushabh Mehtafc19f252012-12-20 17:11:51 +0530234 # product categories
235 products = webnotes.conn.sql("""select t1.item_group as label,
Rushabh Mehta5f183982013-01-01 10:55:58 +0530236 t2.page_name as url,
Rushabh Mehtafc19f252012-12-20 17:11:51 +0530237 ifnull(t1.indent,0) as indent
238 from `tabWebsite Product Category` t1, `tabItem Group` t2
239 where t1.item_group = t2.name
240 and ifnull(t2.show_in_website,0)=1 order by t1.idx""", as_dict=1)
Rushabh Mehtabda54e52012-12-24 12:52:15 +0530241 products_item = filter(lambda d: d.url and d.url.split(".")[0]=="products", top_items)[0]
Rushabh Mehtafc19f252012-12-20 17:11:51 +0530242 products_item.child_items = products
243
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530244 ret = webnotes._dict({
Rushabh Mehta571377a2012-12-07 11:00:26 +0530245 'top_bar_items': top_items,
Rushabh Mehta571377a2012-12-07 11:00:26 +0530246 'footer_items': webnotes.conn.sql("""\
247 select * from `tabTop Bar Item`
248 where parent='Website Settings' and parentfield='footer_items'
249 order by idx asc""", as_dict=1),
250
Rushabh Mehta8ff938a2013-03-06 11:20:56 +0530251 'int':int,
252 "webnotes": webnotes
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530253 })
254
Rushabh Mehtaf2b17d92013-02-21 10:49:37 +0530255 args.update(ret)
256
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530257 settings = webnotes.doc("Website Settings", "Website Settings")
Rushabh Mehta87e48012013-02-20 15:02:21 +0530258 for k in ["brand_html", "copyright", "address", "top_bar_background", "favicon",
Rushabh Mehtaf2b17d92013-02-21 10:49:37 +0530259 "facebook_share", "google_plus_one", "twitter_share", "linked_in_share", "twitter_share_via"]:
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530260 if k in settings.fields:
Rushabh Mehtaf2b17d92013-02-21 10:49:37 +0530261 args[k] = settings.fields.get(k)
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530262
Rushabh Mehta891ff092013-02-27 10:51:26 +0530263 for k in ["facebook_share", "google_plus_one", "twitter_share", "linked_in_share"]:
264 args[k] = int(args.get(k) or 0)
265
Rushabh Mehtaf2b17d92013-02-21 10:49:37 +0530266 if not args.brand_html:
267 args.brand_html = "ERPNext"
268 if not args.top_bar_background:
269 args.top_bar_background = "Black"
Rushabh Mehta87e48012013-02-20 15:02:21 +0530270
Rushabh Mehta1e037fb2013-02-21 11:11:21 +0530271 args.url = quote(str(get_request_site_address(full_address=True)), str(""))
272 args.encoded_title = quote(str(args.title or ""), str(""))
Rushabh Mehta87e48012013-02-20 15:02:21 +0530273
Rushabh Mehtaf2b17d92013-02-21 10:49:37 +0530274 return args
Rushabh Mehta571377a2012-12-07 11:00:26 +0530275
276def get_home_page():
277 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
278 if doc_name:
279 page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
280 else:
281 page_name = 'login'
282
283 return page_name
284
Rushabh Mehta98b99bd2012-12-07 12:55:06 +0530285def clear_cache(page_name=None):
Rushabh Mehta571377a2012-12-07 11:00:26 +0530286 if page_name:
287 delete_page_cache(page_name)
288 else:
Rushabh Mehta28b0b1c2013-01-22 11:04:55 +0530289 cache = webnotes.cache()
290 for p in get_all_pages():
291 cache.delete_value("page:" + p)
292
293def get_all_pages():
294 all_pages = get_template_pages()
295 all_pages += page_settings_map.keys()
296 for doctype in page_map:
297 all_pages += [p[0] for p in webnotes.conn.sql("""select distinct page_name
298 from `tab%s`""" % doctype) if p[0]]
299
300 return all_pages
301
Rushabh Mehta571377a2012-12-07 11:00:26 +0530302def delete_page_cache(page_name):
Anand Doshi65cef742012-12-30 19:58:24 +0530303 if page_name:
304 webnotes.cache().delete_value("page:" + page_name)
Rushabh Mehta5f1d57e2012-12-27 14:05:02 +0530305
306def url_for_website(url):
307 if url and not url.lower().startswith("http"):
308 return "files/" + url
309 else:
310 return url