[website] [cleanup] moved website generic utils to framework

This commit is contained in:
Rushabh Mehta 2013-04-02 10:41:37 +05:30
parent 7692352390
commit 9db1a68d89
18 changed files with 150 additions and 379 deletions

View File

@ -35,13 +35,13 @@ class DocType(DocTypeNestedSet):
if self.doc.show_in_website:
from website.utils import update_page_name
from webnotes.webutils import update_page_name
# webpage updates
page_name = self.doc.name
if webnotes.conn.get_value("Product Settings", None,
"default_product_category")==self.doc.name:
page_name = "products"
from website.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache()
update_page_name(self.doc, page_name)
@ -51,7 +51,7 @@ class DocType(DocTypeNestedSet):
elif self.doc.page_name:
# if unchecked show in website
from website.utils import delete_page_cache
from webnotes.webutils import delete_page_cache
delete_page_cache(self.doc.page_name)
invalidate_cache_for(self.doc.name)

114
startup/website.py Normal file
View File

@ -0,0 +1,114 @@
import webnotes, conf, os
def get_templates_path():
return os.path.join(os.path.dirname(conf.__file__), "app", "website", "templates")
standard_pages = [
"404", "about", "account", "attributions", "blog", "contact", "error", "index",
"login", "message", "order", "orders", "print", "product_search", "profile",
"ticket", "tickets", "writers"
]
page_map = {
'Web Page': webnotes._dict({
"template": 'html/web_page.html',
"condition_field": "published"
}),
'Blog Post': webnotes._dict({
"template": 'html/blog_page.html',
"condition_field": "published",
}),
'Item': webnotes._dict({
"template": 'html/product_page.html',
"condition_field": "show_in_website",
}),
'Item Group': webnotes._dict({
"template": "html/product_group.html",
"condition_field": "show_in_website"
})
}
page_settings_map = {
"about": "website.doctype.about_us_settings.about_us_settings.get_args",
"contact": "Contact Us Settings",
"blog": "website.helpers.blog.get_blog_template_args",
"writers": "website.helpers.blog.get_writers_args",
"print": "core.doctype.print_format.print_format.get_args",
"orders": "selling.doctype.sales_order.sales_order.get_currency_and_number_format",
"order": "selling.doctype.sales_order.sales_order.get_website_args",
"ticket": "support.doctype.support_ticket.support_ticket.get_website_args"
}
no_cache = ["message", "print", "order", "ticket"]
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 update_template_args(page_name, args):
from webnotes.utils import get_request_site_address
from urllib import quote
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
if top_items and ("products" in [d.url.split(".")[0] for d in top_items if d.url]):
# product categories
products = webnotes.conn.sql("""select t1.item_group as label,
t2.page_name as url,
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)
products_item = filter(lambda d: d.url and d.url.split(".")[0]=="products", top_items)[0]
products_item.child_items = products
ret = webnotes._dict({
'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),
'int':int,
"webnotes": webnotes,
"utils": webnotes.utils
})
args.update(ret)
settings = webnotes.doc("Website Settings", "Website Settings")
for k in ["banner_html", "brand_html", "copyright", "address", "twitter_share_via",
"favicon", "facebook_share", "google_plus_one", "twitter_share", "linked_in_share"]:
if k in settings.fields:
args[k] = settings.fields.get(k)
for k in ["facebook_share", "google_plus_one", "twitter_share", "linked_in_share"]:
args[k] = int(args.get(k) or 0)
args.url = quote(str(get_request_site_address(full_address=True)), str(""))
args.encoded_title = quote(str(args.title or ""), str(""))
return args

View File

@ -86,7 +86,7 @@ class DocType(DocListController):
self.doclist.get({"doctype":"Website Item Group"})]
if self.doc.show_in_website:
from website.utils import update_page_name
from webnotes.webutils import update_page_name
if self.doc.name==self.doc.item_name:
page_name_from = self.doc.name
else:
@ -98,7 +98,7 @@ class DocType(DocListController):
elif self.doc.page_name:
# if unchecked show in website
from website.utils import delete_page_cache
from webnotes.webutils import delete_page_cache
delete_page_cache(self.doc.page_name)
_invalidate_cache()
@ -112,7 +112,7 @@ class DocType(DocListController):
where item_code=%s and is_cancelled='Yes' """, self.doc.item_code)
if self.doc.page_name:
from website.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache(self.doc.page_name)
# Check whether Ref Rate is not entered twice for same Price List and Currency
@ -231,7 +231,7 @@ class DocType(DocListController):
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.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache(self.doc.page_name)
def prepare_template_args(self):

