Skip to content

Commit

Permalink
LBSD-2278 Updates the blog limits in frontend after deleted posts and…
Browse files Browse the repository at this point in the history
… remove old ones
  • Loading branch information
eos87 committed Nov 21, 2018
1 parent f675157 commit 082ad0e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 38 deletions.
9 changes: 9 additions & 0 deletions client/app/scripts/liveblog-edit/controllers/blog-edit.js
Expand Up @@ -687,4 +687,13 @@ export default function BlogEditController(
$scope.ingestQueue.queue = $scope.ingestQueue.queue.concat(syndPosts);
}
});

// receives update data of the blog from backend.
// for we will just update total posts and posts limit
$scope.$on('blog:limits', (event, data) => {
if (blog._id === data.blog_id) {
blog['posts_limit'] = data.stats['posts_limit'];
blog['total_posts'] = data.stats['total_posts'];
}
});
}
20 changes: 3 additions & 17 deletions server/liveblog/blogs/tasks.py
Expand Up @@ -19,7 +19,7 @@
from .embeds import embed, render_bloglist_embed
from .exceptions import MediaStorageUnsupportedForBlogPublishing
from .utils import (check_media_storage, get_blog_path,
get_bloglist_path, is_s3_storage_enabled)
get_bloglist_path, is_s3_storage_enabled, get_blog)

logger = logging.getLogger('superdesk')

