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