blob: 7b3de0aaef6ffbb23b03e0d32fc05fa4f11215c8 [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):
Rushabh Mehtaa4fe7182012-08-02 13:07:23 +053095 import webnotes
Anand Doshi51146c02012-07-12 18:41:12 +053096 if page_name == 'index':
97 page_name = get_home_page()
98
99 if page_name in get_predefined_pages():
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530100 args = {
Anand Doshi51146c02012-07-12 18:41:12 +0530101 'template': 'pages/%s.html' % page_name,
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530102 'name': page_name,
Rushabh Mehtaa4fe7182012-08-02 13:07:23 +0530103 'webnotes': webnotes
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530104 }
105 else:
Anand Doshi51146c02012-07-12 18:41:12 +0530106 args = get_doc_fields(page_name)
107
108 args.update(get_outer_env())
109
110 return args
111
112def get_home_page():
113 import webnotes
114 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
115 if doc_name:
116 page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
117 else:
118 page_name = 'login'
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530119
Anand Doshi51146c02012-07-12 18:41:12 +0530120 return page_name
121
122def get_doc_fields(page_name):
123 import webnotes
124 doc_type, doc_name = webnotes.conn.get_value('Web Cache', page_name, ['doc_type', 'doc_name'])
125
126 import webnotes.model.code
127 obj = webnotes.model.code.get_obj(doc_type, doc_name)
128
129 if hasattr(obj, 'prepare_template_args'):
130 obj.prepare_template_args()
131
132 args = obj.doc.fields
133 args['template'] = template_map[doc_type]
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530134
135 return args
Anand Doshi72c945b2012-06-22 20:01:07 +0530136
Anand Doshi72c945b2012-06-22 20:01:07 +0530137def get_outer_env():
Anand Doshi51146c02012-07-12 18:41:12 +0530138 """
139 env dict for outer template
140 """
Anand Doshi72c945b2012-06-22 20:01:07 +0530141 import webnotes
142 return {
Anand Doshi51146c02012-07-12 18:41:12 +0530143 'top_bar_items': webnotes.conn.sql("""\
144 select * from `tabTop Bar Item`
Anand Doshi72c945b2012-06-22 20:01:07 +0530145 where parent='Website Settings' and parentfield='top_bar_items'
146 order by idx asc""", as_dict=1),
147
Anand Doshi51146c02012-07-12 18:41:12 +0530148 'footer_items': webnotes.conn.sql("""\
149 select * from `tabTop Bar Item`
Anand Doshi72c945b2012-06-22 20:01:07 +0530150 where parent='Website Settings' and parentfield='footer_items'
151 order by idx asc""", as_dict=1),
152
Anand Doshif7765a82012-07-11 19:05:27 +0530153 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext',
Anand Doshi72c945b2012-06-22 20:01:07 +0530154 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
Anand Doshi40fce892012-07-09 20:02:52 +0530155 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon')
Anand Doshi72c945b2012-06-22 20:01:07 +0530156 }
157
Anand Doshi51146c02012-07-12 18:41:12 +0530158def build_html(args):
159 """
160 build html using jinja2 templates
161 """
162 import os
163 import conf
164 templates_path = os.path.join(conf.modules_path, 'website', 'templates')
165
166 from jinja2 import Environment, FileSystemLoader
167 jenv = Environment(loader = FileSystemLoader(templates_path))
168 html = jenv.get_template(args['template']).render(args)
169 return html
Anand Doshi72c945b2012-06-22 20:01:07 +0530170
171# cache management
Anand Doshi51146c02012-07-12 18:41:12 +0530172def search_cache(page_name):
Anand Doshi1a51cc92012-07-12 19:03:58 +0530173 if not page_name: return ()
Anand Doshi72c945b2012-06-22 20:01:07 +0530174 import webnotes
Anand Doshi51146c02012-07-12 18:41:12 +0530175 return webnotes.conn.sql("""\
176 select html, doc_type, doc_name
177 from `tabWeb Cache`
178 where name = %s""", page_name)
179
180def create_cache(page_name, doc_type=None, doc_name=None):
181 # check if a record already exists
182 result = search_cache(page_name)
183 if result: return
184
185 # create a Web Cache record
186 import webnotes.model.doc
187 d = webnotes.model.doc.Document('Web Cache')
188 d.name = page_name
189 d.doc_type = doc_type
190 d.doc_name = doc_name
191 d.html = None
192 d.save()
193
194def clear_cache(page_name, doc_type=None, doc_name=None):
195 """
196 * if no page name, clear whole cache
197 * if page_name, doc_type and doc_name match, clear cache's copy
198 * else, raise exception that such a page already exists
199 """
Anand Doshi1a51cc92012-07-12 19:03:58 +0530200 import webnotes
201
Anand Doshi51146c02012-07-12 18:41:12 +0530202 if not page_name:
203 webnotes.conn.sql("""update `tabWeb Cache` set html = ''""")
204 return
205
206 result = search_cache(page_name)
Anand Doshi1a51cc92012-07-12 19:03:58 +0530207
Anand Doshi51146c02012-07-12 18:41:12 +0530208 if not doc_type or (result and result[0][1] == doc_type and result[0][2] == doc_name):
209 webnotes.conn.set_value('Web Cache', page_name, 'html', '')
Anand Doshi72c945b2012-06-22 20:01:07 +0530210 else:
211 webnotes.msgprint("""Page with name "%s" already exists as a %s.
Anand Doshi51146c02012-07-12 18:41:12 +0530212 Please save it with another name.""" % (page_name, result[0][1]),
213 raise_exception=1)
214
215def delete_cache(page_name):
Anand Doshi72c945b2012-06-22 20:01:07 +0530216 """
217 delete entry of page_name from Web Cache
218 used when:
219 * web page is deleted
220 * blog is un-published
221 """
222 import webnotes
Anand Doshi51146c02012-07-12 18:41:12 +0530223 webnotes.conn.sql("""delete from `tabWeb Cache` where name=%s""", page_name)
224
Anand Doshi53bd49d2012-07-13 01:45:19 +0530225def refresh_cache(build=None):
Anand Doshi51146c02012-07-12 18:41:12 +0530226 """delete and re-create web cache entries"""
Anand Doshi87901882012-07-11 20:04:32 +0530227 import webnotes
Anand Doshi87901882012-07-11 20:04:32 +0530228
Anand Doshi72a824f2012-07-13 08:14:32 +0530229 # webnotes.conn.sql("delete from `tabWeb Cache`")
Anand Doshi51146c02012-07-12 18:41:12 +0530230
Anand Doshi255d58b2012-07-13 08:53:07 +0530231 clear_cache(None)
232
Anand Doshi51146c02012-07-12 18:41:12 +0530233 query_map = {
234 'Web Page': """select page_name, name from `tabWeb Page` where docstatus=0""",
235 'Blog': """\
236 select page_name, name from `tabBlog`
237 where docstatus = 0 and ifnull(published, 0) = 1""",
238 'Item': """\
239 select page_name, name from `tabItem`
240 where docstatus = 0 and ifnull(show_in_website, 0) = 1""",
241 }
242
243 for dt in query_map:
Anand Doshi255d58b2012-07-13 08:53:07 +0530244 if build and dt in build:
245 for result in webnotes.conn.sql(query_map[dt], as_dict=1):
246 create_cache(result['page_name'], dt, result['name'])
247 load_into_cache(result['page_name'])
Anand Doshi51146c02012-07-12 18:41:12 +0530248
249 for page_name in get_predefined_pages():
250 create_cache(page_name, None, None)
Anand Doshi72a824f2012-07-13 08:14:32 +0530251 if build: load_into_cache(page_name)