2012-02-23 07:05:32 +00:00
|
|
|
# ERPNext - web based ERP (http://erpnext.com)
|
|
|
|
# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-07-19 08:10:31 +00:00
|
|
|
from __future__ import unicode_literals
|
2012-12-07 05:30:26 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
import conf
|
2012-01-31 12:31:16 +00:00
|
|
|
import webnotes
|
2012-12-07 05:30:26 +00:00
|
|
|
|
2012-12-07 07:14:45 +00:00
|
|
|
page_map = {
|
|
|
|
'Web Page': webnotes._dict({
|
|
|
|
"template": 'html/web_page.html',
|
|
|
|
"condition_field": "published"
|
|
|
|
}),
|
|
|
|
'Blog': webnotes._dict({
|
|
|
|
"template": 'html/blog_page.html',
|
|
|
|
"condition_field": "published",
|
|
|
|
}),
|
|
|
|
'Item': webnotes._dict({
|
|
|
|
"template": 'html/product_page.html',
|
|
|
|
"condition_field": "show_in_website",
|
2012-12-14 11:09:27 +00:00
|
|
|
}),
|
|
|
|
'Item Group': webnotes._dict({
|
|
|
|
"template": "html/product_group.html",
|
|
|
|
"condition_field": "show_in_website"
|
2012-12-07 07:14:45 +00:00
|
|
|
})
|
2012-12-07 05:30:26 +00:00
|
|
|
}
|
2012-01-31 12:31:16 +00:00
|
|
|
|
2012-12-27 11:20:24 +00:00
|
|
|
page_settings_map = {
|
2012-12-27 13:10:42 +00:00
|
|
|
"about": "About Us Settings",
|
|
|
|
"contact": "Contact Us Settings"
|
2012-12-27 11:20:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 07:37:23 +00:00
|
|
|
def render(page_name):
|
|
|
|
"""render html page"""
|
|
|
|
try:
|
|
|
|
if page_name:
|
|
|
|
html = get_html(page_name)
|
|
|
|
else:
|
|
|
|
html = get_html('index')
|
2013-02-12 07:24:58 +00:00
|
|
|
except Exception:
|
2012-12-19 04:44:59 +00:00
|
|
|
html = get_html('error')
|
2012-08-02 07:37:23 +00:00
|
|
|
|
2012-08-03 10:32:24 +00:00
|
|
|
from webnotes.handler import eprint, print_zip
|
|
|
|
eprint("Content-Type: text/html")
|
|
|
|
print_zip(html)
|
2012-12-06 09:28:44 +00:00
|
|
|
|
2012-08-02 07:37:23 +00:00
|
|
|
def get_html(page_name):
|
|
|
|
"""get page html"""
|
|
|
|
page_name = scrub_page_name(page_name)
|
|
|
|
|
2012-12-07 05:30:26 +00:00
|
|
|
html = ''
|
|
|
|
|
|
|
|
# load from cache, if auto cache clear is falsy
|
|
|
|
if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0):
|
|
|
|
html = webnotes.cache().get_value("page:" + page_name)
|
2012-12-19 04:44:59 +00:00
|
|
|
from_cache = True
|
2012-12-07 05:30:26 +00:00
|
|
|
|
2012-12-19 04:44:59 +00:00
|
|
|
if not html:
|
2012-12-07 05:30:26 +00:00
|
|
|
html = load_into_cache(page_name)
|
2012-12-19 04:44:59 +00:00
|
|
|
from_cache = False
|
2012-12-07 05:30:26 +00:00
|
|
|
|
2012-12-19 04:44:59 +00:00
|
|
|
if not html:
|
|
|
|
html = get_html("404")
|
2012-08-02 07:37:23 +00:00
|
|
|
|
2012-12-19 04:44:59 +00:00
|
|
|
if page_name=="error":
|
2012-12-25 12:48:17 +00:00
|
|
|
html = html.replace("%(error)s", webnotes.getTraceback())
|
2012-08-02 07:37:23 +00:00
|
|
|
else:
|
2012-12-19 04:44:59 +00:00
|
|
|
comments = "\n\npage:"+page_name+\
|
|
|
|
"\nload status: " + (from_cache and "cache" or "fresh")
|
|
|
|
html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
|
|
|
|
|
|
|
|
return html
|
2012-12-06 09:28:44 +00:00
|
|
|
|
|
|
|
def scrub_page_name(page_name):
|
|
|
|
if page_name.endswith('.html'):
|
|
|
|
page_name = page_name[:-5]
|
|
|
|
|
|
|
|
return page_name
|
|
|
|
|
|
|
|
def page_name(title):
|
|
|
|
"""make page name from title"""
|
|
|
|
import re
|
|
|
|
name = title.lower()
|
|
|
|
name = re.sub('[~!@#$%^&*()<>,."\']', '', name)
|
2013-01-17 07:54:51 +00:00
|
|
|
name = re.sub('[:/]', '-', name)
|
|
|
|
|
|
|
|
name = '-'.join(name.split())
|
|
|
|
|
|
|
|
# replace repeating hyphens
|
|
|
|
name = re.sub(r"(-)\1+", r"\1", name)
|
|
|
|
|
|
|
|
return name
|
2012-12-06 10:45:38 +00:00
|
|
|
|
|
|
|
def update_page_name(doc, title):
|
|
|
|
"""set page_name and check if it is unique"""
|
2012-12-06 11:39:38 +00:00
|
|
|
webnotes.conn.set(doc, "page_name", page_name(title))
|
2012-12-06 10:45:38 +00:00
|
|
|
|
2012-12-27 11:20:24 +00:00
|
|
|
standard_pages = get_template_pages()
|
|
|
|
if doc.page_name in standard_pages:
|
|
|
|
webnotes.conn.sql("""Page Name cannot be one of %s""" % ', '.join(standard_pages))
|
|
|
|
|
2012-12-06 10:45:38 +00:00
|
|
|
res = webnotes.conn.sql("""\
|
|
|
|
select count(*) from `tab%s`
|
|
|
|
where page_name=%s and name!=%s""" % (doc.doctype, '%s', '%s'),
|
|
|
|
(doc.page_name, doc.name))
|
|
|
|
if res and res[0][0] > 0:
|
|
|
|
webnotes.msgprint("""A %s with the same title already exists.
|
|
|
|
Please change the title of %s and save again."""
|
|
|
|
% (doc.doctype, doc.name), raise_exception=1)
|
2012-12-07 05:30:26 +00:00
|
|
|
|
|
|
|
delete_page_cache(doc.page_name)
|
|
|
|
|
|
|
|
def load_into_cache(page_name):
|
|
|
|
args = prepare_args(page_name)
|
2012-12-19 04:44:59 +00:00
|
|
|
if not args:
|
|
|
|
return ""
|
2012-12-07 05:30:26 +00:00
|
|
|
html = build_html(args)
|
|
|
|
webnotes.cache().set_value("page:" + page_name, html)
|
|
|
|
return html
|
|
|
|
|
|
|
|
def build_html(args):
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
|
|
|
templates_path = os.path.join(os.path.dirname(conf.__file__),
|
|
|
|
'app', 'website', 'templates')
|
|
|
|
|
2012-12-25 09:39:14 +00:00
|
|
|
args["len"] = len
|
|
|
|
|
2012-12-07 05:30:26 +00:00
|
|
|
jenv = Environment(loader = FileSystemLoader(templates_path))
|
|
|
|
html = jenv.get_template(args['template']).render(args)
|
|
|
|
|
|
|
|
return html
|
|
|
|
|
|
|
|
def prepare_args(page_name):
|
|
|
|
if page_name == 'index':
|
|
|
|
page_name = get_home_page()
|
|
|
|
|
|
|
|
if page_name in get_template_pages():
|
2012-12-27 11:20:24 +00:00
|
|
|
args = webnotes._dict({
|
2013-01-01 06:35:13 +00:00
|
|
|
'template': 'pages/%s.html' % page_name,
|
2012-12-07 05:30:26 +00:00
|
|
|
'name': page_name,
|
2012-12-27 11:20:24 +00:00
|
|
|
})
|
|
|
|
if page_name in page_settings_map:
|
2013-02-18 12:54:28 +00:00
|
|
|
args.obj = webnotes.bean(page_settings_map[page_name]).obj
|
2012-12-07 05:30:26 +00:00
|
|
|
else:
|
|
|
|
args = get_doc_fields(page_name)
|
|
|
|
|
2012-12-19 04:44:59 +00:00
|
|
|
if not args:
|
|
|
|
return False
|
|
|
|
|
2013-02-21 05:19:37 +00:00
|
|
|
get_outer_env(page_name, args)
|
2012-12-07 05:30:26 +00:00
|
|
|
|
|
|
|
return args
|
|
|
|
|
2012-12-07 07:14:45 +00:00
|
|
|
def get_template_pages():
|
2012-12-07 05:30:26 +00:00
|
|
|
pages_path = os.path.join(os.path.dirname(conf.__file__), 'app',
|
|
|
|
'website', 'templates', 'pages')
|
|
|
|
page_list = []
|
|
|
|
for page in os.listdir(pages_path):
|
|
|
|
page_list.append(scrub_page_name(page))
|
|
|
|
|
|
|
|
return page_list
|
|
|
|
|
|
|
|
def get_doc_fields(page_name):
|
|
|
|
doc_type, doc_name = get_source_doc(page_name)
|
2012-12-19 04:44:59 +00:00
|
|
|
if not doc_type:
|
|
|
|
return False
|
|
|
|
|
2012-12-17 10:30:34 +00:00
|
|
|
obj = webnotes.get_obj(doc_type, doc_name, with_children=True)
|
2012-12-07 07:14:45 +00:00
|
|
|
|
2012-12-07 05:30:26 +00:00
|
|
|
if hasattr(obj, 'prepare_template_args'):
|
|
|
|
obj.prepare_template_args()
|
2012-12-07 07:14:45 +00:00
|
|
|
|
2012-12-07 05:30:26 +00:00
|
|
|
args = obj.doc.fields
|
2012-12-07 07:14:45 +00:00
|
|
|
args['template'] = page_map[doc_type].template
|
2012-12-17 10:30:34 +00:00
|
|
|
args['obj'] = obj
|
2012-12-20 11:41:51 +00:00
|
|
|
args['int'] = int
|
2012-12-07 05:30:26 +00:00
|
|
|
|
|
|
|
return args
|
|
|
|
|
|
|
|
def get_source_doc(page_name):
|
|
|
|
"""get source doc for the given page name"""
|
2012-12-07 07:14:45 +00:00
|
|
|
for doctype in page_map:
|
2012-12-07 05:30:26 +00:00
|
|
|
name = webnotes.conn.sql("""select name from `tab%s` where
|
2012-12-07 07:14:45 +00:00
|
|
|
page_name=%s and ifnull(%s, 0)=1""" % (doctype, "%s",
|
|
|
|
page_map[doctype].condition_field), page_name)
|
2012-12-07 05:30:26 +00:00
|
|
|
if name:
|
2012-12-07 07:14:45 +00:00
|
|
|
return doctype, name[0][0]
|
|
|
|
|
2012-12-07 05:30:26 +00:00
|
|
|
return None, None
|
|
|
|
|
2013-02-21 05:19:37 +00:00
|
|
|
def get_outer_env(page_name, args):
|
2013-02-20 09:32:21 +00:00
|
|
|
from webnotes.utils import get_request_site_address
|
|
|
|
from urllib import quote
|
|
|
|
|
2012-12-07 05:30:26 +00:00
|
|
|
all_top_items = webnotes.conn.sql("""\
|
|
|
|
select * from `tabTop Bar Item`
|
|
|
|
where parent='Website Settings' and parentfield='top_bar_items'
|
|
|
|
order by idx asc""", as_dict=1)
|
2012-12-21 05:22:26 +00:00
|
|
|
|
2012-12-07 05:30:26 +00:00
|
|
|
top_items = [d for d in all_top_items if not d['parent_label']]
|
|
|
|
|
|
|
|
# attach child items to top bar
|
|
|
|
for d in all_top_items:
|
|
|
|
if d['parent_label']:
|
|
|
|
for t in top_items:
|
|
|
|
if t['label']==d['parent_label']:
|
|
|
|
if not 'child_items' in t:
|
|
|
|
t['child_items'] = []
|
|
|
|
t['child_items'].append(d)
|
|
|
|
break
|
|
|
|
|
2012-12-21 05:31:31 +00:00
|
|
|
if top_items and ("products" in [d.url.split(".")[0] for d in top_items if d.url]):
|
2012-12-20 11:41:51 +00:00
|
|
|
# product categories
|
|
|
|
products = webnotes.conn.sql("""select t1.item_group as label,
|
2013-01-01 05:25:58 +00:00
|
|
|
t2.page_name as url,
|
2012-12-20 11:41:51 +00:00
|
|
|
ifnull(t1.indent,0) as indent
|
|
|
|
from `tabWebsite Product Category` t1, `tabItem Group` t2
|
|
|
|
where t1.item_group = t2.name
|
|
|
|
and ifnull(t2.show_in_website,0)=1 order by t1.idx""", as_dict=1)
|
2012-12-24 07:22:15 +00:00
|
|
|
products_item = filter(lambda d: d.url and d.url.split(".")[0]=="products", top_items)[0]
|
2012-12-20 11:41:51 +00:00
|
|
|
products_item.child_items = products
|
|
|
|
|
2012-12-25 12:48:17 +00:00
|
|
|
ret = webnotes._dict({
|
2012-12-07 05:30:26 +00:00
|
|
|
'top_bar_items': top_items,
|
|
|
|
'footer_items': webnotes.conn.sql("""\
|
|
|
|
select * from `tabTop Bar Item`
|
|
|
|
where parent='Website Settings' and parentfield='footer_items'
|
|
|
|
order by idx asc""", as_dict=1),
|
|
|
|
|
2012-12-20 11:41:51 +00:00
|
|
|
'int':int
|
2012-12-25 12:48:17 +00:00
|
|
|
})
|
|
|
|
|
2013-02-21 05:19:37 +00:00
|
|
|
args.update(ret)
|
|
|
|
|
2012-12-25 12:48:17 +00:00
|
|
|
settings = webnotes.doc("Website Settings", "Website Settings")
|
2013-02-20 09:32:21 +00:00
|
|
|
for k in ["brand_html", "copyright", "address", "top_bar_background", "favicon",
|
2013-02-21 05:19:37 +00:00
|
|
|
"facebook_share", "google_plus_one", "twitter_share", "linked_in_share", "twitter_share_via"]:
|
2012-12-25 12:48:17 +00:00
|
|
|
if k in settings.fields:
|
2013-02-21 05:19:37 +00:00
|
|
|
args[k] = settings.fields.get(k)
|
2012-12-25 12:48:17 +00:00
|
|
|
|
2013-02-27 05:21:26 +00:00
|
|
|
for k in ["facebook_share", "google_plus_one", "twitter_share", "linked_in_share"]:
|
|
|
|
args[k] = int(args.get(k) or 0)
|
|
|
|
|
2013-02-21 05:19:37 +00:00
|
|
|
if not args.brand_html:
|
|
|
|
args.brand_html = "ERPNext"
|
|
|
|
if not args.top_bar_background:
|
|
|
|
args.top_bar_background = "Black"
|
2013-02-20 09:32:21 +00:00
|
|
|
|
2013-02-21 05:41:21 +00:00
|
|
|
args.url = quote(str(get_request_site_address(full_address=True)), str(""))
|
|
|
|
args.encoded_title = quote(str(args.title or ""), str(""))
|
2013-02-20 09:32:21 +00:00
|
|
|
|
2013-02-21 05:19:37 +00:00
|
|
|
return args
|
2012-12-07 05:30:26 +00:00
|
|
|
|
|
|
|
def get_home_page():
|
|
|
|
doc_name = webnotes.conn.get_value('Website Settings', None, 'home_page')
|
|
|
|
if doc_name:
|
|
|
|
page_name = webnotes.conn.get_value('Web Page', doc_name, 'page_name')
|
|
|
|
else:
|
|
|
|
page_name = 'login'
|
|
|
|
|
|
|
|
return page_name
|
|
|
|
|
2012-12-07 07:25:06 +00:00
|
|
|
def clear_cache(page_name=None):
|
2012-12-07 05:30:26 +00:00
|
|
|
if page_name:
|
|
|
|
delete_page_cache(page_name)
|
|
|
|
else:
|
2013-01-22 05:34:55 +00:00
|
|
|
cache = webnotes.cache()
|
|
|
|
for p in get_all_pages():
|
|
|
|
cache.delete_value("page:" + p)
|
|
|
|
|
|
|
|
def get_all_pages():
|
|
|
|
all_pages = get_template_pages()
|
|
|
|
all_pages += page_settings_map.keys()
|
|
|
|
for doctype in page_map:
|
|
|
|
all_pages += [p[0] for p in webnotes.conn.sql("""select distinct page_name
|
|
|
|
from `tab%s`""" % doctype) if p[0]]
|
|
|
|
|
|
|
|
return all_pages
|
|
|
|
|
2012-12-07 05:30:26 +00:00
|
|
|
def delete_page_cache(page_name):
|
2012-12-30 14:28:24 +00:00
|
|
|
if page_name:
|
|
|
|
webnotes.cache().delete_value("page:" + page_name)
|
2012-12-27 08:35:02 +00:00
|
|
|
|
|
|
|
def url_for_website(url):
|
|
|
|
if url and not url.lower().startswith("http"):
|
|
|
|
return "files/" + url
|
|
|
|
else:
|
|
|
|
return url
|