-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2023 CERN. | ||
# | ||
# ZenodoRDM is free software; you can redistribute it and/or modify it | ||
# under the terms of the MIT License; see LICENSE file for more details. | ||
"""Decorators.""" | ||
|
||
|
||
from functools import wraps | ||
|
||
from invenio_cache import current_cache | ||
from flask import session | ||
from flask_login import current_user | ||
|
||
|
||
def has_flashes_or_authenticated_user(): | ||
"""Return True if there are pending flashes or user is authenticated.""" | ||
return "_flashes" in session or current_user.is_authenticated | ||
|
||
|
||
def cached_unless_authenticated_or_flashes(timeout=50, key_prefix="default"): | ||
"""Cache anonymous traffic.""" | ||
def caching(f): | ||
@wraps(f) | ||
def wrapper(*args, **kwargs): | ||
cache_fun = current_cache.cached( | ||
timeout=timeout, key_prefix=key_prefix, | ||
unless=has_flashes_or_authenticated_user) | ||
return cache_fun(f)(*args, **kwargs) | ||
return wrapper | ||
return caching |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters