blob: cd1b6a59745f27b89f12c01e1b5c4b21253ca99e [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 +053022from webnotes.utils import cstr
23
Rushabh Mehtaa494b882012-12-07 12:44:45 +053024page_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 Mehta173a0fd2012-12-14 16:39:27 +053036 }),
37 'Item Group': webnotes._dict({
38 "template": "html/product_group.html",
39 "condition_field": "show_in_website"
Rushabh Mehtaa494b882012-12-07 12:44:45 +053040 })
Rushabh Mehta571377a2012-12-07 11:00:26 +053041}
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053042
Rushabh Mehta1b151712012-12-27 16:50:24 +053043page_settings_map = {
Rushabh Mehtad6164132012-12-27 18:40:42 +053044 "about": "About Us Settings",
45 "contact": "Contact Us Settings"
Rushabh Mehta1b151712012-12-27 16:50:24 +053046}
47
Rushabh Mehta89c7b412012-12-06 14:58:44 +053048def render(page_name):
49 """render html page"""
Rushabh Mehta89c7b412012-12-06 14:58:44 +053050 try:
51 if page_name:
52 html = get_html(page_name)
53 else:
54 html = get_html('index')
55 except Exception, e:
Rushabh Mehtae109fa42012-12-19 10:14:59 +053056 html = get_html('error')
Rushabh Mehta89c7b412012-12-06 14:58:44 +053057
58 from webnotes.handler import eprint, print_zip
59 eprint("Content-Type: text/html")
60 print_zip(html)
61
62def get_html(page_name):
63 """get page html"""
64 page_name = scrub_page_name(page_name)
Rushabh Mehta89c7b412012-12-06 14:58:44 +053065
Rushabh Mehta571377a2012-12-07 11:00:26 +053066 html = ''
67
68 # load from cache, if auto cache clear is falsy
69 if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0):
70 html = webnotes.cache().get_value("page:" + page_name)
Rushabh Mehtae109fa42012-12-19 10:14:59 +053071 from_cache = True
Rushabh Mehta571377a2012-12-07 11:00:26 +053072
Rushabh Mehtae109fa42012-12-19 10:14:59 +053073 if not html:
Rushabh Mehta571377a2012-12-07 11:00:26 +053074 html = load_into_cache(page_name)
Rushabh Mehtae109fa42012-12-19 10:14:59 +053075 from_cache = False
Rushabh Mehta571377a2012-12-07 11:00:26 +053076
Rushabh Mehtae109fa42012-12-19 10:14:59 +053077 if not html:
78 html = get_html("404")
79
80 if page_name=="error":
Rushabh Mehta7edf8992012-12-25 18:18:17 +053081 html = html.replace("%(error)s", webnotes.getTraceback())
Rushabh Mehtae109fa42012-12-19 10:14:59 +053082 else:
83 comments = "\n\npage:"+page_name+\
84 "\nload status: " + (from_cache and "cache" or "fresh")
85 html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
Rushabh Mehta571377a2012-12-07 11:00:26 +053086
Rushabh Mehta89c7b412012-12-06 14:58:44 +053087 return html
Rushabh Mehta89c7b412012-12-06 14:58:44 +053088
Anand Doshi51146c02012-07-12 18:41:12 +053089def scrub_page_name(page_name):
90 if page_name.endswith('.html'):
91 page_name = page_name[:-5]
92
93 return page_name
94
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053095def page_name(title):
Anand Doshi72c945b2012-06-22 20:01:07 +053096 """make page name from title"""
97 import re
98 name = title.lower()
99 name = re.sub('[~!@#$%^&*()<>,."\']', '', name)
Nabin Hait80c68122013-01-17 13:24:51 +0530100 name = re.sub('[:/]', '-', name)
101
102 name = '-'.join(name.split())
103
104 # replace repeating hyphens
105 name = re.sub(r"(-)\1+", r"\1", name)
106
107 return name
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +0530108
109def update_page_name(doc, title):
110 """set page_name and check if it is unique"""
Rushabh Mehtaa2e4cb02012-12-06 17:09:38 +0530111 webnotes.conn.set(doc, "page_name", page_name(title))
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +0530112
Rushabh Mehta1b151712012-12-27 16:50:24 +0530113 standard_pages = get_template_pages()
114 if doc.page_name in standard_pages:
115 webnotes.conn.sql("""Page Name cannot be one of %s""" % ', '.join(standard_pages))
116
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +0530117 res = webnotes.conn.sql("""\
118 select count(*) from `tab%s`
119 where page_name=%s and name!=%s""" % (doc.doctype, '%s', '%s'),
120 (doc.page_name, doc.name))
121 if res and res[0][0] > 0:
122 webnotes.msgprint("""A %s with the same title already exists.
123 Please change the title of %s and save again."""
124 % (doc.doctype, doc.name), raise_exception=1)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530125
126 delete_page_cache(doc.page_name)
127
128def load_into_cache(page_name):
129 args = prepare_args(page_name)
Rushabh Mehtae109fa42012-12-19 10:14:59 +0530130 if not args:
131 return ""
Rushabh Mehta571377a2012-12-07 11:00:26 +0530132 html = build_html(args)
133 webnotes.cache().set_value("page:" + page_name, html)
134 return html
135
136def build_html(args):
137 from jinja2 import Environment, FileSystemLoader
138
139 templates_path = os.path.join(os.path.dirname(conf.__file__),
140 'app', 'website', 'templates')
141
Rushabh Mehta7837d802012-12-25 15:09:14 +0530142 args["len"] = len
143
Rushabh Mehta571377a2012-12-07 11:00:26 +0530144 jenv = Environment(loader = FileSystemLoader(templates_path))
145 html = jenv.get_template(args['template']).render(args)
146
147 return html
148
149def prepare_args(page_name):
150 if page_name == 'index':
151 page_name = get_home_page()
152
153 if page_name in get_template_pages():
Rushabh Mehta1b151712012-12-27 16:50:24 +0530154 args = webnotes._dict({
Nabin Haitd68fa3b2013-01-01 12:05:13 +0530155 'template': 'pages/%s.html' % page_name,
Rushabh Mehta571377a2012-12-07 11:00:26 +0530156 'name': page_name,
Rushabh Mehta1b151712012-12-27 16:50:24 +0530157 })
158 if page_name in page_settings_map:
159 args.obj = webnotes.model_wrapper(page_settings_map[page_name]).obj
Rushabh Mehta571377a2012-12-07 11:00:26 +0530160 else:
161 args = get_doc_fields(page_name)
162
Rushabh Mehtae109fa42012-12-19 10:14:59 +0530163 if not args:
164 return False
165
Rushabh Mehta571377a2012-12-07 11:00:26 +0530166 args.update(get_outer_env())
167
168 return args
169
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530170def get_template_pages():
Rushabh Mehta571377a2012-12-07 11:00:26 +0530171 pages_path = os.path.join(os.path.dirname(conf.__file__), 'app',
172 'website', 'templates', 'pages')
173 page_list = []
174 for page in os.listdir(pages_path):
175 page_list.append(scrub_page_name(page))
176
177 return page_list
178
179def get_doc_fields(page_name):
180 doc_type, doc_name = get_source_doc(page_name)
Rushabh Mehtae109fa42012-12-19 10:14:59 +0530181 if not doc_type:
182 return False
183
Rushabh Mehta0e9e8482012-12-17 16:00:34 +0530184 obj = webnotes.get_obj(doc_type, doc_name, with_children=True)
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530185
Rushabh Mehta571377a2012-12-07 11:00:26 +0530186 if hasattr(obj, 'prepare_template_args'):
187 obj.prepare_template_args()
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530188
Rushabh Mehta571377a2012-12-07 11:00:26 +0530189 args = obj.doc.fields
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530190 args['template'] = page_map[doc_type].template
Rushabh Mehta0e9e8482012-12-17 16:00:34 +0530191 args['obj'] = obj
Rushabh Mehtafc19f252012-12-20 17:11:51 +0530192 args['int'] = int
Rushabh Mehta571377a2012-12-07 11:00:26 +0530193
194 return args
195
196def get_source_doc(page_name):
197 """get source doc for the given page name"""
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530198 for doctype in page_map:
Rushabh Mehta571377a2012-12-07 11:00:26 +0530199 name = webnotes.conn.sql("""select name from `tab%s` where
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530200 page_name=%s and ifnull(%s, 0)=1""" % (doctype, "%s",
201 page_map[doctype].condition_field), page_name)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530202 if name:
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530203 return doctype, name[0][0]
204
Rushabh Mehta571377a2012-12-07 11:00:26 +0530205 return None, None
206
207def get_outer_env():
208 all_top_items = webnotes.conn.sql("""\
209 select * from `tabTop Bar Item`
210 where parent='Website Settings' and parentfield='top_bar_items'
211 order by idx asc""", as_dict=1)
Rushabh Mehta60cc0cc2012-12-21 10:52:26 +0530212
Rushabh Mehta571377a2012-12-07 11:00:26 +0530213 top_items = [d for d in all_top_items if not d['parent_label']]
214
215 # attach child items to top bar
216 for d in all_top_items:
217 if d['parent_label']:
218 for t in top_items:
219 if t['label']==d['parent_label']:
220 if not 'child_items' in t:
221 t['child_items'] = []
222 t['child_items'].append(d)
223 break
224
Nabin Haitab573ce2012-12-21 11:01:31 +0530225 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 +0530226 # product categories
227 products = webnotes.conn.sql("""select t1.item_group as label,
Rushabh Mehta5f183982013-01-01 10:55:58 +0530228 t2.page_name as url,
Rushabh Mehtafc19f252012-12-20 17:11:51 +0530229 ifnull(t1.indent,0) as indent
230 from `tabWebsite Product Category` t1, `tabItem Group` t2
231 where t1.item_group = t2.name
232 and ifnull(t2.show_in_website,0)=1 order by t1.idx""", as_dict=1)
Rushabh Mehtabda54e52012-12-24 12:52:15 +0530233 products_item = filter(lambda d: d.url and d.url.split(".")[0]=="products", top_items)[0]
Rushabh Mehtafc19f252012-12-20 17:11:51 +0530234 products_item.child_items = products
235
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530236 ret = webnotes._dict({
Rushabh Mehta571377a2012-12-07 11:00:26 +0530237 'top_bar_items': top_items,
Rushabh Mehta571377a2012-12-07 11:00:26 +0530238 'footer_items': webnotes.conn.sql("""\
239 select * from `tabTop Bar Item`
240 where parent='Website Settings' and parentfield='footer_items'
241 order by idx asc""", as_dict=1),
242
Rushabh Mehtafc19f252012-12-20 17:11:51 +0530243 'int':int
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530244 })
245
246 settings = webnotes.doc("Website Settings", "Website Settings")
Rushabh Mehta5f1d57e2012-12-27 14:05:02 +0530247 for k in ["brand_html", "copyright", "address", "top_bar_background"]:
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530248 if k in settings.fields:
249 ret[k] = settings.fields[k]
250
251 if not ret.brand_html:
252 ret.brand_html = "ERPNext"
Rushabh Mehta5f1d57e2012-12-27 14:05:02 +0530253 if not ret.top_bar_background:
254 ret.top_bar_background = "Black"
Rushabh Mehta7edf8992012-12-25 18:18:17 +0530255 return ret
Rushabh Mehta571377a2012-12-07 11:00:26 +0530256
257def get_home_page():
258 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
259 if doc_name:
260 page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
261 else:
262 page_name = 'login'
263
264 return page_name
265
Rushabh Mehta98b99bd2012-12-07 12:55:06 +0530266def clear_cache(page_name=None):
Rushabh Mehta571377a2012-12-07 11:00:26 +0530267 if page_name:
268 delete_page_cache(page_name)
269 else:
Rushabh Mehta28b0b1c2013-01-22 11:04:55 +0530270 cache = webnotes.cache()
271 for p in get_all_pages():
272 cache.delete_value("page:" + p)
273
274def get_all_pages():
275 all_pages = get_template_pages()
276 all_pages += page_settings_map.keys()
277 for doctype in page_map:
278 all_pages += [p[0] for p in webnotes.conn.sql("""select distinct page_name
279 from `tab%s`""" % doctype) if p[0]]
280
281 return all_pages
282
Rushabh Mehta571377a2012-12-07 11:00:26 +0530283def delete_page_cache(page_name):
Anand Doshi65cef742012-12-30 19:58:24 +0530284 if page_name:
285 webnotes.cache().delete_value("page:" + page_name)
Rushabh Mehta5f1d57e2012-12-27 14:05:02 +0530286
287def url_for_website(url):
288 if url and not url.lower().startswith("http"):
289 return "files/" + url
290 else:
291 return url