def delete_view_wiki_cache(link): """ Delete the template cache for this wiki page. http://pythonhosted.org/Flask-Caching/#caching-jinja2-snippets """ key = make_template_fragment_key("wiki_title", vary_on=[link]) cache.delete(key) key2 = make_template_fragment_key("wiki_content", vary_on=[link]) cache.delete(key2)
def blog_delete(post): # Clear the cached template fragment cache_key = make_template_fragment_key("blogpost_" + str(post.id)) cache.delete(cache_key) db.session.delete(post) db.session.commit() flash('Post `{0}` deleted.'.format(post.title)) return redirect(url_for('blog_index'))
def test_scoreboard_is_cached(): """Test that /api/v1/scoreboard is properly cached and cleared""" app = create_ctfd() with app.app_context(): # create user1 register_user(app, name="user1", email="*****@*****.**") # create challenge chal = gen_challenge(app.db, value=100) gen_flag(app.db, challenge_id=chal.id, content="flag") chal_id = chal.id # create a solve for the challenge for user1. (the id is 2 because of the admin) gen_solve(app.db, user_id=2, challenge_id=chal_id) with login_as_user(app, "user1") as client: # No cached data assert app.cache.get("view/api.scoreboard_scoreboard_list") is None assert app.cache.get("view/api.scoreboard_scoreboard_detail") is None # Load and check cached data client.get("/api/v1/scoreboard") assert app.cache.get("view/api.scoreboard_scoreboard_list") client.get("/api/v1/scoreboard/top/10") assert app.cache.get("view/api.scoreboard_scoreboard_detail") # Check scoreboard page assert ( app.cache.get(make_template_fragment_key("public_scoreboard_table")) is None ) client.get("/scoreboard") assert app.cache.get(make_template_fragment_key("public_scoreboard_table")) # Empty standings and check that the cached data is gone clear_standings() assert app.cache.get("view/api.scoreboard_scoreboard_list") is None assert app.cache.get("view/api.scoreboard_scoreboard_detail") is None assert ( app.cache.get(make_template_fragment_key("public_scoreboard_table")) is None ) destroy_ctfd(app)
def blog_edit(post): form = PostForm(user=post.user, timestamp=post.timestamp, published=post.published, title=post.title, body=post.body) if form.validate_on_submit(): form.populate_obj(post) db.session.add(post) db.session.commit() # Clear the cached template fragment cache_key = make_template_fragment_key("blogpost_" + str(post.id)) cache.delete(cache_key) return redirect(post.url) return render_blog('blog_edit.html', form=form)
def test_20_jinja2ext_cache(self): somevar = ''.join( [random.choice(string.ascii_letters) for x in range(6)]) testkeys = [ make_template_fragment_key("fragment1"), make_template_fragment_key("fragment1", vary_on=["key1"]), make_template_fragment_key("fragment1", vary_on=["key1", somevar]), ] delkey = make_template_fragment_key("fragment2") with self.app.test_request_context(): #: Test if elements are cached render_template("test_template.html", somevar=somevar, timeout=60) for k in testkeys: assert self.cache.get(k) == somevar assert self.cache.get(delkey) == somevar #: Test timeout=del to delete key render_template("test_template.html", somevar=somevar, timeout="del") for k in testkeys: assert self.cache.get(k) == somevar assert self.cache.get(delkey) is None #: Test rendering templates from strings output = render_template_string( """{% cache 60, "fragment3" %}{{somevar}}{% endcache %}""", somevar=somevar) assert self.cache.get( make_template_fragment_key("fragment3")) == somevar assert output == somevar #: Test backwards compatibility output = render_template_string( """{% cache 30 %}{{somevar}}{% endcache %}""", somevar=somevar) assert self.cache.get( make_template_fragment_key("None1")) == somevar assert output == somevar output = render_template_string( """{% cache 30, "fragment4", "fragment5"%}{{somevar}}{% endcache %}""", somevar=somevar) k = make_template_fragment_key("fragment4", vary_on=["fragment5"]) assert self.cache.get(k) == somevar assert output == somevar
def _cache(self, timeout, fragment_name, vary_on, caller): try: cache = getattr(self.environment, JINJA_CACHE_ATTR_NAME) except AttributeError as e: raise e key = make_template_fragment_key(fragment_name, vary_on=vary_on) #: Delete key if timeout is 'del' if timeout == "del": cache.delete(key) return caller() rv = cache.get(key) if rv is None: rv = caller() cache.set(key, rv, timeout) return rv
def test_20_jinja2ext_cache(self): somevar = ''.join([random.choice(string.ascii_letters) for x in range(6)]) testkeys = [ make_template_fragment_key("fragment1"), make_template_fragment_key("fragment1", vary_on=["key1"]), make_template_fragment_key("fragment1", vary_on=["key1", somevar]), ] delkey = make_template_fragment_key("fragment2") with self.app.test_request_context(): #: Test if elements are cached render_template("test_template.html", somevar=somevar, timeout=60) for k in testkeys: assert self.cache.get(k) == somevar assert self.cache.get(delkey) == somevar #: Test timeout=del to delete key render_template("test_template.html", somevar=somevar, timeout="del") for k in testkeys: assert self.cache.get(k) == somevar assert self.cache.get(delkey) is None #: Test rendering templates from strings output = render_template_string( """{% cache 60, "fragment3" %}{{somevar}}{% endcache %}""", somevar=somevar ) assert self.cache.get(make_template_fragment_key("fragment3")) == somevar assert output == somevar #: Test backwards compatibility output = render_template_string( """{% cache 30 %}{{somevar}}{% endcache %}""", somevar=somevar) assert self.cache.get(make_template_fragment_key("None1")) == somevar assert output == somevar output = render_template_string( """{% cache 30, "fragment4", "fragment5"%}{{somevar}}{% endcache %}""", somevar=somevar) k = make_template_fragment_key("fragment4", vary_on=["fragment5"]) assert self.cache.get(k) == somevar assert output == somevar
def clear_standings(): from CTFd.models import Users, Teams from CTFd.constants.static import CacheKeys from CTFd.utils.scores import get_standings, get_team_standings, get_user_standings from CTFd.api.v1.scoreboard import ScoreboardDetail, ScoreboardList from CTFd.api import api from CTFd.utils.user import ( get_user_score, get_user_place, get_team_score, get_team_place, ) # Clear out the bulk standings functions cache.delete_memoized(get_standings) cache.delete_memoized(get_team_standings) cache.delete_memoized(get_user_standings) # Clear out the individual helpers for accessing score via the model cache.delete_memoized(Users.get_score) cache.delete_memoized(Users.get_place) cache.delete_memoized(Teams.get_score) cache.delete_memoized(Teams.get_place) # Clear the Jinja Attrs constants cache.delete_memoized(get_user_score) cache.delete_memoized(get_user_place) cache.delete_memoized(get_team_score) cache.delete_memoized(get_team_place) # Clear out HTTP request responses cache.delete(make_cache_key(path=api.name + "." + ScoreboardList.endpoint)) cache.delete( make_cache_key(path=api.name + "." + ScoreboardDetail.endpoint)) cache.delete_memoized(ScoreboardList.get) # Clear out scoreboard templates cache.delete(make_template_fragment_key(CacheKeys.PUBLIC_SCOREBOARD_TABLE))
def get_fragment(self, name, vary_on=None): key = make_template_fragment_key(name, vary_on=vary_on or []) return self.get(key)
def get_fragment(self, name, vary_on=None): key = make_template_fragment_key(name, vary_on=vary_on or []) return self.get(key)