blob: 0221f47f33a00233a077ddcbfe50af3f6a5ace8b [file] [log] [blame]
Anand Doshi72c945b2012-06-22 20:01:07 +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 Doshi87901882012-07-11 20:04:32 +053017page_exception_list = ['404', 'blog', 'products', 'login-page']
18
Anand Doshi72c945b2012-06-22 20:01:07 +053019# used by web.py
Anand Doshi8c7e76b2012-07-11 18:40:57 +053020def load_from_web_cache(page_name, comments, template):
Anand Doshi72c945b2012-06-22 20:01:07 +053021 """
22 * search for page in cache
23 * if html exists, return
24 * if not, build html, store it in cache, return
25 """
26 import webnotes
27 import conf
Anand Doshi72c945b2012-06-22 20:01:07 +053028
29 res = webnotes.conn.sql("""\
30 select html, doc_type, doc_name from `tabWeb Cache`
31 where name = %s""", page_name)
32
33 # if page doesn't exist, raise exception
Anand Doshi87901882012-07-11 20:04:32 +053034 if not res and page_name not in page_exception_list + ['index']:
Anand Doshi72c945b2012-06-22 20:01:07 +053035 raise Exception, "Page %s not found" % page_name
36
37 html, doc_type, doc_name = res and res[0] or (None, None, None)
38 auto_cache_clear = hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0
39 if not html or auto_cache_clear:
40 comments += "\n\npage load status: fresh"
41 html = load_into_web_cache(page_name, template, doc_type, doc_name)
42 else:
43 comments += "\n\npage load status: cached"
44
45 from webnotes.utils import cstr
46 html += """\n<!-- %s -->""" % cstr(comments)
Anand Doshibba4db12012-06-27 12:06:45 +053047
Anand Doshi72c945b2012-06-22 20:01:07 +053048 return html
49
50def load_into_web_cache(page_name, template, doc_type, doc_name):
51 """build html and store it in web cache"""
52 import webnotes
Anand Doshi72c945b2012-06-22 20:01:07 +053053
Anand Doshi8c7e76b2012-07-11 18:40:57 +053054 args = prepare_args(page_name, doc_type, doc_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053055
Anand Doshi10bcf5e2012-06-26 18:54:10 +053056 # decide template and update args
Anand Doshi40fce892012-07-09 20:02:52 +053057 if doc_type == 'Web Page':
Anand Doshi72c945b2012-06-22 20:01:07 +053058 template = 'web_page.html'
Anand Doshie47def82012-07-09 15:56:12 +053059 else:
Anand Doshi10bcf5e2012-06-26 18:54:10 +053060 args.update({ 'insert_code': 1 })
Anand Doshi40fce892012-07-09 20:02:52 +053061 if doc_type == 'Blog':
62 template = 'blog/blog.html'
63 elif doc_type == 'Item':
64 template = 'product/product.html'
65 elif page_name == 'blog':
Anand Doshie47def82012-07-09 15:56:12 +053066 template = 'blog/blog_list.html'
67 elif page_name == 'products':
68 template = 'product/product_list.html'
69 elif page_name == 'login-page':
70 template = 'login/login.html'
Anand Doshi72c945b2012-06-22 20:01:07 +053071
72 html = build_html(args, template)
73
74 # save html in web cache
75 webnotes.conn.begin()
Anand Doshi87901882012-07-11 20:04:32 +053076 if page_name in page_exception_list + ['index']:
77 clear_web_cache(doc_type, doc_name, page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053078 webnotes.conn.set_value('Web Cache', page_name, 'html', html)
79 webnotes.conn.commit()
80
81 return html
Anand Doshi8c7e76b2012-07-11 18:40:57 +053082
83def prepare_args(page_name, doc_type, doc_name, with_outer_env=1):
Anand Doshi51fc20b2012-07-11 18:56:13 +053084 if page_name == 'index':
85 page_name, doc_type, doc_name = get_index_page()
86
Anand Doshi87901882012-07-11 20:04:32 +053087 if page_name in page_exception_list:
Anand Doshi8c7e76b2012-07-11 18:40:57 +053088 args = {
89 'name': page_name,
90 }
91 else:
Anand Doshi8c7e76b2012-07-11 18:40:57 +053092 from webnotes.model.code import get_obj
93 obj = get_obj(doc_type, doc_name)
94 if hasattr(obj, 'prepare_template_args'):
95 obj.prepare_template_args()
96 args = obj.doc.fields
97
98 outer_env_dict = with_outer_env and get_outer_env() or {}
99 args.update(outer_env_dict)
100
101 return args
Anand Doshi72c945b2012-06-22 20:01:07 +0530102
103def build_html(args, template):
104 """build html using jinja2 templates"""
105 from jinja2 import Environment, FileSystemLoader
106 jenv = Environment(loader = FileSystemLoader('../erpnext/website/templates'))
107 html = jenv.get_template(template).render(args)
108 return html
109
110def get_outer_env():
111 """env dict for outer template"""
112 import webnotes
113 return {
114 'top_bar_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
115 where parent='Website Settings' and parentfield='top_bar_items'
116 order by idx asc""", as_dict=1),
117
118 'footer_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
119 where parent='Website Settings' and parentfield='footer_items'
120 order by idx asc""", as_dict=1),
121
Anand Doshif7765a82012-07-11 19:05:27 +0530122 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext',
Anand Doshi72c945b2012-06-22 20:01:07 +0530123 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
Anand Doshi40fce892012-07-09 20:02:52 +0530124 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon')
Anand Doshi72c945b2012-06-22 20:01:07 +0530125 }
126
127def get_index_page():
128 import webnotes
129 doc_type = 'Web Page'
130 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
131 page_name = webnotes.conn.get_value(doc_type, doc_name, 'page_name')
Anand Doshi51fc20b2012-07-11 18:56:13 +0530132 if not page_name:
133 page_name = 'login-page'
Anand Doshi72c945b2012-06-22 20:01:07 +0530134 return page_name, doc_type, doc_name
135
136# cache management
137def clear_web_cache(doc_type, doc_name, page_name):
138 """
139 * check if a record corresponding to (type, name) exists
140 * if exists, just clear html column
141 * if does not exist, create a record for (type, name)
142 * if a record like (some other type, name) exists, raise exception that the page name is not unique
143 """
144 import webnotes
Anand Doshi87901882012-07-11 20:04:32 +0530145 res = webnotes.conn.get_value('Web Cache', page_name, ['doc_type', 'name'])
Anand Doshi72c945b2012-06-22 20:01:07 +0530146 if not res:
147 import webnotes.model.doc
148 d = webnotes.model.doc.Document('Web Cache')
149 d.name = page_name
150 d.doc_type = doc_type
151 d.doc_name = doc_name
152 d.html = None
153 d.save()
Anand Doshi87901882012-07-11 20:04:32 +0530154 elif not doc_type or res[0] == doc_type:
Anand Doshi72c945b2012-06-22 20:01:07 +0530155 webnotes.conn.set_value('Web Cache', page_name, 'html', None)
156 else:
157 webnotes.msgprint("""Page with name "%s" already exists as a %s.
Anand Doshi87901882012-07-11 20:04:32 +0530158 Please save it with another name.""" % (page_name, res[0]), raise_exception=1)
Anand Doshi72c945b2012-06-22 20:01:07 +0530159
160def clear_all_web_cache():
161 import webnotes
162 webnotes.conn.sql("update `tabWeb Cache` set html = NULL")
163
164def delete_web_cache(page_name):
165 """
166 delete entry of page_name from Web Cache
167 used when:
168 * web page is deleted
169 * blog is un-published
170 """
171 import webnotes
172 webnotes.conn.sql("""\
173 delete from `tabWeb Cache`
174 where name=%s""", page_name)
175
Anand Doshi87901882012-07-11 20:04:32 +0530176def rebuild_web_cache():
177 """build web cache entries"""
178 import webnotes
179 from webnotes.model.doclist import DocList
180 save_list = [
181 {
182 'doctype': 'Web Page',
183 'query': """select name from `tabWeb Page` where docstatus=0"""
184 },
185 {
186 'doctype': 'Blog',
187 'query': """\
188 select name from `tabBlog`
189 where docstatus = 0 and ifnull(published, 0) = 1"""
190 },
191 {
192 'doctype': 'Item',
193 'query': """\
194 select name from `tabItem`
195 where docstatus = 0 and ifnull(show_in_website, 0) = 1"""
196 }
197 ]
198
199 for s in save_list:
200 for p in webnotes.conn.sql(s['query'], as_dict=1):
201 DocList(s['doctype'], p['name']).save()