From 270abe0eab98d89bd6b1230163458d23b583965e Mon Sep 17 00:00:00 2001 From: Anand Doshi Date: Thu, 6 Feb 2014 12:48:09 +0530 Subject: [PATCH] Clear Website Cache, Website Group Cache, Post Cache (#421) --- webnotes/hooks.txt | 2 + .../templates/generators/website_group.py | 21 ++++++++++ webnotes/templates/pages/error.html | 2 +- webnotes/templates/pages/error.py | 5 ++- webnotes/templates/website_group/post.py | 29 +++++++++---- .../website/doctype/blog_post/blog_post.py | 8 ++-- webnotes/website/doctype/post/post.py | 12 +++--- .../website_sitemap/website_sitemap.py | 4 +- .../website_sitemap_permission.py | 4 +- webnotes/webutils.py | 42 +++++++++---------- 10 files changed, 82 insertions(+), 47 deletions(-) diff --git a/webnotes/hooks.txt b/webnotes/hooks.txt index 769619f150..377302f4fd 100644 --- a/webnotes/hooks.txt +++ b/webnotes/hooks.txt @@ -18,6 +18,8 @@ web_include_css = assets/css/webnotes-web.css web_include_js = website_script.js web_include_css = style_settings.css +website_clear_cache = webnotes.templates.generators.website_group.clear_cache + website_group_handler:Forum = webnotes.templates.website_group.forum website_group_handler:Events = webnotes.templates.website_group.events website_group_handler:Tasks = webnotes.templates.website_group.tasks diff --git a/webnotes/templates/generators/website_group.py b/webnotes/templates/generators/website_group.py index 0badc16a9b..0f8d366f42 100644 --- a/webnotes/templates/generators/website_group.py +++ b/webnotes/templates/generators/website_group.py @@ -3,6 +3,7 @@ import webnotes from webnotes.webutils import get_access, render_blocks, can_cache +from webnotes.templates.website_group.post import clear_post_cache doctype = "Website Group" no_cache = 1 @@ -101,3 +102,23 @@ def has_access(group, view): return access.get("write") else: return access.get("read") + +def clear_cache(page_name=None, website_group=None): + if page_name or website_group: + filters = {"page_name": page_name} if page_name else website_group + + website_group = webnotes.conn.get_value("Website Group", filters, + ["page_name", "group_type"], as_dict=True) + + if not website_group: + return + + website_groups = [website_group] + else: + clear_post_cache() + website_groups = webnotes.conn.sql("""select page_name, group_type from `tabWebsite Group`""", as_dict=True) + + cache = webnotes.cache() + for group in website_groups: + for view in get_views(group.group_type): + cache.delete_value("website_group_context:{}:{}".format(group.page_name, view)) diff --git a/webnotes/templates/pages/error.html b/webnotes/templates/pages/error.html index 71f1c1d913..89cde84b89 100644 --- a/webnotes/templates/pages/error.html +++ b/webnotes/templates/pages/error.html @@ -8,6 +8,6 @@ {% block content %}
-
%(error)s
+
{{ error }}
{% endblock %} \ No newline at end of file diff --git a/webnotes/templates/pages/error.py b/webnotes/templates/pages/error.py index 0bbaabfe5c..6e7b8c097d 100644 --- a/webnotes/templates/pages/error.py +++ b/webnotes/templates/pages/error.py @@ -9,4 +9,7 @@ no_cache = 1 no_sitemap = 1 def get_context(context): - return render_blocks(context) + error_context = {"error": webnotes.get_traceback()} + error_context.update(context) + + return render_blocks(error_context) diff --git a/webnotes/templates/website_group/post.py b/webnotes/templates/website_group/post.py index 32a42796fa..9071ea1365 100644 --- a/webnotes/templates/website_group/post.py +++ b/webnotes/templates/website_group/post.py @@ -9,15 +9,19 @@ def get_post_context(group_context): post = webnotes.doc("Post", webnotes.form_dict.name) if post.parent_post: raise webnotes.PermissionError + + def _get_post_context(): + fullname = get_fullname(post.owner) + return { + "title": "{} by {}".format(post.title, fullname), + # "group_title": group_context.get("unit_title") + " by {}".format(fullname), + "parent_post_html": get_parent_post_html(post, group_context.get("view")), + "post_list_html": get_child_posts_html(post, group_context.get("view")), + "parent_post": post.name + } - fullname = get_fullname(post.owner) - return { - "title": "{} by {}".format(post.title, fullname), - # "group_title": group_context.get("unit_title") + " by {}".format(fullname), - "parent_post_html": get_parent_post_html(post, group_context.get("view")), - "post_list_html": get_child_posts_html(post, group_context.get("view")), - "parent_post": post.name - } + cache_key = "website_group_post:".format(post.name) + return webnotes.cache().get_value(cache_key, lambda: _get_post_context()) def get_parent_post_html(post, view): profile = webnotes.bean("Profile", post.owner).doc @@ -38,4 +42,11 @@ def get_child_posts_html(post, view): "posts": posts, "parent_post": post.name, "view": view - }) \ No newline at end of file + }) + +def clear_post_cache(post=None): + cache = webnotes.cache() + posts = [post] if post else webnotes.conn.sql_list("select name from `tabPost`") + + for post in posts: + cache.delete_value("website_group_post:{}".format(post)) \ No newline at end of file diff --git a/webnotes/website/doctype/blog_post/blog_post.py b/webnotes/website/doctype/blog_post/blog_post.py index b54754473b..3cc1816bb0 100644 --- a/webnotes/website/doctype/blog_post/blog_post.py +++ b/webnotes/website/doctype/blog_post/blog_post.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals import webnotes -from webnotes.webutils import WebsiteGenerator, cleanup_page_name, delete_page_cache +from webnotes.webutils import WebsiteGenerator, cleanup_page_name, clear_cache from webnotes import _ from webnotes.utils import today @@ -29,11 +29,11 @@ class DocType(WebsiteGenerator): def on_update(self): WebsiteGenerator.on_update(self) - delete_page_cache("writers") + clear_cache("writers") def clear_blog_cache(): for blog in webnotes.conn.sql_list("""select page_name from `tabBlog Post` where ifnull(published,0)=1"""): - delete_page_cache(blog) + clear_cache(blog) - delete_page_cache("writers") + clear_cache("writers") diff --git a/webnotes/website/doctype/post/post.py b/webnotes/website/doctype/post/post.py index 86863330e3..5ff8b7874a 100644 --- a/webnotes/website/doctype/post/post.py +++ b/webnotes/website/doctype/post/post.py @@ -12,10 +12,8 @@ from webnotes.utils.email_lib import sendmail from webnotes.utils.file_manager import save_file from webnotes.webutils import get_access - -# TODO move these functions to framework -# from aapkamanch.post import clear_post_cache -# from aapkamanch.unit import clear_unit_views +from webnotes.templates.generators.website_group import clear_cache +from webnotes.templates.website_group.post import clear_post_cache class DocType: def __init__(self, d, dl): @@ -44,8 +42,8 @@ class DocType: self.doc.event_datetime = None def on_update(self): - # clear_unit_views(self.doc.website_group) - # clear_post_cache(self.doc.parent_post or self.doc.name) + clear_cache(website_group=self.doc.website_group) + clear_post_cache(self.doc.parent_post or self.doc.name) if self.doc.assigned_to and self.doc.assigned_to != self.assigned_to \ and webnotes.session.user != self.doc.assigned_to: @@ -181,7 +179,7 @@ def process_picture(post, picture_name, picture): file_data = save_file(picture_name, picture, "Post", post.doc.name, decode=True) post.doc.picture_url = file_data.file_name or file_data.file_url webnotes.conn.set_value("Post", post.doc.name, "picture_url", post.doc.picture_url) - # clear_unit_views(post.doc.website_group) + clear_cache(website_group=post.doc.website_group) @webnotes.whitelist() def suggest_user(group, term): diff --git a/webnotes/website/doctype/website_sitemap/website_sitemap.py b/webnotes/website/doctype/website_sitemap/website_sitemap.py index e47929be88..ebfeb33d63 100644 --- a/webnotes/website/doctype/website_sitemap/website_sitemap.py +++ b/webnotes/website/doctype/website_sitemap/website_sitemap.py @@ -52,14 +52,14 @@ class DocType(DocTypeNestedSet): self.doc.public_read = self.doc.public_write = 0 def on_trash(self): - from webnotes.webutils import delete_page_cache + from webnotes.webutils import clear_cache # remove website sitemap permissions to_remove = webnotes.conn.sql_list("""select name from `tabWebsite Sitemap Permission` where website_sitemap=%s""", (self.doc.name,)) webnotes.delete_doc("Website Sitemap Permission", to_remove, ignore_permissions=True) - delete_page_cache(self.doc.name) + clear_cache(self.doc.name) def add_to_sitemap(options): bean = webnotes.new_bean("Website Sitemap") diff --git a/webnotes/website/doctype/website_sitemap_permission/website_sitemap_permission.py b/webnotes/website/doctype/website_sitemap_permission/website_sitemap_permission.py index 5de997cfcb..299f82fcea 100644 --- a/webnotes/website/doctype/website_sitemap_permission/website_sitemap_permission.py +++ b/webnotes/website/doctype/website_sitemap_permission/website_sitemap_permission.py @@ -68,9 +68,11 @@ def _get_access(sitemap_page, profile): return { "read": read, "write": write, "admin": admin, "private_read": private_read } -def clear_permissions(profiles): +def clear_permissions(profiles=None): if isinstance(profiles, basestring): profiles = [profiles] + elif profiles is None: + profiles = webnotes.conn.sql_list("""select name from `tabProfile`""") cache = webnotes.cache() for profile in profiles: diff --git a/webnotes/webutils.py b/webnotes/webutils.py index 776c471ce3..b90b573a9d 100644 --- a/webnotes/webutils.py +++ b/webnotes/webutils.py @@ -13,8 +13,10 @@ from urllib import quote import mimetypes from webnotes.website.doctype.website_sitemap.website_sitemap import add_to_sitemap, update_sitemap, remove_sitemap + +# frequently used imports (used by other modules) from webnotes.website.doctype.website_sitemap_permission.website_sitemap_permission \ - import get_access + import get_access, clear_permissions class PageNotFoundError(Exception): pass @@ -27,7 +29,6 @@ def render(page_name): except Exception: page_name = "error" data = render_page(page_name) - data = insert_traceback(data) data = set_content_type(data, page_name) webnotes._response.data = data @@ -46,7 +47,9 @@ def render_page(page_name): out = out.get("data") if out: - webnotes._response.headers[b"From Cache"] = True + if hasattr(webnotes, "_response"): + webnotes._response.headers[b"From Cache"] = True + return out return build(page_name) @@ -238,15 +241,6 @@ def scrub_page_name(page_name): return page_name -def insert_traceback(data): - traceback = webnotes.get_traceback() - if isinstance(data, dict): - data["content"] = data["content"] % {"error": traceback} - else: - data = data % {"error": traceback} - - return data - def set_content_type(data, page_name): if isinstance(data, dict): webnotes._response.headers[b"Content-Type"] = b"application/json; charset: utf-8" @@ -262,23 +256,27 @@ def set_content_type(data, page_name): return data def clear_cache(page_name=None): + cache = webnotes.cache() + if page_name: delete_page_cache(page_name) + else: - cache = webnotes.cache() for p in webnotes.conn.sql_list("""select name from `tabWebsite Sitemap`"""): if p is not None: - cache.delete_value("page:" + p) - cache.delete_value("home_page") - cache.delete_value("page:index") - cache.delete_value("website_sitemap") - cache.delete_value("website_sitemap_config") + delete_page_cache(p) + cache.delete_value("home_page") + clear_permissions() + + for method in webnotes.get_hooks("website_clear_cache"): + webnotes.get_attr(method)(page_name) + def delete_page_cache(page_name): - if page_name: - cache = webnotes.cache() - cache.delete_value("page:" + page_name) - cache.delete_value("website_sitemap") + cache = webnotes.cache() + cache.delete_value("page:" + page_name) + cache.delete_value("page_context:" + page_name) + cache.delete_value("sitemap_options:" + page_name) def is_signup_enabled(): if getattr(webnotes.local, "is_signup_enabled", None) is None: