blob: 5fcae82bc3b34affc0d39d4f0a9b312737af0450 [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 Doshi40fce892012-07-09 20:02:52 +053076 if doc_type == 'Web Page':
Anand Doshi72c945b2012-06-22 20:01:07 +053077 template = 'web_page.html'
Anand Doshie47def82012-07-09 15:56:12 +053078 else:
Anand Doshi10bcf5e2012-06-26 18:54:10 +053079 args.update({ 'insert_code': 1 })
Anand Doshi40fce892012-07-09 20:02:52 +053080 if doc_type == 'Blog':
81 template = 'blog/blog.html'
82 elif doc_type == 'Item':
83 template = 'product/product.html'
84 elif page_name == 'blog':
Anand Doshie47def82012-07-09 15:56:12 +053085 template = 'blog/blog_list.html'
86 elif page_name == 'products':
87 template = 'product/product_list.html'
88 elif page_name == 'login-page':
89 template = 'login/login.html'
Anand Doshi72c945b2012-06-22 20:01:07 +053090
91 html = build_html(args, template)
92
93 # save html in web cache
94 webnotes.conn.begin()
95 webnotes.conn.set_value('Web Cache', page_name, 'html', html)
96 webnotes.conn.commit()
97
98 return html
99
100def build_html(args, template):
101 """build html using jinja2 templates"""
102 from jinja2 import Environment, FileSystemLoader
103 jenv = Environment(loader = FileSystemLoader('../erpnext/website/templates'))
104 html = jenv.get_template(template).render(args)
105 return html
106
107def get_outer_env():
108 """env dict for outer template"""
109 import webnotes
110 return {
111 'top_bar_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
112 where parent='Website Settings' and parentfield='top_bar_items'
113 order by idx asc""", as_dict=1),
114
115 'footer_items': webnotes.conn.sql("""select * from `tabTop Bar Item`
116 where parent='Website Settings' and parentfield='footer_items'
117 order by idx asc""", as_dict=1),
118
119 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html'),
120 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
Anand Doshi40fce892012-07-09 20:02:52 +0530121 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon')
Anand Doshi72c945b2012-06-22 20:01:07 +0530122 }
123
124def get_index_page():
125 import webnotes
126 doc_type = 'Web Page'
127 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
128 page_name = webnotes.conn.get_value(doc_type, doc_name, 'page_name')
129 return page_name, doc_type, doc_name
130
131# cache management
132def clear_web_cache(doc_type, doc_name, page_name):
133 """
134 * check if a record corresponding to (type, name) exists
135 * if exists, just clear html column
136 * if does not exist, create a record for (type, name)
137 * if a record like (some other type, name) exists, raise exception that the page name is not unique
138 """
139 import webnotes
140 res = webnotes.conn.get_value('Web Cache', page_name, 'doc_type')
141 if not res:
142 import webnotes.model.doc
143 d = webnotes.model.doc.Document('Web Cache')
144 d.name = page_name
145 d.doc_type = doc_type
146 d.doc_name = doc_name
147 d.html = None
148 d.save()
149 elif res == doc_type:
150 webnotes.conn.set_value('Web Cache', page_name, 'html', None)
151 else:
152 webnotes.msgprint("""Page with name "%s" already exists as a %s.
153 Please save it with another name.""" % (page_name, res), raise_exception=1)
154
155def clear_all_web_cache():
156 import webnotes
157 webnotes.conn.sql("update `tabWeb Cache` set html = NULL")
158
159def delete_web_cache(page_name):
160 """
161 delete entry of page_name from Web Cache
162 used when:
163 * web page is deleted
164 * blog is un-published
165 """
166 import webnotes
167 webnotes.conn.sql("""\
168 delete from `tabWeb Cache`
169 where name=%s""", page_name)
170
171def build_web_cache():
172 """build web cache so that pages can load faster"""
173 pass