Cleaned up some blog post viewing code
This commit is contained in:
@@ -128,13 +128,8 @@ def services():
|
|||||||
def main_blog(url=None):
|
def main_blog(url=None):
|
||||||
"""Blog with project news"""
|
"""Blog with project news"""
|
||||||
project_id = current_app.config['MAIN_PROJECT_ID']
|
project_id = current_app.config['MAIN_PROJECT_ID']
|
||||||
|
|
||||||
@current_app.cache.memoize(timeout=3600, unless=current_user_is_authenticated)
|
|
||||||
def cache_post_view(url):
|
|
||||||
return posts_view(project_id, url)
|
return posts_view(project_id, url)
|
||||||
|
|
||||||
return cache_post_view(url)
|
|
||||||
|
|
||||||
|
|
||||||
@blueprint.route('/blog/create')
|
@blueprint.route('/blog/create')
|
||||||
def main_posts_create():
|
def main_posts_create():
|
||||||
@@ -147,18 +142,7 @@ def main_posts_create():
|
|||||||
def project_blog(project_url, url=None):
|
def project_blog(project_url, url=None):
|
||||||
"""View project blog"""
|
"""View project blog"""
|
||||||
|
|
||||||
@current_app.cache.memoize(timeout=3600,
|
return posts_view(project_url=project_url, url=url)
|
||||||
unless=current_user_is_authenticated)
|
|
||||||
def cache_post_view(project_url, url):
|
|
||||||
api = system_util.pillar_api()
|
|
||||||
try:
|
|
||||||
project = Project.find_one({
|
|
||||||
'where': '{"url" : "%s"}' % (project_url)}, api=api)
|
|
||||||
return posts_view(project._id, url=url)
|
|
||||||
except ResourceNotFound:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
return cache_post_view(project_url, url)
|
|
||||||
|
|
||||||
|
|
||||||
def get_projects(category):
|
def get_projects(category):
|
||||||
|
@@ -2,4 +2,7 @@ from .routes import blueprint
|
|||||||
|
|
||||||
|
|
||||||
def setup_app(app, url_prefix=None):
|
def setup_app(app, url_prefix=None):
|
||||||
|
from . import custom
|
||||||
|
|
||||||
|
custom.setup_app(app)
|
||||||
app.register_blueprint(blueprint, url_prefix=url_prefix)
|
app.register_blueprint(blueprint, url_prefix=url_prefix)
|
||||||
|
@@ -1,2 +1,8 @@
|
|||||||
def append_custom_node_endpoints():
|
def append_custom_node_endpoints():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def setup_app(app):
|
||||||
|
from . import posts
|
||||||
|
|
||||||
|
posts.setup_app(app)
|
||||||
|
@@ -8,6 +8,7 @@ from flask_login import login_required, current_user
|
|||||||
from pillar.web.utils import system_util
|
from pillar.web.utils import system_util
|
||||||
from pillar.web.utils import attach_project_pictures
|
from pillar.web.utils import attach_project_pictures
|
||||||
from pillar.web.utils import get_file
|
from pillar.web.utils import get_file
|
||||||
|
from pillar.web.utils import current_user_is_authenticated
|
||||||
|
|
||||||
from pillar.web.nodes.routes import blueprint
|
from pillar.web.nodes.routes import blueprint
|
||||||
from pillar.web.nodes.routes import url_for_node
|
from pillar.web.nodes.routes import url_for_node
|
||||||
@@ -16,28 +17,35 @@ from pillar.web.nodes.forms import process_node_form
|
|||||||
from pillar.web.projects.routes import project_update_nodes_list
|
from pillar.web.projects.routes import project_update_nodes_list
|
||||||
|
|
||||||
|
|
||||||
def posts_view(project_id, url=None):
|
# Cached, see setup_app() below.
|
||||||
|
def posts_view(project_id=None, project_url=None, url=None):
|
||||||
"""View individual blogpost"""
|
"""View individual blogpost"""
|
||||||
|
|
||||||
|
if bool(project_id) == bool(project_url):
|
||||||
|
raise ValueError('posts_view(): pass either project_id or project_url')
|
||||||
|
|
||||||
api = system_util.pillar_api()
|
api = system_util.pillar_api()
|
||||||
|
|
||||||
# Fetch project (for backgroud images and links generation)
|
# Fetch project (for backgroud images and links generation)
|
||||||
|
if project_id:
|
||||||
project = Project.find(project_id, api=api)
|
project = Project.find(project_id, api=api)
|
||||||
|
else:
|
||||||
|
project = Project.find_one({'where': {'url': project_url}}, api=api)
|
||||||
|
project_id = project['_id']
|
||||||
|
|
||||||
attach_project_pictures(project, api)
|
attach_project_pictures(project, api)
|
||||||
try:
|
|
||||||
blog = Node.find_one({
|
blog = Node.find_one({
|
||||||
'where': {'node_type': 'blog', 'project': project_id},
|
'where': {'node_type': 'blog', 'project': project_id},
|
||||||
}, api=api)
|
}, api=api)
|
||||||
except ResourceNotFound:
|
|
||||||
abort(404)
|
|
||||||
if url:
|
if url:
|
||||||
try:
|
|
||||||
post = Node.find_one({
|
post = Node.find_one({
|
||||||
'where': '{"parent": "%s", "properties.url": "%s"}' % (blog._id, url),
|
'where': {'parent': blog._id, 'properties.url': url},
|
||||||
'embedded': '{"node_type": 1, "user": 1}',
|
'embedded': {'node_type': 1, 'user': 1},
|
||||||
}, api=api)
|
}, api=api)
|
||||||
if post.picture:
|
if post.picture:
|
||||||
post.picture = get_file(post.picture, api=api)
|
post.picture = get_file(post.picture, api=api)
|
||||||
except ResourceNotFound:
|
|
||||||
return abort(404)
|
|
||||||
|
|
||||||
# If post is not published, check that the user is also the author of
|
# If post is not published, check that the user is also the author of
|
||||||
# the post. If not, return 404.
|
# the post. If not, return 404.
|
||||||
@@ -165,3 +173,11 @@ def posts_edit(post_id):
|
|||||||
form=form,
|
form=form,
|
||||||
project=project,
|
project=project,
|
||||||
api=api)
|
api=api)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def setup_app(app):
|
||||||
|
global posts_view
|
||||||
|
|
||||||
|
memoize = app.cache.memoize(timeout=3600, unless=current_user_is_authenticated)
|
||||||
|
posts_view = memoize(posts_view)
|
||||||
|
Reference in New Issue
Block a user