View File

@ -8,7 +8,7 @@ class DocType:
self.doc, self.doclist = d, dl
def on_update(self):
from website.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache("about")
def get_args():

View File

@ -9,6 +9,6 @@ class DocType:
def on_update(self):
# for blog footer
from website.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache()

View File

@ -17,7 +17,7 @@
from __future__ import unicode_literals
import webnotes
import website.utils
import webnotes.webutils
from webnotes import _
class DocType:
@ -25,7 +25,7 @@ class DocType:
self.doc, self.doclist = d, dl
def autoname(self):
from website.utils import page_name
from webnotes.webutils import page_name
self.doc.name = page_name(self.doc.title)
def validate(self):
@ -38,8 +38,8 @@ class DocType:
where name=%s""", self.doc.blogger)
def on_update(self):
website.utils.update_page_name(self.doc, self.doc.title)
website.utils.delete_page_cache("writers")
webnotes.webutils.update_page_name(self.doc, self.doc.title)
webnotes.webutils.delete_page_cache("writers")
def send_emails(self):
"""send emails to subscribers"""

View File

@ -17,5 +17,5 @@ class DocType:
self.address = webnotes.bean("Address", self.doc.address).doc
def on_update(self):
from website.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache("contact")

View File

@ -9,7 +9,7 @@ class DocType:
def on_update(self):
"""clear web cache"""
from website.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache()
if self.doc.default_product_category:

View File

@ -27,7 +27,7 @@ class DocType:
def validate(self):
"""make custom css"""
from jinja2 import Template
from website.utils import get_hex_shade
from webnotes.webutils import get_hex_shade
import os
self.validate_colors()
@ -46,7 +46,7 @@ class DocType:
from webnotes.sessions import clear_cache
clear_cache('Guest')
from website.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache()
for f in ["small_font_size", "at_import", "heading_text_style"]:

View File

@ -22,11 +22,11 @@ class DocType():
self.doc, self.doclist = d, dl
def autoname(self):
from website.utils import page_name
from webnotes.webutils import page_name
self.doc.name = page_name(self.doc.title)
def on_update(self):
from website.utils import update_page_name
from webnotes.webutils import update_page_name
update_page_name(self.doc, self.doc.title)
self.if_home_clear_cache()
@ -36,7 +36,7 @@ class DocType():
from webnotes.sessions import clear_cache
clear_cache('Guest')
from website.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache(self.doc.page_name)
clear_cache('index')

View File

@ -28,7 +28,7 @@ class DocType:
make()
# clear web cache (for menus!)
from website.utils import clear_cache
from webnotes.webutils import clear_cache
clear_cache()
def set_home_page(self):

View File

@ -3,15 +3,15 @@
from __future__ import unicode_literals
import webnotes
import website.utils
import webnotes.webutils
from webnotes import _
def clear_blog_cache():
for blog in webnotes.conn.sql_list("""select page_name from
`tabBlog Post` where ifnull(published,0)=1"""):
website.utils.delete_page_cache(blog)
webnotes.webutils.delete_page_cache(blog)
website.utils.delete_page_cache("writers")
webnotes.webutils.delete_page_cache("writers")
@webnotes.whitelist(allow_guest=True)
def get_blog_list(start=0, by=None, category=None):
@ -44,7 +44,7 @@ def get_blog_list(start=0, by=None, category=None):
from webnotes.utils import global_date_format, get_fullname
res['published'] = global_date_format(res['creation'])
if not res['content']:
res['content'] = website.utils.get_html(res['page_name'])
res['content'] = webnotes.webutils.get_html(res['page_name'])
res['content'] = res['content'][:140]
return result
@ -71,13 +71,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.utils.clear_cache(args.get('page_name'))
webnotes.webutils.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.utils.build_html(template_args)
comment_html = webnotes.webutils.build_html(template_args)
# notify commentors
commentors = [d[0] for d in webnotes.conn.sql("""select comment_by from tabComment where
@ -115,8 +115,8 @@ def add_subscriber(name, email_id):
lead.save()
def get_blog_content(blog_page_name):
import website.utils
content = website.utils.get_html(blog_page_name)
import webnotes.webutils
content = webnotes.webutils.get_html(blog_page_name)
import webnotes.utils
content = webnotes.utils.escape_html(content)
return content

View File

@ -3,14 +3,14 @@
import os
import webnotes
import website.utils
import webnotes.webutils
def make():
if not webnotes.conn:
webnotes.connect()
home_page = website.utils.get_home_page()
home_page = webnotes.webutils.get_home_page()
fname = 'js/wn-web.js'
if os.path.basename(os.path.abspath('.'))!='public':

View File

@ -5,7 +5,7 @@ from __future__ import unicode_literals
import webnotes
from webnotes.utils import cstr
from website.utils import build_html, delete_page_cache
from webnotes.webutils import build_html, delete_page_cache
@webnotes.whitelist(allow_guest=True)

View File

@ -13,7 +13,7 @@ def generate(domain):
global frame_xml, link_xml
import urllib, os
import webnotes
import website.utils
import webnotes.webutils
# settings
max_doctypes = 10
@ -24,8 +24,8 @@ def generate(domain):
if domain:
# list of all pages in web cache
for doctype in website.utils.page_map:
d = website.utils.page_map[doctype];
for doctype in webnotes.webutils.page_map:
d = webnotes.webutils.page_map[doctype];
pages = webnotes.conn.sql("""select page_name, `modified`
from `tab%s` where ifnull(%s,0)=1
order by modified desc""" % (doctype, d.condition_field))

View File

@ -93,6 +93,7 @@ wn.module_page["Website"] = [
{
title: wn._("Advanced Scripting"),
icon: "icon-wrench",
right: true,
items: [
{
"route":"Form/Website Script",

View File

@ -1,33 +0,0 @@
import webnotes
page_map = {
'Web Page': webnotes._dict({
"template": 'html/web_page.html',
"condition_field": "published"
}),
'Blog Post': webnotes._dict({
"template": 'html/blog_page.html',
"condition_field": "published",
}),
'Item': webnotes._dict({
"template": 'html/product_page.html',
"condition_field": "show_in_website",
}),
'Item Group': webnotes._dict({
"template": "html/product_group.html",
"condition_field": "show_in_website"
})
}
page_settings_map = {
"about": "website.doctype.about_us_settings.about_us_settings.get_args",
"contact": "Contact Us Settings",
"blog": "website.helpers.blog.get_blog_template_args",
"writers": "website.helpers.blog.get_writers_args",
"print": "core.doctype.print_format.print_format.get_args",
"orders": "selling.doctype.sales_order.sales_order.get_currency_and_number_format",
"order": "selling.doctype.sales_order.sales_order.get_website_args",
"ticket": "support.doctype.support_ticket.support_ticket.get_website_args"
}
no_cache = ["message", "print", "order", "ticket"]

View File

@ -1,311 +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/>.
from __future__ import unicode_literals
import os
import conf
from website.settings import *
import webnotes
import webnotes.utils
def render(page_name):
"""render html page"""
try:
if page_name:
html = get_html(page_name)
else:
html = get_html('index')
except Exception:
html = get_html('error')
from webnotes.handler import eprint, print_zip
eprint("Content-Type: text/html")
print_zip(html)
def get_html(page_name):
"""get page html"""
page_name = scrub_page_name(page_name)
html = ''
# load from cache, if auto cache clear is falsy
if not (hasattr(conf, 'auto_cache_clear') and conf.auto_cache_clear or 0):
if not page_name in no_cache:
html = webnotes.cache().get_value("page:" + page_name)
from_cache = True
if not html:
from webnotes.auth import HTTPRequest
webnotes.http_request = HTTPRequest()
#webnotes.connect()
html = load_into_cache(page_name)
from_cache = False
if not html:
html = get_html("404")
if page_name=="error":
html = html.replace("%(error)s", webnotes.getTraceback())
else:
comments = "\n\npage:"+page_name+\
"\nload status: " + (from_cache and "cache" or "fresh")
html += """\n<!-- %s -->""" % webnotes.utils.cstr(comments)
return html
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)
name = re.sub('[:/]', '-', name)
name = '-'.join(name.split())
# replace repeating hyphens
name = re.sub(r"(-)\1+", r"\1", name)
return name
def update_page_name(doc, title):
"""set page_name and check if it is unique"""
webnotes.conn.set(doc, "page_name", page_name(title))
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))
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)
delete_page_cache(doc.page_name)
def load_into_cache(page_name):
args = prepare_args(page_name)
if not args:
return ""
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')
args["len"] = len
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 = webnotes._dict({
'template': 'pages/%s.html' % page_name,
'name': page_name,
})
if page_name in page_settings_map:
target = page_settings_map[page_name]
if "." in target:
args.update(webnotes.get_method(target)())
else:
args.obj = webnotes.bean(page_settings_map[page_name]).obj
else:
args = get_doc_fields(page_name)
if not args:
return False
get_outer_env(page_name, args)
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)
if not doc_type:
return False
obj = webnotes.get_obj(doc_type, doc_name, with_children=True)
if hasattr(obj, 'prepare_template_args'):
obj.prepare_template_args()
args = obj.doc.fields
args['template'] = page_map[doc_type].template
args['obj'] = obj
args['int'] = int
return args
def get_source_doc(page_name):
"""get source doc for the given page name"""
for doctype in page_map:
name = webnotes.conn.sql("""select name from `tab%s` where
page_name=%s and ifnull(%s, 0)=1""" % (doctype, "%s",
page_map[doctype].condition_field), page_name)
if name:
return doctype, name[0][0]
return None, None
def get_outer_env(page_name, args):
from webnotes.utils import get_request_site_address
from urllib import quote
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
if top_items and ("products" in [d.url.split(".")[0] for d in top_items if d.url]):
# product categories
products = webnotes.conn.sql("""select t1.item_group as label,
t2.page_name as url,
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)
products_item = filter(lambda d: d.url and d.url.split(".")[0]=="products", top_items)[0]
products_item.child_items = products
ret = webnotes._dict({
'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),
'int':int,
"webnotes": webnotes,
"utils": webnotes.utils
})
args.update(ret)
settings = webnotes.doc("Website Settings", "Website Settings")
for k in ["banner_html", "brand_html", "copyright", "address", "twitter_share_via",
"favicon", "facebook_share", "google_plus_one", "twitter_share", "linked_in_share"]:
if k in settings.fields:
args[k] = settings.fields.get(k)
for k in ["facebook_share", "google_plus_one", "twitter_share", "linked_in_share"]:
args[k] = int(args.get(k) or 0)
args.url = quote(str(get_request_site_address(full_address=True)), str(""))
args.encoded_title = quote(str(args.title or ""), str(""))
return args
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=None):
if page_name:
delete_page_cache(page_name)
else:
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
def delete_page_cache(page_name):
if page_name:
webnotes.cache().delete_value("page:" + page_name)
def get_hex_shade(color, percent):
def p(c):
v = int(c, 16) + int(int('ff', 16) * (float(percent)/100))
if v < 0:
v=0
if v > 255:
v=255
h = hex(v)[2:]
if len(h) < 2:
h = "0" + h
return h
r, g, b = color[0:2], color[2:4], color[4:6]
avg = (float(int(r, 16) + int(g, 16) + int(b, 16)) / 3)
# switch dark and light shades
if avg > 128:
percent = -percent
# stronger diff for darker shades
if percent < 25 and avg < 64:
percent = percent * 2
return p(r) + p(g) + p(b)