blob: 991a4860038a5d50719c2a9fe824db6b33aea04e [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",
36 })
Rushabh Mehta571377a2012-12-07 11:00:26 +053037}
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053038
Rushabh Mehta89c7b412012-12-06 14:58:44 +053039def render(page_name):
40 """render html page"""
Rushabh Mehta89c7b412012-12-06 14:58:44 +053041 try:
42 if page_name:
43 html = get_html(page_name)
44 else:
45 html = get_html('index')
46 except Exception, e:
47 html = get_html('404')
48
49 from webnotes.handler import eprint, print_zip
50 eprint("Content-Type: text/html")
51 print_zip(html)
52
53def get_html(page_name):
54 """get page html"""
55 page_name = scrub_page_name(page_name)
56 comments = get_comments(page_name)
57
Rushabh Mehta571377a2012-12-07 11:00:26 +053058 html = ''
59
60 # load from cache, if auto cache clear is falsy
61 if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0):
62 html = webnotes.cache().get_value("page:" + page_name)
Rushabh Mehta571377a2012-12-07 11:00:26 +053063
Rushabh Mehta7c24a422012-12-07 13:29:32 +053064 if html:
65 comments += "\nload status: cache"
66 else:
Rushabh Mehta571377a2012-12-07 11:00:26 +053067 html = load_into_cache(page_name)
Rushabh Mehtab5c39702012-12-07 12:54:08 +053068 comments += "\nload status: fresh"
Rushabh Mehta571377a2012-12-07 11:00:26 +053069
70 # insert comments
71 html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
72
Rushabh Mehta89c7b412012-12-06 14:58:44 +053073 return html
74
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +053075def get_comments(page_name):
Rushabh Mehta89c7b412012-12-06 14:58:44 +053076 if page_name == '404':
77 comments = """error: %s""" % webnotes.getTraceback()
78 else:
79 comments = """page: %s""" % page_name
80
81 return comments
82
Anand Doshi51146c02012-07-12 18:41:12 +053083def scrub_page_name(page_name):
84 if page_name.endswith('.html'):
85 page_name = page_name[:-5]
86
87 return page_name
88
Rushabh Mehtaab1148c2012-01-31 18:01:16 +053089def page_name(title):
Anand Doshi72c945b2012-06-22 20:01:07 +053090 """make page name from title"""
91 import re
92 name = title.lower()
93 name = re.sub('[~!@#$%^&*()<>,."\']', '', name)
94 return '-'.join(name.split()[:4])
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +053095
96def update_page_name(doc, title):
97 """set page_name and check if it is unique"""
Rushabh Mehtaa2e4cb02012-12-06 17:09:38 +053098 webnotes.conn.set(doc, "page_name", page_name(title))
Rushabh Mehtadf0b00a2012-12-06 16:15:38 +053099
100 res = webnotes.conn.sql("""\
101 select count(*) from `tab%s`
102 where page_name=%s and name!=%s""" % (doc.doctype, '%s', '%s'),
103 (doc.page_name, doc.name))
104 if res and res[0][0] > 0:
105 webnotes.msgprint("""A %s with the same title already exists.
106 Please change the title of %s and save again."""
107 % (doc.doctype, doc.name), raise_exception=1)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530108
109 delete_page_cache(doc.page_name)
110
111def load_into_cache(page_name):
112 args = prepare_args(page_name)
113 html = build_html(args)
114 webnotes.cache().set_value("page:" + page_name, html)
115 return html
116
117def build_html(args):
118 from jinja2 import Environment, FileSystemLoader
119
120 templates_path = os.path.join(os.path.dirname(conf.__file__),
121 'app', 'website', 'templates')
122
123 jenv = Environment(loader = FileSystemLoader(templates_path))
124 html = jenv.get_template(args['template']).render(args)
125
126 return html
127
128def prepare_args(page_name):
129 if page_name == 'index':
130 page_name = get_home_page()
131
132 if page_name in get_template_pages():
133 args = {
134 'template': 'pages/%s.html' % page_name,
135 'name': page_name,
136 }
137 else:
138 args = get_doc_fields(page_name)
139
140 args.update(get_outer_env())
141
142 return args
143
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530144def get_template_pages():
Rushabh Mehta571377a2012-12-07 11:00:26 +0530145 pages_path = os.path.join(os.path.dirname(conf.__file__), 'app',
146 'website', 'templates', 'pages')
147 page_list = []
148 for page in os.listdir(pages_path):
149 page_list.append(scrub_page_name(page))
150
151 return page_list
152
153def get_doc_fields(page_name):
154 doc_type, doc_name = get_source_doc(page_name)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530155 obj = webnotes.get_obj(doc_type, doc_name)
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530156
Rushabh Mehta571377a2012-12-07 11:00:26 +0530157 if hasattr(obj, 'prepare_template_args'):
158 obj.prepare_template_args()
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530159
Rushabh Mehta571377a2012-12-07 11:00:26 +0530160 args = obj.doc.fields
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530161 args['template'] = page_map[doc_type].template
Rushabh Mehta571377a2012-12-07 11:00:26 +0530162
163 return args
164
165def get_source_doc(page_name):
166 """get source doc for the given page name"""
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530167 for doctype in page_map:
Rushabh Mehta571377a2012-12-07 11:00:26 +0530168 name = webnotes.conn.sql("""select name from `tab%s` where
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530169 page_name=%s and ifnull(%s, 0)=1""" % (doctype, "%s",
170 page_map[doctype].condition_field), page_name)
Rushabh Mehta571377a2012-12-07 11:00:26 +0530171 if name:
Rushabh Mehtaa494b882012-12-07 12:44:45 +0530172 return doctype, name[0][0]
173
Rushabh Mehta571377a2012-12-07 11:00:26 +0530174 return None, None
175
176def get_outer_env():
177 all_top_items = webnotes.conn.sql("""\
178 select * from `tabTop Bar Item`
179 where parent='Website Settings' and parentfield='top_bar_items'
180 order by idx asc""", as_dict=1)
181
182 top_items = [d for d in all_top_items if not d['parent_label']]
183
184 # attach child items to top bar
185 for d in all_top_items:
186 if d['parent_label']:
187 for t in top_items:
188 if t['label']==d['parent_label']:
189 if not 'child_items' in t:
190 t['child_items'] = []
191 t['child_items'].append(d)
192 break
193
194 return {
195 'top_bar_items': top_items,
196
197 'footer_items': webnotes.conn.sql("""\
198 select * from `tabTop Bar Item`
199 where parent='Website Settings' and parentfield='footer_items'
200 order by idx asc""", as_dict=1),
201
202 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext',
203 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
204 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon')
205 }
206
207def get_home_page():
208 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
209 if doc_name:
210 page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
211 else:
212 page_name = 'login'
213
214 return page_name
215
Rushabh Mehta98b99bd2012-12-07 12:55:06 +0530216def clear_cache(page_name=None):
Rushabh Mehta571377a2012-12-07 11:00:26 +0530217 if page_name:
218 delete_page_cache(page_name)
219 else:
220 webnotes.cache().delete_keys("page:")
221
222def delete_page_cache(page_name):
223 webnotes.cache().delete_value("page:" + page_name)