added html to tinymce + moved website/web_cache.py > website/utils.py

This commit is contained in:
Rushabh Mehta 2012-12-07 11:00:26 +05:30
parent 0ea130a97c
commit 571377a313
6 changed files with 151 additions and 201 deletions

View File

@ -42,9 +42,6 @@ class DocType:
# webpage updates
from website.utils import update_page_name
update_page_name(self.doc, self.doc.item_name)
elif self.doc.page_name:
from website.web_cache import clear_cache
clear_cache(self.doc.page_name)
bin = sql("select stock_uom from `tabBin` where item_code = '%s' " % self.doc.item_code)
if bin and cstr(bin[0][0]) != cstr(self.doc.stock_uom):
@ -85,7 +82,7 @@ class DocType:
where item_code=%s and is_cancelled='Yes' """, self.doc.item_code)
if self.doc.page_name:
from website.web_cache import clear_cache
from website.utils import clear_cache
clear_cache(self.doc.page_name)
# Check whether Ref Rate is not entered twice for same Price List and Currency
@ -199,7 +196,7 @@ class DocType:
def on_rename(self,newdn,olddn):
sql("update tabItem set item_code = %s where name = %s", (newdn, olddn))
if self.doc.page_name:
from website.web_cache import clear_cache
from website.utils import clear_cache
clear_cache(self.doc.page_name)
def prepare_template_args(self):

View File

@ -29,14 +29,13 @@ def get_blog_list(args=None):
# strip html tags from content
import webnotes.utils
import website.web_cache
for res in result:
from webnotes.utils import global_date_format, get_fullname
res['full_name'] = get_fullname(res['owner'])
res['published'] = global_date_format(res['creation'])
if not res['content']:
res['content'] = website.web_cache.get_page_html(res['name'])
res['content'] = website.utils.get_html(res['name'])
res['content'] = split_blog_content(res['content'])
res['content'] = res['content'][:1000]
@ -88,7 +87,6 @@ def add_comment(args=None):
import webnotes
import webnotes.utils, markdown2
import webnotes.widgets.form.comments
import website.web_cache
if not args: args = webnotes.form_dict
args['comment'] = unicode(markdown2.markdown(args.get('comment') or ''))
@ -96,13 +94,13 @@ def add_comment(args=None):
comment = webnotes.widgets.form.comments.add_comment(args)
# since comments are embedded in the page, clear the web cache
website.web_cache.clear_cache(args.get('page_name'))
website.utils.clear_cache(args.get('page_name'))
comment['comment_date'] = webnotes.utils.global_date_format(comment['creation'])
template_args = { 'comment_list': [comment], 'template': 'html/comment.html' }
# get html of comment row
comment_html = website.web_cache.build_html(template_args)
comment_html = website.utils.build_html(template_args)
# notify commentors
commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where
@ -142,8 +140,8 @@ def add_subscriber():
lead.save()
def get_blog_content(blog_page_name):
import website.web_cache
content = website.web_cache.get_html(blog_page_name)
import website.utils
content = website.utils.get_html(blog_page_name)
content = split_blog_content(content)
import webnotes.utils
content = webnotes.utils.escape_html(content)

View File

@ -36,7 +36,7 @@ class DocType():
from webnotes.sessions import clear_cache
clear_cache('Guest')
from website.web_cache import clear_cache
from website.utils import clear_cache
clear_cache(self.doc.page_name)
clear_cache('index')

View File

@ -28,7 +28,7 @@ class DocType:
make_web_core()
# clear web cache (for menus!)
from website.web_cache import clear_cache
from website.utils import clear_cache
clear_cache()
from webnotes.sessions import clear_cache

View File

@ -15,7 +15,17 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import os
import conf
import webnotes
from webnotes.utils import cstr
template_map = {
'Web Page': 'html/web_page.html',
'Blog': 'html/blog_page.html',
'Item': 'html/product_page.html',
}
def render(page_name):
"""render html page"""
@ -36,8 +46,20 @@ def get_html(page_name):
page_name = scrub_page_name(page_name)
comments = get_comments(page_name)
from website.web_cache import get_page_html
html = get_page_html(page_name, comments)
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)
comments += "\nload status: fresh"
if not html:
html = load_into_cache(page_name)
comments += "\nload status: cache"
# insert comments
html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
return html
def get_comments(page_name):
@ -54,16 +76,6 @@ def scrub_page_name(page_name):
return page_name
def make_template(doc, path, convert_fields = ['main_section', 'side_section']):
"""make template"""
import os, jinja2
# write template
with open(path, 'r') as f:
temp = jinja2.Template(f.read())
return temp.render(doc = doc.fields)
def page_name(title):
"""make page name from title"""
import re
@ -83,3 +95,121 @@ def update_page_name(doc, title):
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)
delete_page_cache(doc.page_name)
def load_into_cache(page_name):
args = prepare_args(page_name)
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')
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():
args = {
'template': 'pages/%s.html' % page_name,
'name': page_name,
}
else:
args = get_doc_fields(page_name)
args.update(get_outer_env())
return args
def get_template_pages():
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)
obj = webnotes.get_obj(doc_type, doc_name)
if hasattr(obj, 'prepare_template_args'):
obj.prepare_template_args()
args = obj.doc.fields
args['template'] = template_map[doc_type]
return args
def get_source_doc(page_name):
"""get source doc for the given page name"""
for doctype in [('Web Page', 'published'), ('Blog', 'published'),
('Item', 'show_in_website')]:
name = webnotes.conn.sql("""select name from `tab%s` where
page_name=%s and ifnull(`%s`, 0)=1""" % (doctype[0], "%s", doctype[1]),
page_name)
if name:
return doctype[0], name[0][0]
return None, None
def get_outer_env():
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)
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
return {
'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),
'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext',
'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon')
}
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
def clear_cache(page_name):
if page_name:
delete_page_cache(page_name)
else:
webnotes.cache().delete_keys("page:")
def delete_page_cache(page_name):
webnotes.cache().delete_value("page:" + page_name)

View File

@ -1,175 +0,0 @@
# 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/>.
# html generation functions
from __future__ import unicode_literals
import os
import conf
import webnotes
import website.utils
from webnotes.utils import cstr
template_map = {
'Web Page': 'html/web_page.html',
'Blog': 'html/blog_page.html',
'Item': 'html/product_page.html',
}
def get_page_html(page_name, comments=''):
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)
comments += "\n\npage load status: fresh"
if not html:
html = load_into_cache(page_name)
comments += "\n\npage load status: cache"
# insert comments
html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
return html
def load_into_cache(page_name):
args = prepare_args(page_name)
html = build_html(args)
webnotes.cache().set_value("page:" + page_name, html)
return html
def build_html(args):
templates_path = os.path.join(os.path.dirname(conf.__file__),
'app', 'website', 'templates')
from jinja2 import Environment, FileSystemLoader
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_predefined_pages():
args = {
'template': 'pages/%s.html' % page_name,
'name': page_name,
}
else:
args = get_doc_fields(page_name)
args.update(get_outer_env())
return args
def load_from_cache(page_name):
result = search_cache(page_name)
if not result:
if page_name in get_predefined_pages():
# if a predefined page doesn't exist, load it into cache
return None
else:
# if page doesn't exist, raise exception
raise Exception, "Page %s not found" % page_name
return result[0][0]
def get_predefined_pages():
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(website.utils.scrub_page_name(page))
return page_list
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
def get_doc_fields(page_name):
doc_type, doc_name = get_source_doc(page_name)
obj = webnotes.get_obj(doc_type, doc_name)
if hasattr(obj, 'prepare_template_args'):
obj.prepare_template_args()
args = obj.doc.fields
args['template'] = template_map[doc_type]
return args
def get_source_doc(page_name):
"""get source doc for the given page name"""
for doctype in [('Web Page', 'published'), ('Blog', 'published'),
('Item', 'show_in_website')]:
name = webnotes.conn.sql("""select name from `tab%s` where
page_name=%s and ifnull(`%s`, 0)=1""" % (doctype[0], "%s", doctype[1]),
page_name)
if name:
return doctype[0], name[0][0]
return None, None
def get_outer_env():
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)
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
return {
'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),
'brand': webnotes.conn.get_value('Website Settings', None, 'brand_html') or 'ERPNext',
'copyright': webnotes.conn.get_value('Website Settings', None, 'copyright'),
'favicon': webnotes.conn.get_value('Website Settings', None, 'favicon')
}
def clear_cache(page_name):
if page_name:
delete_page_cache(page_name)
else:
webnotes.cache().delete_keys("page:")
def delete_page_cache(page_name):
webnotes.cache().delete_value("page:" + page_name)