|
@@ -10,6 +10,8 @@ from webnotes import _ |
|
|
import webnotes.utils |
|
|
import webnotes.utils |
|
|
import mimetypes |
|
|
import mimetypes |
|
|
|
|
|
|
|
|
|
|
|
from website.doctype.website_sitemap.website_sitemap import add_to_sitemap |
|
|
|
|
|
|
|
|
class PageNotFoundError(Exception): pass |
|
|
class PageNotFoundError(Exception): pass |
|
|
|
|
|
|
|
|
def render(page_name): |
|
|
def render(page_name): |
|
@@ -62,11 +64,13 @@ def build_page(page_name): |
|
|
if not webnotes.conn: |
|
|
if not webnotes.conn: |
|
|
webnotes.connect() |
|
|
webnotes.connect() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sitemap_options = webnotes.doc("Website Sitemap", page_name).fields |
|
|
sitemap_options = webnotes.doc("Website Sitemap", page_name).fields |
|
|
|
|
|
|
|
|
page_options = webnotes.doc("Website Sitemap Config", |
|
|
page_options = webnotes.doc("Website Sitemap Config", |
|
|
sitemap_options.get("website_sitemap_config")).fields.update(sitemap_options) |
|
|
|
|
|
|
|
|
sitemap_options.get("website_sitemap_config")).fields.update({ |
|
|
|
|
|
"page_name":sitemap_options.page_name, |
|
|
|
|
|
"docname":sitemap_options.docname |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
if not page_options: |
|
|
if not page_options: |
|
|
raise PageNotFoundError |
|
|
raise PageNotFoundError |
|
@@ -106,39 +110,7 @@ def build_page(page_name): |
|
|
if not no_cache: |
|
|
if not no_cache: |
|
|
webnotes.cache().set_value("page:" + page_name, html) |
|
|
webnotes.cache().set_value("page:" + page_name, html) |
|
|
return html |
|
|
return html |
|
|
|
|
|
|
|
|
def build_sitemap(): |
|
|
|
|
|
webnotes.conn.sql("""delete from `tabWebsite Sitemap Config`""") |
|
|
|
|
|
webnotes.conn.sql("""delete from `tabWebsite Sitemap`""") |
|
|
|
|
|
webnotes.conn.commit() |
|
|
|
|
|
build_website_sitemap_config() |
|
|
|
|
|
|
|
|
|
|
|
for config in webnotes.conn.sql("""select * from `tabWebsite Sitemap Config`""", as_dict=True): |
|
|
|
|
|
if config.page_or_generator == "Page": |
|
|
|
|
|
config.page_name = config.link_name |
|
|
|
|
|
add_to_sitemap(config) |
|
|
|
|
|
else: |
|
|
|
|
|
module = webnotes.get_module(config.controller) |
|
|
|
|
|
|
|
|
|
|
|
condition = "" |
|
|
|
|
|
if hasattr(module, "condition_field"): |
|
|
|
|
|
condition = " where ifnull(%s, 0)=1" % module.condition_field |
|
|
|
|
|
|
|
|
|
|
|
page_name_field = getattr(module, "page_name_field", "page_name") |
|
|
|
|
|
|
|
|
|
|
|
for name in webnotes.conn.sql_list("""select name from `tab%s` %s""" \ |
|
|
|
|
|
% (module.doctype, condition)): |
|
|
|
|
|
webnotes.bean(module.doctype, name).run_method("on_update") |
|
|
|
|
|
|
|
|
|
|
|
def add_to_sitemap(options): |
|
|
|
|
|
doc = webnotes.doc({"doctype":"Website Sitemap"}) |
|
|
|
|
|
for key in ("page_name", "ref_doctype", "docname", "page_or_generator", "lastmod"): |
|
|
|
|
|
doc.fields[key] = options.get(key) |
|
|
|
|
|
doc.name = options.page_name |
|
|
|
|
|
doc.website_sitemap_config = options.link_name |
|
|
|
|
|
doc.insert() |
|
|
|
|
|
webnotes.conn.commit() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_home_page(): |
|
|
def get_home_page(): |
|
|
if not webnotes.conn: |
|
|
if not webnotes.conn: |
|
|
webnotes.connect() |
|
|
webnotes.connect() |
|
@@ -149,65 +121,6 @@ def get_home_page(): |
|
|
page_name = 'login' |
|
|
page_name = 'login' |
|
|
|
|
|
|
|
|
return page_name |
|
|
return page_name |
|
|
|
|
|
|
|
|
def build_website_sitemap_config(): |
|
|
|
|
|
config = {"pages": {}, "generators":{}} |
|
|
|
|
|
basepath = webnotes.utils.get_base_path() |
|
|
|
|
|
|
|
|
|
|
|
for path, folders, files in os.walk(basepath, followlinks=True): |
|
|
|
|
|
if 'locale' in folders: |
|
|
|
|
|
folders.remove('locale') |
|
|
|
|
|
|
|
|
|
|
|
# utility - remove pyc files |
|
|
|
|
|
for f in files: |
|
|
|
|
|
if f.decode("utf-8").endswith(".pyc"): |
|
|
|
|
|
os.remove(os.path.join(path, f)) |
|
|
|
|
|
|
|
|
|
|
|
if os.path.basename(path)=="pages" and os.path.basename(os.path.dirname(path))=="templates": |
|
|
|
|
|
for fname in files: |
|
|
|
|
|
fname = webnotes.utils.cstr(fname) |
|
|
|
|
|
if fname.split(".")[-1] in ("html", "xml", "js", "css"): |
|
|
|
|
|
add_website_sitemap_config("Page", path, fname) |
|
|
|
|
|
|
|
|
|
|
|
if os.path.basename(path)=="generators" and os.path.basename(os.path.dirname(path))=="templates": |
|
|
|
|
|
for fname in files: |
|
|
|
|
|
if fname.endswith(".html"): |
|
|
|
|
|
add_website_sitemap_config("Generator", path, fname) |
|
|
|
|
|
|
|
|
|
|
|
webnotes.conn.commit() |
|
|
|
|
|
|
|
|
|
|
|
def add_website_sitemap_config(page_or_generator, path, fname): |
|
|
|
|
|
basepath = webnotes.utils.get_base_path() |
|
|
|
|
|
name = fname |
|
|
|
|
|
if fname.endswith(".html"): |
|
|
|
|
|
name = fname[:-5] |
|
|
|
|
|
|
|
|
|
|
|
template_path = os.path.relpath(os.path.join(path, fname), basepath) |
|
|
|
|
|
|
|
|
|
|
|
options = webnotes._dict({ |
|
|
|
|
|
"doctype": "Website Sitemap Config", |
|
|
|
|
|
"page_or_generator": page_or_generator, |
|
|
|
|
|
"link_name": name, |
|
|
|
|
|
"template_path": template_path, |
|
|
|
|
|
"lastmod": time.ctime(os.path.getmtime(template_path)) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
controller_name = fname.split(".")[0].replace("-", "_") + ".py" |
|
|
|
|
|
controller_path = os.path.join(path, controller_name) |
|
|
|
|
|
if os.path.exists(controller_path): |
|
|
|
|
|
options.controller = os.path.relpath(controller_path[:-3], basepath).replace(os.path.sep, ".") |
|
|
|
|
|
options.controller = ".".join(options.controller.split(".")[1:]) |
|
|
|
|
|
|
|
|
|
|
|
if options.controller: |
|
|
|
|
|
module = webnotes.get_module(options.controller) |
|
|
|
|
|
options.no_cache = getattr(module, "no_cache", 0) |
|
|
|
|
|
options.no_sitemap = options.no_cache or getattr(module, "no_sitemap", 0) |
|
|
|
|
|
options.ref_doctype = getattr(module, "doctype", None) |
|
|
|
|
|
|
|
|
|
|
|
webnotes.doc(options).insert() |
|
|
|
|
|
|
|
|
|
|
|
return options |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_website_settings(): |
|
|
def get_website_settings(): |
|
|
from webnotes.utils import get_request_site_address, encode, cint |
|
|
from webnotes.utils import get_request_site_address, encode, cint |
|
@@ -288,31 +201,6 @@ def delete_page_cache(page_name): |
|
|
cache.delete_value("page:" + page_name) |
|
|
cache.delete_value("page:" + page_name) |
|
|
cache.delete_value("website_sitemap") |
|
|
cache.delete_value("website_sitemap") |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
|
|
def is_signup_enabled(): |
|
|
def is_signup_enabled(): |
|
|
if getattr(webnotes.local, "is_signup_enabled", None) is None: |
|
|
if getattr(webnotes.local, "is_signup_enabled", None) is None: |
|
|
webnotes.local.is_signup_enabled = True |
|
|
webnotes.local.is_signup_enabled = True |
|
@@ -324,17 +212,15 @@ def is_signup_enabled(): |
|
|
|
|
|
|
|
|
class WebsiteGenerator(object): |
|
|
class WebsiteGenerator(object): |
|
|
def setup_generator(self): |
|
|
def setup_generator(self): |
|
|
self._website_config = webnotes.conn.get_values("Website Sitemap Config", {"ref_doctype": self.doc.doctype}, "*")[0] |
|
|
|
|
|
self._website_module = webnotes.get_module(self._website_config.controller) |
|
|
|
|
|
|
|
|
|
|
|
self._page_name_field = getattr(self._website_module, "page_name_field", "page_name") |
|
|
|
|
|
self._condition_field = getattr(self._website_module, "condition_field", "") |
|
|
|
|
|
|
|
|
self._website_config = webnotes.conn.get_values("Website Sitemap Config", |
|
|
|
|
|
{"ref_doctype": self.doc.doctype}, "*")[0] |
|
|
|
|
|
|
|
|
def on_update(self, page_name=None): |
|
|
def on_update(self, page_name=None): |
|
|
self.setup_generator() |
|
|
self.setup_generator() |
|
|
if self._condition_field: |
|
|
|
|
|
if not self.doc.fields[self._condition_field]: |
|
|
|
|
|
remove_page(self.doc.fields[self._page_name_field]) |
|
|
|
|
|
|
|
|
if self._website_config.condition_field: |
|
|
|
|
|
if not self.doc.fields[self._website_config.condition_field]: |
|
|
|
|
|
# condition field failed, return |
|
|
|
|
|
remove_page(self.doc.fields[self._website_config.page_name_field]) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
if not page_name: |
|
|
if not page_name: |
|
@@ -343,17 +229,19 @@ class WebsiteGenerator(object): |
|
|
self.check_if_page_name_is_unique(new_page_name) |
|
|
self.check_if_page_name_is_unique(new_page_name) |
|
|
|
|
|
|
|
|
remove_page(self.doc.page_name) |
|
|
remove_page(self.doc.page_name) |
|
|
add_generator_to_sitemap(self.doc.doctype, self.doc.name, new_page_name, self.doc.modified, |
|
|
|
|
|
self._website_config, self._website_module) |
|
|
|
|
|
|
|
|
|
|
|
if self.doc.fields[self._page_name_field]!=new_page_name: |
|
|
|
|
|
webnotes.conn.set(self.doc, self._page_name_field, new_page_name) |
|
|
|
|
|
else: |
|
|
|
|
|
add_generator_to_sitemap(self.doc.doctype, self.doc.name, page_name, self.doc.modified, |
|
|
|
|
|
self._website_config, self._website_module) |
|
|
|
|
|
|
|
|
if self.doc.fields[self._website_config.page_name_field]!=new_page_name: |
|
|
|
|
|
webnotes.conn.set(self.doc, self._website_config.page_name_field, new_page_name) |
|
|
|
|
|
|
|
|
|
|
|
page_name = new_page_name |
|
|
|
|
|
|
|
|
|
|
|
add_to_sitemap(webnotes._dict({ |
|
|
|
|
|
"ref_doctype":self.doc.doctype, |
|
|
|
|
|
"docname": self.doc.name, |
|
|
|
|
|
"page_name": page_name, |
|
|
|
|
|
"link_name": self._website_config.name, |
|
|
|
|
|
"lastmod": self.doc.modified})) |
|
|
|
|
|
|
|
|
delete_page_cache(self.doc.page_name) |
|
|
|
|
|
|
|
|
|
|
|
def check_if_page_name_is_unique(self, new_page_name): |
|
|
def check_if_page_name_is_unique(self, new_page_name): |
|
|
if webnotes.conn.sql("""select name from `tabWebsite Sitemap` where name=%s |
|
|
if webnotes.conn.sql("""select name from `tabWebsite Sitemap` where name=%s |
|
|
and ref_doctype!=%s and docname!=%s""", (new_page_name, self.doc.doctype, self.doc.name)): |
|
|
and ref_doctype!=%s and docname!=%s""", (new_page_name, self.doc.doctype, self.doc.name)): |
|
@@ -364,23 +252,6 @@ class WebsiteGenerator(object): |
|
|
self.setup_generator() |
|
|
self.setup_generator() |
|
|
remove_page(self.doc.fields[self._page_name_field]) |
|
|
remove_page(self.doc.fields[self._page_name_field]) |
|
|
|
|
|
|
|
|
def add_generator_to_sitemap(ref_doctype, docname, page_name, modified, config=None, module=None): |
|
|
|
|
|
if not config: |
|
|
|
|
|
config = webnotes.conn.get_values("Website Sitemap Config", {"ref_doctype": ref_doctype}, "*")[0] |
|
|
|
|
|
if not module: |
|
|
|
|
|
module = webnotes.get_module(config.controller) |
|
|
|
|
|
|
|
|
|
|
|
page_name_field = getattr(module, "page_name_field", "page_name") |
|
|
|
|
|
|
|
|
|
|
|
opts = config.copy() |
|
|
|
|
|
opts["page_name"] = page_name |
|
|
|
|
|
if page_name_field != "page_name": |
|
|
|
|
|
opts["page_name_field"] = page_name_field |
|
|
|
|
|
opts["docname"] = docname |
|
|
|
|
|
opts["lastmod"] = modified |
|
|
|
|
|
add_to_sitemap(opts) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_page(page_name): |
|
|
def remove_page(page_name): |
|
|
if page_name: |
|
|
if page_name: |
|
|
delete_page_cache(page_name) |
|
|
delete_page_cache(page_name) |
|
@@ -399,3 +270,28 @@ def cleanup_page_name(title): |
|
|
name = re.sub(r"(-)\1+", r"\1", name) |
|
|
name = re.sub(r"(-)\1+", r"\1", name) |
|
|
|
|
|
|
|
|
return name |
|
|
return 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) |