Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow featured title to be stored in redis or environment variable
- This allows us to let users set the featured guide more reliably from the CMS UI itself, ONLY if the REDISCLOUD_URL environment variable and caching is enabled. - We'll fallback to storing the featured title in the environment if the REDISCLOUD_URL environment variable is not available.
- Loading branch information
Showing
6 changed files
with
114 additions
and
46 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
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
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
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,75 @@ | ||
""" | ||
Handle storing and retrieving featured article | ||
The FEATURED_TITLE can only be set via the UI when there's an available | ||
persistent storage mechanism like Redis configured. Otherwise the | ||
FEATURED_TITLE is stored in an environment variable that cannot be set from the | ||
UI. We do not allow setting of the environment variable from the UI because | ||
the application could be running on multiple instances so setting a single | ||
environment variable will only affect a single instance. | ||
You can use Heroku's admin panel or CLI to set environment variables for all | ||
instances of your application, if you're running on Heroku without Redis. | ||
""" | ||
|
||
import os | ||
|
||
from .. import PUBLISHED | ||
from .. import cache | ||
from . import get_available_articles | ||
|
||
KEY = 'FEATURED_TITLE' | ||
|
||
|
||
def allow_set_featured_article(): | ||
""" | ||
Return True or False if the ability to set the featured article is allowed | ||
from the UI. If False, then the featured guide should be set via an | ||
environment variable called FEATURED_TITLE. | ||
""" | ||
|
||
return cache.is_enabled() | ||
|
||
|
||
def set_featured_article(title): | ||
""" | ||
Set featured article | ||
:param title: Title of featured article | ||
""" | ||
|
||
# None for timeout b/c this should never expire | ||
cache.save(KEY, title, timeout=None) | ||
|
||
|
||
def get_featured_article(articles=None): | ||
""" | ||
Find featured article in list of articles or published articles | ||
:params articles: List of article objects to search for featured article or | ||
use published articles if no list is given | ||
:returns: Article object of featured article or None if not found | ||
""" | ||
|
||
featured = None | ||
if allow_set_featured_article(): | ||
featured = cache.get(KEY) | ||
|
||
if featured is None: | ||
featured = os.environ.get(KEY) | ||
|
||
if featured is None: | ||
return None | ||
|
||
if articles is None: | ||
# FIXME: This should only fetch the most recent x number. | ||
articles = list(get_available_articles(status=PUBLISHED)) | ||
|
||
featured = featured.strip() | ||
|
||
for article in articles: | ||
# Don't allow surrounding spaces to mismatch | ||
if article.title.strip() == featured: | ||
return article | ||
|
||
return None |
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
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