blob: 0fc5f984e14945fffda1169c8a9893371a26aabd [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 Mehta89c7b412012-12-06 14:58:44 +053043def render(page_name):
44 """render html page"""
Rushabh Mehta89c7b412012-12-06 14:58:44 +053045 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
57def get_html(page_name):
58 """get page html"""
59 page_name = scrub_page_name(page_name)
60 comments = get_comments(page_name)
61
Rushabh Mehta571377a2012-12-07 11:00:26 +053062 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 Mehta571377a2012-12-07 11:00:26 +053067
Rushabh Mehta7c24a422012-12-07 13:29:32 +053068 if html:
69 comments += "\nload status: cache"
70 else:
Rushabh Mehta571377a2012-12-07 11:00:26 +053071 html = load_into_cache(page_name)
Rushabh Mehtab5c39702012-12-07 12:54:08 +053072 comments += "\nload status: fresh"
Rushabh Mehta571377a2012-12-07 11:00:26 +053073
74 # insert comments
75 html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
76
Rushabh Mehta89c7b412012-12-06 14:58:44 +053077 return html
78
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +053079def get_comments(page_name):
Rushabh Mehta89c7b412012-12-06 14:58:44 +053080 if page_name == '404':
81 comments = """error: %s""" % webnotes.getTraceback()
82 else:
83 comments = """page: %s""" % page_name
84
85 return comments
86
Anand Doshi51146c02012-07-12 18:41:12 +053087def scrub_page_name(page_name):
88 if page_name.endswith('.html'):
89 page_name = page_name[:-5]
90
91 return page_name
92
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053093def page_name(title):
Anand Doshi72c945b2012-06-22 20:01:07 +053094 """make page name from title"""
95 import re
96 name = title.lower()
97 name = re.sub('[~!@#$%^&*()<>,."\']', '', name)
98 return '-'.join(name.split()[:4])
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +053099
100def update_page_name(doc, title):
101 """set page_name and check if it is unique"""
Rushabh Mehtaa2e4cb02012-12-06 17:09:38 +0530102 webnotes.conn.set(doc, "page_name", page_name(title))
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +0530103
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 Mehta571377a2012-12-07 11:00:26 +0530112
113 delete_page_cache(doc.page_name)
114
115def 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
121def 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
132def 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 Mehtaa494b882012-12-07 12:44:45 +0530148def get_template_pages():
Rushabh Mehta571377a2012-12-07 11:00:26 +0530149 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
157def get_doc_fields(page_name):
158 doc_type, doc_name = get_source_doc(page_name)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530159 obj = webnotes.get_obj(doc_type, doc_name)
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530160
Rushabh Mehta571377a2012-12-07 11:00:26 +0530161 if hasattr(obj, 'prepare_template_args'):
162 obj.prepare_template_args()
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530163
Rushabh Mehta571377a2012-12-07 11:00:26 +0530164 args = obj.doc.fields
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530165 args['template'] = page_map[doc_type].template
Rushabh Mehta571377a2012-12-07 11:00:26 +0530166
167 return args
168
169def get_source_doc(page_name):
170 """get source doc for the given page name"""
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530171 for doctype in page_map:
Rushabh Mehta571377a2012-12-07 11:00:26 +0530172 name = webnotes.conn.sql("""select name from `tab%s` where
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530173 page_name=%s and ifnull(%s, 0)=1""" % (doctype, "%s",
174 page_map[doctype].condition_field), page_name)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530175 if name:
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530176 return doctype, name[0][0]
177
Rushabh Mehta571377a2012-12-07 11:00:26 +0530178 return None, None
179
180def get_outer_env():
181 all_top_items = webnotes.conn.sql("""\
182 select * from `tabTop Bar Item`
183 where parent='Website Settings' and parentfield='top_bar_items'
184 order by idx asc""", as_dict=1)
185
186 top_items = [d for d in all_top_items if not d['parent_label']]
187
188 # attach child items to top bar
189 for d in all_top_items:
190 if d['parent_label']:
191 for t in top_items:
192 if t['label']==d['parent_label']:
193 if not 'child_items' in t:
194 t['child_items'] = []
195 t['child_items'].append(d)
196 break
197
198 return {
199 'top_bar_items': top_items,
200
201 'footer_items': webnotes.conn.sql("""\
202 select * from `tabTop Bar Item`
203 where parent='Website Settings' and parentfield='footer_items'
204 order by idx asc""", as_dict=1),
205
206 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext',
207 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
208 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon')
209 }
210
211def get_home_page():
212 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
213 if doc_name:
214 page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
215 else:
216 page_name = 'login'
217
218 return page_name
219
Rushabh Mehta98b99bd2012-12-07 12:55:06 +0530220def clear_cache(page_name=None):
Rushabh Mehta571377a2012-12-07 11:00:26 +0530221 if page_name:
222 delete_page_cache(page_name)
223 else:
224 webnotes.cache().delete_keys("page:")
225
226def delete_page_cache(page_name):
227 webnotes.cache().delete_value("page:" + page_name)