Expand Down Expand Up @@ -153,23 +153,9 @@ def _publish_blog_embed_on_s3(blog_or_id, theme=None, output=None, safe=True, sa
return public_url, public_urls


def _get_blog(blog_or_id):
blogs = get_resource_service('client_blogs')
if isinstance(blog_or_id, (str, ObjectId)):
blog_id = blog_or_id
blog = blogs.find_one(req=None, _id=blog_or_id)
elif isinstance(blog_or_id, dict):
blog = blog_or_id
blog_id = blog['_id']
else:
raise ValueError(blog_or_id)

return blog_id, blog


@celery.task(soft_time_limit=1800)
def publish_blog_embed_on_s3(blog_or_id, theme=None, output=None, safe=True, save=True):
blog_id, blog = _get_blog(blog_or_id)
blog_id, blog = get_blog(blog_or_id)
if not blog:
logger.warning('publish_blog_on_s3 for blog "{}" not started: blog not found'.format(blog_id))
return
Expand All @@ -188,7 +174,7 @@ def publish_blog_embed_on_s3(blog_or_id, theme=None, output=None, safe=True, sav
@celery.task(soft_time_limit=1800)
def publish_blog_embeds_on_s3(blog_or_id, safe=True, save=True, subtask_save=False):
blogs = get_resource_service('client_blogs')
blog_id, blog = _get_blog(blog_or_id)
blog_id, blog = get_blog(blog_or_id)
if not blog:
logger.warning('publish_blog_on_s3 for blog "{}" not started: blog not found'.format(blog_id))
return
Expand Down
50 changes: 41 additions & 9 deletions server/liveblog/blogs/utils.py
Expand Up @@ -4,6 +4,7 @@
from bson.objectid import ObjectId
from flask import current_app as app
from superdesk import get_resource_service
from superdesk.notification import push_notification

from .app_settings import BLOGSLIST_ASSETS_DIR
from .exceptions import MediaStorageUnsupportedForBlogPublishing
Expand Down Expand Up @@ -34,19 +35,27 @@ def is_relative_to_current_folder(url):
return not (url.startswith('/') or url.startswith('http://') or url.startswith('https://'))


def is_seo_enabled(blog_or_id):
"""
Return True if blog has seo output enabled in theme.
"""
def get_blog(blog_or_id):
blogs = get_resource_service('client_blogs')
if isinstance(blog_or_id, dict):
if isinstance(blog_or_id, (str, ObjectId)):
blog_id = blog_or_id
blog = blogs.find_one(req=None, _id=blog_or_id)
elif isinstance(blog_or_id, dict):
blog = blog_or_id
blog_id = blog['_id']
else:
blog_id = blog_or_id
blog = blogs.find_one(req=None, _id=blog_id)
if not blog:
return
raise ValueError(blog_or_id)

return blog_id, blog


def is_seo_enabled(blog_or_id):
"""
Return True if blog has seo output enabled in theme.
"""
blog_id, blog = get_blog(blog_or_id)
if not blog:
return

themes = get_resource_service('themes')
blog_preferences = blog.get('blog_preferences')
Expand Down Expand Up @@ -100,3 +109,26 @@ def check_limit_and_delete_oldest(blog_id):
logger.warning(
'Deleted oldest post `%s` because posts_limit (%s) in blog `%s` has been reached'
% (doc['_id'], blog['posts_limit'], blog['_id']))

stats = get_blog_stats(blog_id)
push_notification('blog:limits', blog_id=blog_id, stats=stats)


def get_blog_stats(blog_or_id):
"""
This find some specific information about the given blog
like posts_limit, total_posts and returns it in a dictionary
"""
posts = get_resource_service('posts')

blog_id, blog = get_blog(blog_or_id)
if not blog:
return

total_posts = posts.find({'$and': [
{'blog': blog_id},
{'post_status': 'open'},
{'deleted': False}
]}).count()

return {'total_posts': total_posts, 'posts_limit': blog['posts_limit']}
8 changes: 6 additions & 2 deletions server/liveblog/posts/posts.py
Expand Up @@ -15,7 +15,7 @@
from superdesk import get_resource_service
from liveblog.common import check_comment_length

from ..blogs.utils import check_limit_and_delete_oldest
from ..blogs.utils import check_limit_and_delete_oldest, get_blog_stats
from .tasks import update_post_blog_data, update_post_blog_embed


Expand Down Expand Up @@ -304,7 +304,8 @@ def on_updated(self, updates, original):
super().on_updated(updates, original)
out_service = get_resource_service('syndication_out')
# invalidate cache for updated blog
app.blog_cache.invalidate(original.get('blog'))
blog_id = original.get('blog')
app.blog_cache.invalidate(blog_id)
doc = original.copy()
doc.update(updates)
posts = []
Expand All @@ -317,6 +318,9 @@ def on_updated(self, updates, original):
out_service.send_syndication_post(original, action='deleted')
# Push notification.
push_notification('posts', deleted=True, post_id=original.get('_id'))

stats = get_blog_stats(blog_id)
push_notification('blog:limits', blog_id=blog_id, stats=stats)
else:
# Update blog post data and embed for SEO-enabled blogs.
update_post_blog_data.delay(doc, action='updated')
Expand Down
13 changes: 3 additions & 10 deletions server/liveblog/posts/tasks.py
Expand Up @@ -4,7 +4,7 @@
from superdesk.celery_app import celery
from celery.exceptions import SoftTimeLimitExceeded
from liveblog.blogs.tasks import publish_blog_embeds_on_s3
from liveblog.blogs.utils import is_seo_enabled
from liveblog.blogs.utils import is_seo_enabled, get_blog_stats
from eve.io.base import DataLayer

logger = logging.getLogger('superdesk')
Expand All @@ -20,7 +20,6 @@ def update_post_blog_data(post, action='created'):
:return: None
"""
blogs = get_resource_service('client_blogs')
posts = get_resource_service('posts')
blog_id = post.get('blog')
if not blog_id:
return
Expand All @@ -30,14 +29,8 @@ def update_post_blog_data(post, action='created'):
return

# Fetch total posts.
total_posts = posts.find({'$and': [
{'blog': blog_id},
{'post_status': 'open'},
{'deleted': False}
]}).count()
updates = {
'total_posts': total_posts,
}
stats = get_blog_stats(blog_id)
updates = {'total_posts': stats['total_posts']}

if action in ('updated', 'created'):
# Update last_updated_post or last_created_post.
Expand Down

0 comments on commit 082ad0e

Please sign in to comment.