blob: b0b5a34d6f536bd690ca81545eb27304e844b0fc [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 Doshi51146c02012-07-12 18:41:12 +053017# html generation functions
Anand Doshi87901882012-07-11 20:04:32 +053018
Anand Doshi486f9df2012-07-19 13:40:31 +053019from __future__ import unicode_literals
Anand Doshi51146c02012-07-12 18:41:12 +053020template_map = {
21 'Web Page': 'html/web_page.html',
22 'Blog': 'html/blog_page.html',
23 'Item': 'html/product_page.html',
24}
25
26def get_html(page_name, comments=''):
Anand Doshi72c945b2012-06-22 20:01:07 +053027 import conf
Anand Doshi51146c02012-07-12 18:41:12 +053028
29 html = ''
30
31 # load from cache, if auto cache clear is falsy
32 if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0):
33 html = load_from_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053034
Anand Doshi51146c02012-07-12 18:41:12 +053035 if not html:
36 html = load_into_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053037 comments += "\n\npage load status: fresh"
Anand Doshi51146c02012-07-12 18:41:12 +053038
39 # insert comments
40 import webnotes.utils
41 html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
Anand Doshibba4db12012-06-27 12:06:45 +053042
Anand Doshi72c945b2012-06-22 20:01:07 +053043 return html
44
Anand Doshi51146c02012-07-12 18:41:12 +053045def load_from_cache(page_name):
Anand Doshi72c945b2012-06-22 20:01:07 +053046 import webnotes
Anand Doshi51146c02012-07-12 18:41:12 +053047
48 result = search_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053049
Anand Doshi51146c02012-07-12 18:41:12 +053050 if not result:
51 if page_name in get_predefined_pages():
52 # if a predefined page doesn't exist, load it into cache
53 return None
54 else:
55 # if page doesn't exist, raise exception
56 raise Exception, "Page %s not found" % page_name
57
58 return result[0][0]
59
60def load_into_cache(page_name):
61 args = prepare_args(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053062
Anand Doshi51146c02012-07-12 18:41:12 +053063 html = build_html(args)
Anand Doshi72c945b2012-06-22 20:01:07 +053064
Anand Doshi51146c02012-07-12 18:41:12 +053065 # create cache entry for predefined pages, if not exists
66 if page_name in get_predefined_pages():
67 create_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053068
Anand Doshi51146c02012-07-12 18:41:12 +053069 import webnotes
Anand Doshi72c945b2012-06-22 20:01:07 +053070 webnotes.conn.begin()
71 webnotes.conn.set_value('Web Cache', page_name, 'html', html)
72 webnotes.conn.commit()
73
74 return html
Anand Doshi51fc20b2012-07-11 18:56:13 +053075
Anand Doshi51146c02012-07-12 18:41:12 +053076def get_predefined_pages():
77 """
78 gets a list of predefined pages
79 they do not exist in `tabWeb Page`
80 """
81 import os
82 import conf
83 import website.utils
84
85 pages_path = os.path.join(conf.modules_path, 'website', 'templates', 'pages')
86
87 page_list = []
88
89 for page in os.listdir(pages_path):
90 page_list.append(website.utils.scrub_page_name(page))
91
92 return page_list
93
94def prepare_args(page_name):
95 if page_name == 'index':
96 page_name = get_home_page()
97
98 if page_name in get_predefined_pages():
Anand Doshi8c7e76b2012-07-11 18:40:57 +053099 args = {
Anand Doshi51146c02012-07-12 18:41:12 +0530100 'template': 'pages/%s.html' % page_name,
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530101 'name': page_name,
102 }
103 else:
Anand Doshi51146c02012-07-12 18:41:12 +0530104 args = get_doc_fields(page_name)
105
106 args.update(get_outer_env())
107
108 return args
109
110def get_home_page():
111 import webnotes
112 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
113 if doc_name:
114 page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
115 else:
116 page_name = 'login'
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530117
Anand Doshi51146c02012-07-12 18:41:12 +0530118 return page_name
119
120def get_doc_fields(page_name):
121 import webnotes
122 doc_type, doc_name = webnotes.conn.get_value('Web Cache', page_name, ['doc_type', 'doc_name'])
123
124 import webnotes.model.code
125 obj = webnotes.model.code.get_obj(doc_type, doc_name)
126
127 if hasattr(obj, 'prepare_template_args'):
128 obj.prepare_template_args()
129
130 args = obj.doc.fields
131 args['template'] = template_map[doc_type]
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530132
133 return args
Anand Doshi72c945b2012-06-22 20:01:07 +0530134
Anand Doshi72c945b2012-06-22 20:01:07 +0530135def get_outer_env():
Anand Doshi51146c02012-07-12 18:41:12 +0530136 """
137 env dict for outer template
138 """
Anand Doshi72c945b2012-06-22 20:01:07 +0530139 import webnotes
140 return {
Anand Doshi51146c02012-07-12 18:41:12 +0530141 'top_bar_items': webnotes.conn.sql("""\
142 select * from `tabTop Bar Item`
Anand Doshi72c945b2012-06-22 20:01:07 +0530143 where parent='Website Settings' and parentfield='top_bar_items'
144 order by idx asc""", as_dict=1),
145
Anand Doshi51146c02012-07-12 18:41:12 +0530146 'footer_items': webnotes.conn.sql("""\
147 select * from `tabTop Bar Item`
Anand Doshi72c945b2012-06-22 20:01:07 +0530148 where parent='Website Settings' and parentfield='footer_items'
149 order by idx asc""", as_dict=1),
150
Anand Doshif7765a82012-07-11 19:05:27 +0530151 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext',
Anand Doshi72c945b2012-06-22 20:01:07 +0530152 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
Anand Doshi40fce892012-07-09 20:02:52 +0530153 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon')
Anand Doshi72c945b2012-06-22 20:01:07 +0530154 }
155
Anand Doshi51146c02012-07-12 18:41:12 +0530156def build_html(args):
157 """
158 build html using jinja2 templates
159 """
160 import os
161 import conf
162 templates_path = os.path.join(conf.modules_path, 'website', 'templates')
163
164 from jinja2 import Environment, FileSystemLoader
165 jenv = Environment(loader = FileSystemLoader(templates_path))
166 html = jenv.get_template(args['template']).render(args)
167 return html
Anand Doshi72c945b2012-06-22 20:01:07 +0530168
169# cache management
Anand Doshi51146c02012-07-12 18:41:12 +0530170def search_cache(page_name):
Anand Doshi1a51cc92012-07-12 19:03:58 +0530171 if not page_name: return ()
Anand Doshi72c945b2012-06-22 20:01:07 +0530172 import webnotes
Anand Doshi51146c02012-07-12 18:41:12 +0530173 return webnotes.conn.sql("""\
174 select html, doc_type, doc_name
175 from `tabWeb Cache`
176 where name = %s""", page_name)
177
178def create_cache(page_name, doc_type=None, doc_name=None):
179 # check if a record already exists
180 result = search_cache(page_name)
181 if result: return
182
183 # create a Web Cache record
184 import webnotes.model.doc
185 d = webnotes.model.doc.Document('Web Cache')
186 d.name = page_name
187 d.doc_type = doc_type
188 d.doc_name = doc_name
189 d.html = None
190 d.save()
191
192def clear_cache(page_name, doc_type=None, doc_name=None):
193 """
194 * if no page name, clear whole cache
195 * if page_name, doc_type and doc_name match, clear cache's copy
196 * else, raise exception that such a page already exists
197 """
Anand Doshi1a51cc92012-07-12 19:03:58 +0530198 import webnotes
199
Anand Doshi51146c02012-07-12 18:41:12 +0530200 if not page_name:
201 webnotes.conn.sql("""update `tabWeb Cache` set html = ''""")
202 return
203
204 result = search_cache(page_name)
Anand Doshi1a51cc92012-07-12 19:03:58 +0530205
Anand Doshi51146c02012-07-12 18:41:12 +0530206 if not doc_type or (result and result[0][1] == doc_type and result[0][2] == doc_name):
207 webnotes.conn.set_value('Web Cache', page_name, 'html', '')
Anand Doshi72c945b2012-06-22 20:01:07 +0530208 else:
209 webnotes.msgprint("""Page with name "%s" already exists as a %s.
Anand Doshi51146c02012-07-12 18:41:12 +0530210 Please save it with another name.""" % (page_name, result[0][1]),
211 raise_exception=1)
212
213def delete_cache(page_name):
Anand Doshi72c945b2012-06-22 20:01:07 +0530214 """
215 delete entry of page_name from Web Cache
216 used when:
217 * web page is deleted
218 * blog is un-published
219 """
220 import webnotes
Anand Doshi51146c02012-07-12 18:41:12 +0530221 webnotes.conn.sql("""delete from `tabWeb Cache` where name=%s""", page_name)
222
Anand Doshi53bd49d2012-07-13 01:45:19 +0530223def refresh_cache(build=None):
Anand Doshi51146c02012-07-12 18:41:12 +0530224 """delete and re-create web cache entries"""
Anand Doshi87901882012-07-11 20:04:32 +0530225 import webnotes
Anand Doshi87901882012-07-11 20:04:32 +0530226
Anand Doshi72a824f2012-07-13 08:14:32 +0530227 # webnotes.conn.sql("delete from `tabWeb Cache`")
Anand Doshi51146c02012-07-12 18:41:12 +0530228
Anand Doshi255d58b2012-07-13 08:53:07 +0530229 clear_cache(None)
230
Anand Doshi51146c02012-07-12 18:41:12 +0530231 query_map = {
232 'Web Page': """select page_name, name from `tabWeb Page` where docstatus=0""",
233 'Blog': """\
234 select page_name, name from `tabBlog`
235 where docstatus = 0 and ifnull(published, 0) = 1""",
236 'Item': """\
237 select page_name, name from `tabItem`
238 where docstatus = 0 and ifnull(show_in_website, 0) = 1""",
239 }
240
241 for dt in query_map:
Anand Doshi255d58b2012-07-13 08:53:07 +0530242 if build and dt in build:
243 for result in webnotes.conn.sql(query_map[dt], as_dict=1):
244 create_cache(result['page_name'], dt, result['name'])
245 load_into_cache(result['page_name'])
Anand Doshi51146c02012-07-12 18:41:12 +0530246
247 for page_name in get_predefined_pages():
248 create_cache(page_name, None, None)
Anand Doshi72a824f2012-07-13 08:14:32 +0530249 if build: load_into_cache(page_name)