Parcourir la source

Clear Website Cache, Website Group Cache, Post Cache (#421)

version-14
Anand Doshi il y a 11 ans
Parent
révision
270abe0eab
10 fichiers modifiés avec 82 ajouts et 47 suppressions
  1. +2
    -0
      webnotes/hooks.txt
  2. +21
    -0
      webnotes/templates/generators/website_group.py
  3. +1
    -1
      webnotes/templates/pages/error.html
  4. +4
    -1
      webnotes/templates/pages/error.py
  5. +20
    -9
      webnotes/templates/website_group/post.py
  6. +4
    -4
      webnotes/website/doctype/blog_post/blog_post.py
  7. +5
    -7
      webnotes/website/doctype/post/post.py
  8. +2
    -2
      webnotes/website/doctype/website_sitemap/website_sitemap.py
  9. +3
    -1
      webnotes/website/doctype/website_sitemap_permission/website_sitemap_permission.py
  10. +20
    -22
      webnotes/webutils.py

+ 2
- 0
webnotes/hooks.txt Voir le fichier

@@ -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


+ 21
- 0
webnotes/templates/generators/website_group.py Voir le fichier

@@ -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))

+ 1
- 1
webnotes/templates/pages/error.html Voir le fichier

@@ -8,6 +8,6 @@

{% block content %}
<div class="error-content">
<pre>%(error)s</pre>
<pre>{{ error }}</pre>
</div>
{% endblock %}

+ 4
- 1
webnotes/templates/pages/error.py Voir le fichier

@@ -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)

+ 20
- 9
webnotes/templates/website_group/post.py Voir le fichier

@@ -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
})
})
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))

+ 4
- 4
webnotes/website/doctype/blog_post/blog_post.py Voir le fichier

@@ -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")

+ 5
- 7
webnotes/website/doctype/post/post.py Voir le fichier

@@ -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):


+ 2
- 2
webnotes/website/doctype/website_sitemap/website_sitemap.py Voir le fichier

@@ -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")


+ 3
- 1
webnotes/website/doctype/website_sitemap_permission/website_sitemap_permission.py Voir le fichier

@@ -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:


+ 20
- 22
webnotes/webutils.py Voir le fichier

@@ -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:


Chargement…
Annuler
Enregistrer