def ajax(target_attr, target, link, page, per_page, template_pagination, last_viewed_comment=None, extra_data=None): if not target.bl.has_comments_access(current_user._get_current_object()): abort(403) maxdepth = None if request.args.get('fulltree') == '1' else calc_maxdepth(current_user) comments_count, paged, comments_tree_list = target.bl.paginate_comments(page, per_page, maxdepth, last_viewed_comment=last_viewed_comment) if not comments_tree_list and paged.number != 1: abort(404) comment_ids = [x[0].id for x in comments_tree_list] if current_user.is_authenticated: comment_votes_cache = target.bl.select_comment_votes(current_user._get_current_object(), comment_ids) else: comment_votes_cache = {i: 0 for i in comment_ids} data = dict(extra_data or {}) data.update({ target_attr: target, 'comments_tree_list': comments_tree_list, 'last_viewed_comment': last_viewed_comment, 'page_obj': paged, 'comment_votes_cache': comment_votes_cache, }) return jsonify({ 'success': True, 'link': link, 'comments_count': comments_count, 'comments_tree': render_template('includes/comments_tree.html', **data), 'pagination': render_template(template_pagination, **data), })
def view(story_id, comments_page): user = current_user._get_current_object() local = get_local_thread(story_id, user) story = local.story per_page = user.comments_per_page or current_app.config['COMMENTS_COUNT']['page'] maxdepth = None if request.args.get('fulltree') == '1' else calc_maxdepth(user) last_viewed_comment = story.bl.last_viewed_local_comment_by(user) comments_count, paged, comments_tree_list = local.bl.paginate_comments(comments_page, per_page, maxdepth, last_viewed_comment=last_viewed_comment) if not comments_tree_list and paged.number != 1: abort(404) if comments_page < 0 or comments_page == paged.num_pages: story.bl.viewed_localcomments(user) data = { 'story': story, 'local': local, 'comments_tree_list': comments_tree_list, 'comments_count': comments_count, 'last_viewed_comment': last_viewed_comment, 'page_title': 'Обсуждение', 'comment_form': CommentForm(), 'page_obj': paged, 'sub_comments': story.bl.get_local_comments_subscription(user), } return render_template('story_localcomments.html', **data)
def view(story_id, comments_page): user = current_user._get_current_object() local = get_local_thread(story_id, user) story = local.story per_page = user.comments_per_page or current_app.config['COMMENTS_COUNT'][ 'page'] maxdepth = None if request.args.get('fulltree') == '1' else calc_maxdepth( user) last_viewed_comment = story.bl.last_viewed_local_comment_by(user) comments_count, paged, comments_tree_list = local.bl.paginate_comments( comments_page, per_page, maxdepth, last_viewed_comment=last_viewed_comment) if not comments_tree_list and paged.number != 1: abort(404) if comments_page < 0 or comments_page == paged.num_pages: story.bl.viewed_localcomments(user) data = { 'story': story, 'local': local, 'comments_tree_list': comments_tree_list, 'comments_count': comments_count, 'last_viewed_comment': last_viewed_comment, 'page_title': 'Обсуждение', 'comment_form': CommentForm(), 'page_obj': paged, 'sub_comments': story.bl.get_local_comments_subscription(user), } return render_template('story_localcomments.html', **data)
def show(name, comments_page): newsitem = NewsItem.get(name=name) if not newsitem: abort(404) if newsitem.is_template: template = current_app.jinja_env.from_string(newsitem.content) template.name = 'db/news/{}.html'.format(name) content = render_template(template, newsitem_name=newsitem.name, newsitem_title=newsitem.title) else: content = newsitem.content per_page = current_user.comments_per_page or current_app.config[ 'COMMENTS_COUNT']['page'] maxdepth = None if request.args.get('fulltree') == '1' else calc_maxdepth( current_user) comments_count, paged, comments_tree_list = newsitem.bl.paginate_comments( comments_page, per_page, maxdepth) paged.page_arg_name = 'comments_page' if not comments_tree_list and paged.number != 1: abort(404) comment_ids = [x[0].id for x in comments_tree_list] if current_user.is_authenticated: comment_votes_cache = newsitem.bl.select_comment_votes( current_user._get_current_object(), comment_ids) else: comment_votes_cache = {i: 0 for i in comment_ids} data = { 'page_title': newsitem.title, 'newsitem': newsitem, 'content': Markup(content), 'comments_count': comments_count, 'page_obj': paged, 'comments_tree_list': comments_tree_list, 'comment_form': CommentForm(), 'comment_votes_cache': comment_votes_cache, 'sub_comments': newsitem.bl.get_comments_subscription( current_user._get_current_object()), } return render_template('news/show.html', **data)
def get_paged_link(self, for_user=None, check_tree=True, _external=False): c = self.model page = self.get_page_number(for_user=for_user) result = url_for('news.show', name=c.newsitem.name, comments_page=page, _external=_external) if check_tree: maxdepth = calc_maxdepth(for_user) if maxdepth is not None and c.tree_depth > maxdepth: result += '?fulltree=1' return result + '#' + str(c.local_id)
def get_paged_link(self, for_user=None, check_tree=True, _external=False): c = self.model page = self.get_page_number(for_user=for_user) result = url_for('story_local_comment.view', story_id=c.local.story.id, comments_page=page, _external=_external) if check_tree: maxdepth = calc_maxdepth(for_user) if maxdepth is not None and c.tree_depth > maxdepth: result += '?fulltree=1' return result + '#' + str(c.local_id)
def show(name, comments_page): newsitem = NewsItem.get(name=name) if not newsitem: abort(404) if newsitem.is_template: template = current_app.jinja_env.from_string(newsitem.content) template.name = 'db/news/{}.html'.format(name) content = render_template(template, newsitem_name=newsitem.name, newsitem_title=newsitem.title) else: content = newsitem.content per_page = current_user.comments_per_page or current_app.config['COMMENTS_COUNT']['page'] maxdepth = None if request.args.get('fulltree') == '1' else calc_maxdepth(current_user) comments_count, paged, comments_tree_list = newsitem.bl.paginate_comments(comments_page, per_page, maxdepth) paged.page_arg_name = 'comments_page' if not comments_tree_list and paged.number != 1: abort(404) comment_ids = [x[0].id for x in comments_tree_list] if current_user.is_authenticated: comment_votes_cache = newsitem.bl.select_comment_votes(current_user._get_current_object(), comment_ids) else: comment_votes_cache = {i: 0 for i in comment_ids} data = { 'page_title': newsitem.title, 'newsitem': newsitem, 'content': Markup(content), 'comments_count': comments_count, 'page_obj': paged, 'comments_tree_list': comments_tree_list, 'comment_form': CommentForm(), 'comment_votes_cache': comment_votes_cache, 'sub_comments': newsitem.bl.get_comments_subscription(current_user._get_current_object()), } return render_template('news/show.html', **data)
def view(pk, comments_page): user = current_user._get_current_object() story = get_story(pk) per_page = user.comments_per_page or current_app.config['COMMENTS_COUNT'][ 'page'] maxdepth = None if request.args.get('fulltree') == '1' else calc_maxdepth( user) last_viewed_comment = story.bl.last_viewed_comment_by(user) comments_count, paged, comments_tree_list = story.bl.paginate_comments( comments_page, per_page, maxdepth, last_viewed_comment=last_viewed_comment) if not comments_tree_list and paged.number != 1: abort(404) act = story.bl.get_activity(user) local_comments_count = 0 new_local_comments_count = 0 if story.local and ((user and user.is_staff) or story.bl.is_contributor(user)): local_comments_count = story.bl.get_or_create_local_thread( ).comments_count new_local_comments_count = ( (story.bl.get_or_create_local_thread().comments_count - act.last_local_comments) if act else 0) chapters = list(story.chapters.select().order_by(Chapter.order, Chapter.id)) if not user.is_staff and not story.bl.is_contributor(user): chapters = [x for x in chapters if not x.draft] show_first_chapter = len(chapters) == 1 and not chapters[0].draft if user.is_authenticated: story.bl.viewed(user) if show_first_chapter: chapters[0].bl.viewed(user) user_vote = story.votes.select(lambda x: x.author == user).first() else: user_vote = None comment_ids = [x[0].id for x in comments_tree_list] if user.is_authenticated: comment_votes_cache = story.bl.select_comment_votes(user, comment_ids) else: comment_votes_cache = {i: 0 for i in comment_ids} chapter_subscriptions_count = Subscription.select().filter( lambda x: x.type == 'story_chapter' and x.target_id == story.id).count( ) data = { 'story': story, 'contributors': story.bl.get_contributors_for_view(), 'vote': user_vote, 'author_ids': [x.id for x in story.authors], 'comments_tree_list': comments_tree_list, 'comments_count': comments_count, 'comment_votes_cache': comment_votes_cache, 'last_viewed_comment': last_viewed_comment, 'local_comments_count': local_comments_count, 'new_local_comments_count': new_local_comments_count, 'chapters': chapters, 'show_first_chapter': show_first_chapter, 'page_title': story.title, 'comment_form': CommentForm(), 'page_obj': paged, 'sub': story.bl.get_subscription(user), 'sub_comments': story.bl.get_comments_subscription(user), 'robots_noindex': not story.published or story.robots_noindex, 'favorites_count': story.favorites.select().count(), 'show_meta_description': comments_page == -1, 'chapter_subscriptions_count': chapter_subscriptions_count, } return render_template('story_view.html', **data)
def view(pk, comments_page): user = current_user._get_current_object() story = get_story(pk) per_page = user.comments_per_page or current_app.config['COMMENTS_COUNT']['page'] maxdepth = None if request.args.get('fulltree') == '1' else calc_maxdepth(user) last_viewed_comment = story.bl.last_viewed_comment_by(user) comments_count, paged, comments_tree_list = story.bl.paginate_comments(comments_page, per_page, maxdepth, last_viewed_comment=last_viewed_comment) if not comments_tree_list and paged.number != 1: abort(404) act = story.bl.get_activity(user) local_comments_count = 0 new_local_comments_count = 0 if story.local and ((user and user.is_staff) or story.bl.is_contributor(user)): local_comments_count = story.bl.get_or_create_local_thread().comments_count new_local_comments_count = ( (story.bl.get_or_create_local_thread().comments_count - act.last_local_comments) if act else 0 ) chapters = list(story.chapters.select().order_by(Chapter.order, Chapter.id)) if not user.is_staff and not story.bl.is_contributor(user): chapters = [x for x in chapters if not x.draft] show_first_chapter = len(chapters) == 1 and not chapters[0].draft if user.is_authenticated: story.bl.viewed(user) if show_first_chapter: chapters[0].bl.viewed(user) user_vote = story.votes.select(lambda x: x.author == user).first() else: user_vote = None comment_ids = [x[0].id for x in comments_tree_list] if user.is_authenticated: comment_votes_cache = story.bl.select_comment_votes(user, comment_ids) else: comment_votes_cache = {i: 0 for i in comment_ids} chapter_subscriptions_count = Subscription.select().filter( lambda x: x.type == 'story_chapter' and x.target_id == story.id ).count() data = { 'story': story, 'contributors': story.bl.get_contributors_for_view(), 'vote': user_vote, 'author_ids': [x.id for x in story.authors], 'comments_tree_list': comments_tree_list, 'comments_count': comments_count, 'comment_votes_cache': comment_votes_cache, 'last_viewed_comment': last_viewed_comment, 'local_comments_count': local_comments_count, 'new_local_comments_count': new_local_comments_count, 'chapters': chapters, 'show_first_chapter': show_first_chapter, 'page_title': story.title, 'comment_form': CommentForm(), 'page_obj': paged, 'sub': story.bl.get_subscription(user), 'sub_comments': story.bl.get_comments_subscription(user), 'robots_noindex': not story.published or story.robots_noindex, 'favorites_count': story.favorites.select().count(), 'show_meta_description': comments_page == -1, 'chapter_subscriptions_count': chapter_subscriptions_count, } return render_template('story_view.html', **data)