blob: 42bd1d111119a88b4c0ab29c5c87667de2a4e223 [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
17# used by web.py
Anand Doshibba4db12012-06-27 12:06:45 +053018def load_from_web_cache(page_name, comments, template): #, script=None):
Anand Doshi72c945b2012-06-22 20:01:07 +053019 """
20 * search for page in cache
21 * if html exists, return
22 * if not, build html, store it in cache, return
23 """
24 import webnotes
25 import conf
26
27 if page_name == 'index':
28 page_name = get_index_page()[0]
29
30 res = webnotes.conn.sql("""\
31 select html, doc_type, doc_name from `tabWeb Cache`
32 where name = %s""", page_name)
33
34 # if page doesn't exist, raise exception
Anand Doshi10bcf5e2012-06-26 18:54:10 +053035 if not res and page_name not in ['404', 'index', 'blog', 'products']:
Anand Doshi72c945b2012-06-22 20:01:07 +053036 raise Exception, "Page %s not found" % page_name
37
38 html, doc_type, doc_name = res and res[0] or (None, None, None)
39 auto_cache_clear = hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0
40 if not html or auto_cache_clear:
41 comments += "\n\npage load status: fresh"
42 html = load_into_web_cache(page_name, template, doc_type, doc_name)
43 else:
44 comments += "\n\npage load status: cached"
45
46 from webnotes.utils import cstr
47 html += """\n<!-- %s -->""" % cstr(comments)
Anand Doshibba4db12012-06-27 12:06:45 +053048
49 # show error in error console
50 # if script: html += """\n\n<script>\n%s\n</script>""" % cstr(script)
Anand Doshi72c945b2012-06-22 20:01:07 +053051 return html
52
53def load_into_web_cache(page_name, template, doc_type, doc_name):
54 """build html and store it in web cache"""
55 import webnotes
56 outer_env_dict = get_outer_env()
57
Anand Doshi10bcf5e2012-06-26 18:54:10 +053058 if page_name in ['404', 'blog', 'products']:
Anand Doshi72c945b2012-06-22 20:01:07 +053059 args = outer_env_dict
Anand Doshi10bcf5e2012-06-26 18:54:10 +053060 args.update({
61 'name': page_name,
62 })
Anand Doshi72c945b2012-06-22 20:01:07 +053063 else:
64 if page_name == 'index':
65 page_name, doc_type, doc_name = get_index_page()
66
67 from webnotes.model.code import get_obj
68 obj = get_obj(doc_type, doc_name)
Anand Doshi10bcf5e2012-06-26 18:54:10 +053069 if hasattr(obj, 'get_html'):
70 obj.get_html()
Anand Doshi72c945b2012-06-22 20:01:07 +053071 args = obj.doc.fields
72 args.update(outer_env_dict)
73
Anand Doshi10bcf5e2012-06-26 18:54:10 +053074 # decide template and update args
Anand Doshi72c945b2012-06-22 20:01:07 +053075 if doc_type == 'Blog':
Anand Doshi10bcf5e2012-06-26 18:54:10 +053076 template = 'blog/blog.html'
77 args.update({ 'insert_code': 1 })
78 elif doc_type == 'Item':
79 template = 'product/product.html'
80 args.update({ 'insert_code': 1 })
Anand Doshi72c945b2012-06-22 20:01:07 +053081 elif doc_type == 'Web Page':
82 template = 'web_page.html'
Anand Doshi10bcf5e2012-06-26 18:54:10 +053083 elif page_name == 'blog':
84 template = 'blog/blog_list.html'
85 args.update({ 'insert_code': 1 })
86 elif page_name == 'products':
87 template = 'product/product_list.html'
88 args.update({ 'insert_code': 1 })
Anand Doshi72c945b2012-06-22 20:01:07 +053089
90 html = build_html(args, template)
91
92 # save html in web cache
93 webnotes.conn.begin()
94 webnotes.conn.set_value('Web Cache', page_name, 'html', html)
95 webnotes.conn.commit()
96
97 return html
98
99def build_html(args, template):
100 """build html using jinja2 templates"""
101 from jinja2 import Environment, FileSystemLoader
102 jenv = Environment(loader = FileSystemLoader('../erpnext/website/templates'))
103 html = jenv.get_template(template).render(args)
104 return html
105
106def get_outer_env():
107 """env dict for outer template"""
108 import webnotes
109 return {
110 'top_bar_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
111 where parent='Website Settings' and parentfield='top_bar_items'
112 order by idx asc""", as_dict=1),
113
114 'footer_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
115 where parent='Website Settings' and parentfield='footer_items'
116 order by idx asc""", as_dict=1),
117
118 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html'),
119 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
120 }
121
122def get_index_page():
123 import webnotes
124 doc_type = 'Web Page'
125 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
126 page_name = webnotes.conn.get_value(doc_type, doc_name, 'page_name')
127 return page_name, doc_type, doc_name
128
129# cache management
130def clear_web_cache(doc_type, doc_name, page_name):
131 """
132 * check if a record corresponding to (type, name) exists
133 * if exists, just clear html column
134 * if does not exist, create a record for (type, name)
135 * if a record like (some other type, name) exists, raise exception that the page name is not unique
136 """
137 import webnotes
138 res = webnotes.conn.get_value('Web Cache', page_name, 'doc_type')
139 if not res:
140 import webnotes.model.doc
141 d = webnotes.model.doc.Document('Web Cache')
142 d.name = page_name
143 d.doc_type = doc_type
144 d.doc_name = doc_name
145 d.html = None
146 d.save()
147 elif res == doc_type:
148 webnotes.conn.set_value('Web Cache', page_name, 'html', None)
149 else:
150 webnotes.msgprint("""Page with name "%s" already exists as a %s.
151 Please save it with another name.""" % (page_name, res), raise_exception=1)
152
153def clear_all_web_cache():
154 import webnotes
155 webnotes.conn.sql("update `tabWeb Cache` set html = NULL")
156
157def delete_web_cache(page_name):
158 """
159 delete entry of page_name from Web Cache
160 used when:
161 * web page is deleted
162 * blog is un-published
163 """
164 import webnotes
165 webnotes.conn.sql("""\
166 delete from `tabWeb Cache`
167 where name=%s""", page_name)
168
169def build_web_cache():
170 """build web cache so that pages can load faster"""
171 pass