blob: 21ec6a8bd3c45ae543c031d896e01554643038f8 [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 Doshi51146c02012-07-12 18:41:12 +053019template_map = {
20 'Web Page': 'html/web_page.html',
21 'Blog': 'html/blog_page.html',
22 'Item': 'html/product_page.html',
23}
24
25def get_html(page_name, comments=''):
Anand Doshi72c945b2012-06-22 20:01:07 +053026 import conf
Anand Doshi51146c02012-07-12 18:41:12 +053027
28 html = ''
29
30 # load from cache, if auto cache clear is falsy
31 if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0):
32 html = load_from_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053033
Anand Doshi51146c02012-07-12 18:41:12 +053034 if not html:
35 html = load_into_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053036 comments += "\n\npage load status: fresh"
Anand Doshi51146c02012-07-12 18:41:12 +053037
38 # insert comments
39 import webnotes.utils
40 html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
Anand Doshibba4db12012-06-27 12:06:45 +053041
Anand Doshi72c945b2012-06-22 20:01:07 +053042 return html
43
Anand Doshi51146c02012-07-12 18:41:12 +053044def load_from_cache(page_name):
Anand Doshi72c945b2012-06-22 20:01:07 +053045 import webnotes
Anand Doshi51146c02012-07-12 18:41:12 +053046
47 result = search_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053048
Anand Doshi51146c02012-07-12 18:41:12 +053049 if not result:
50 if page_name in get_predefined_pages():
51 # if a predefined page doesn't exist, load it into cache
52 return None
53 else:
54 # if page doesn't exist, raise exception
55 raise Exception, "Page %s not found" % page_name
56
57 return result[0][0]
58
59def load_into_cache(page_name):
60 args = prepare_args(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053061
Anand Doshi51146c02012-07-12 18:41:12 +053062 html = build_html(args)
Anand Doshi72c945b2012-06-22 20:01:07 +053063
Anand Doshi51146c02012-07-12 18:41:12 +053064 # create cache entry for predefined pages, if not exists
65 if page_name in get_predefined_pages():
66 create_cache(page_name)
Anand Doshi72c945b2012-06-22 20:01:07 +053067
Anand Doshi51146c02012-07-12 18:41:12 +053068 import webnotes
Anand Doshi72c945b2012-06-22 20:01:07 +053069 webnotes.conn.begin()
70 webnotes.conn.set_value('Web Cache', page_name, 'html', html)
71 webnotes.conn.commit()
72
73 return html
Anand Doshi51fc20b2012-07-11 18:56:13 +053074
Anand Doshi51146c02012-07-12 18:41:12 +053075def get_predefined_pages():
76 """
77 gets a list of predefined pages
78 they do not exist in `tabWeb Page`
79 """
80 import os
81 import conf
82 import website.utils
83
84 pages_path = os.path.join(conf.modules_path, 'website', 'templates', 'pages')
85
86 page_list = []
87
88 for page in os.listdir(pages_path):
89 page_list.append(website.utils.scrub_page_name(page))
90
91 return page_list
92
93def prepare_args(page_name):
94 if page_name == 'index':
95 page_name = get_home_page()
96
97 if page_name in get_predefined_pages():
Anand Doshi8c7e76b2012-07-11 18:40:57 +053098 args = {
Anand Doshi51146c02012-07-12 18:41:12 +053099 'template': 'pages/%s.html' % page_name,
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530100 'name': page_name,
101 }
102 else:
Anand Doshi51146c02012-07-12 18:41:12 +0530103 args = get_doc_fields(page_name)
104
105 args.update(get_outer_env())
106
107 return args
108
109def get_home_page():
110 import webnotes
111 doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
112 if doc_name:
113 page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
114 else:
115 page_name = 'login'
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530116
Anand Doshi51146c02012-07-12 18:41:12 +0530117 return page_name
118
119def get_doc_fields(page_name):
120 import webnotes
121 doc_type, doc_name = webnotes.conn.get_value('Web Cache', page_name, ['doc_type', 'doc_name'])
122
123 import webnotes.model.code
124 obj = webnotes.model.code.get_obj(doc_type, doc_name)
125
126 if hasattr(obj, 'prepare_template_args'):
127 obj.prepare_template_args()
128
129 args = obj.doc.fields
130 args['template'] = template_map[doc_type]
Anand Doshi8c7e76b2012-07-11 18:40:57 +0530131
132 return args
Anand Doshi72c945b2012-06-22 20:01:07 +0530133
Anand Doshi72c945b2012-06-22 20:01:07 +0530134def get_outer_env():
Anand Doshi51146c02012-07-12 18:41:12 +0530135 """
136 env dict for outer template
137 """
Anand Doshi72c945b2012-06-22 20:01:07 +0530138 import webnotes
139 return {
Anand Doshi51146c02012-07-12 18:41:12 +0530140 'top_bar_items': webnotes.conn.sql("""\
141 select * from `tabTop Bar Item`
Anand Doshi72c945b2012-06-22 20:01:07 +0530142 where parent='Website Settings' and parentfield='top_bar_items'
143 order by idx asc""", as_dict=1),
144
Anand Doshi51146c02012-07-12 18:41:12 +0530145 'footer_items': webnotes.conn.sql("""\
146 select * from `tabTop Bar Item`
Anand Doshi72c945b2012-06-22 20:01:07 +0530147 where parent='Website Settings' and parentfield='footer_items'
148 order by idx asc""", as_dict=1),
149
Anand Doshif7765a82012-07-11 19:05:27 +0530150 'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext',
Anand Doshi72c945b2012-06-22 20:01:07 +0530151 'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
Anand Doshi40fce892012-07-09 20:02:52 +0530152 'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon')
Anand Doshi72c945b2012-06-22 20:01:07 +0530153 }
154
Anand Doshi51146c02012-07-12 18:41:12 +0530155def build_html(args):
156 """
157 build html using jinja2 templates
158 """
159 import os
160 import conf
161 templates_path = os.path.join(conf.modules_path, 'website', 'templates')
162
163 from jinja2 import Environment, FileSystemLoader
164 jenv = Environment(loader = FileSystemLoader(templates_path))
165 html = jenv.get_template(args['template']).render(args)
166 return html
Anand Doshi72c945b2012-06-22 20:01:07 +0530167
168# cache management
Anand Doshi51146c02012-07-12 18:41:12 +0530169def search_cache(page_name):
Anand Doshi1a51cc92012-07-12 19:03:58 +0530170 if not page_name: return ()
Anand Doshi72c945b2012-06-22 20:01:07 +0530171 import webnotes
Anand Doshi51146c02012-07-12 18:41:12 +0530172 return webnotes.conn.sql("""\
173 select html, doc_type, doc_name
174 from `tabWeb Cache`
175 where name = %s""", page_name)
176
177def create_cache(page_name, doc_type=None, doc_name=None):
178 # check if a record already exists
179 result = search_cache(page_name)
180 if result: return
181
182 # create a Web Cache record
183 import webnotes.model.doc
184 d = webnotes.model.doc.Document('Web Cache')
185 d.name = page_name
186 d.doc_type = doc_type
187 d.doc_name = doc_name
188 d.html = None
189 d.save()
190
191def clear_cache(page_name, doc_type=None, doc_name=None):
192 """
193 * if no page name, clear whole cache
194 * if page_name, doc_type and doc_name match, clear cache's copy
195 * else, raise exception that such a page already exists
196 """
Anand Doshi1a51cc92012-07-12 19:03:58 +0530197 import webnotes
198
Anand Doshi51146c02012-07-12 18:41:12 +0530199 if not page_name:
200 webnotes.conn.sql("""update `tabWeb Cache` set html = ''""")
201 return
202
203 result = search_cache(page_name)
Anand Doshi1a51cc92012-07-12 19:03:58 +0530204
Anand Doshi51146c02012-07-12 18:41:12 +0530205 if not doc_type or (result and result[0][1] == doc_type and result[0][2] == doc_name):
206 webnotes.conn.set_value('Web Cache', page_name, 'html', '')
Anand Doshi72c945b2012-06-22 20:01:07 +0530207 else:
208 webnotes.msgprint("""Page with name "%s" already exists as a %s.
Anand Doshi51146c02012-07-12 18:41:12 +0530209 Please save it with another name.""" % (page_name, result[0][1]),
210 raise_exception=1)
211
212def delete_cache(page_name):
Anand Doshi72c945b2012-06-22 20:01:07 +0530213 """
214 delete entry of page_name from Web Cache
215 used when:
216 * web page is deleted
217 * blog is un-published
218 """
219 import webnotes
Anand Doshi51146c02012-07-12 18:41:12 +0530220 webnotes.conn.sql("""delete from `tabWeb Cache` where name=%s""", page_name)
221
Anand Doshi200c4432012-07-13 01:14:52 +0530222def refresh_cache(build=False):
Anand Doshi51146c02012-07-12 18:41:12 +0530223 """delete and re-create web cache entries"""
Anand Doshi87901882012-07-11 20:04:32 +0530224 import webnotes
Anand Doshi87901882012-07-11 20:04:32 +0530225
Anand Doshi51146c02012-07-12 18:41:12 +0530226 webnotes.conn.sql("delete from `tabWeb Cache`")
227
228 query_map = {
229 'Web Page': """select page_name, name from `tabWeb Page` where docstatus=0""",
230 'Blog': """\
231 select page_name, name from `tabBlog`
232 where docstatus = 0 and ifnull(published, 0) = 1""",
233 'Item': """\
234 select page_name, name from `tabItem`
235 where docstatus = 0 and ifnull(show_in_website, 0) = 1""",
236 }
237
238 for dt in query_map:
239 for result in webnotes.conn.sql(query_map[dt], as_dict=1):
240 create_cache(result['page_name'], dt, result['name'])
241 clear_cache(result['page_name'], dt, result['name'])
Anand Doshi200c4432012-07-13 01:14:52 +0530242 if build: load_into_cache(result['page_name'])
Anand Doshi51146c02012-07-12 18:41:12 +0530243
244 for page_name in get_predefined_pages():
245 create_cache(page_name, None, None)
Anand Doshi200c4432012-07-13 01:14:52 +0530246 clear_cache(page_name, None, None)
247 if build: load_into_cache(page_name)