def test_title_in_metadata(self): user = self.user title = "here's a title for this comment" cmt = create_comment(title=title) ctx = self._ctx(user, cmt) cmt = TemplateComment(cmt.details(), request_context=ctx) self.assertTrue(strip_template_chars(title) in cmt.shareable_metadata()['title'])
def thread_new_comment(context, comment, is_op=False, is_current=False, is_first_reply_with_content=False, is_last_reply_with_content=False, ignore_lazy_load=False): from canvas.templatetags.helpers import TemplateComment from canvas.models import CommentSticker request = context['request'] comment = TemplateComment(comment, request_context=context, is_op=is_op) try: sticker_name = comment.top_sticker()['name'] except TypeError: sticker_name = None try: viewer_following_thread = request.user.redis.followed_threads.sismember(comment.id) except AttributeError: viewer_following_thread = False ctx = { 'request': request, 'short_id': context.get('short_id'), 'comment': comment, 'can_edit_tags': request.user.is_staff or comment.is_author(request.user), 'lazy_content': features.lazy_content(request), 'is_current':is_current, 'ignore_lazy_load': ignore_lazy_load, 'is_first_reply_with_content': is_first_reply_with_content, 'is_last_reply_with_content': is_last_reply_with_content, 'my_post': comment.is_author(request.user), 'stickered_by_viewer': bool(CommentSticker.get_sticker_from_user(comment.id, request.user)), 'sticker_name': sticker_name, 'has_content': bool(comment.reply_content_id), 'viewer_following_thread': viewer_following_thread, } return Markup(render_jinja_to_string('threads_new/_thread_comment.html', ctx))
def test_anonymous_user_url_when_staff(self): user = self.user author = create_user() cmt = create_comment(author=author, anonymous=True) ctx = self._ctx(user, cmt) cmt = TemplateComment(cmt.details(), request_context=ctx) url = cmt.get_user_url() self.assertEqual(url, reverse('user_link', args=[author.username]))
def _metadata(self, **cmt_kwargs): group = create_group() cmt = TemplateComment( self.post_comment(category=group.name, **cmt_kwargs).details()) return ( cmt, cmt.shareable_metadata(), )
def test_title_in_metadata(self): user = self.user title = "here's a title for this comment" cmt = create_comment(title=title) ctx = self._ctx(user, cmt) cmt = TemplateComment(cmt.details(), request_context=ctx) self.assertTrue( strip_template_chars(title) in cmt.shareable_metadata()['title'])
def thread_new_comment(context, comment, is_op=False, is_current=False, is_first_reply_with_content=False, is_last_reply_with_content=False, ignore_lazy_load=False): from canvas.templatetags.helpers import TemplateComment from canvas.models import CommentSticker request = context['request'] comment = TemplateComment(comment, request_context=context, is_op=is_op) try: sticker_name = comment.top_sticker()['name'] except TypeError: sticker_name = None try: viewer_following_thread = request.user.redis.followed_threads.sismember( comment.id) except AttributeError: viewer_following_thread = False ctx = { 'request': request, 'short_id': context.get('short_id'), 'comment': comment, 'can_edit_tags': request.user.is_staff or comment.is_author(request.user), 'lazy_content': features.lazy_content(request), 'is_current': is_current, 'ignore_lazy_load': ignore_lazy_load, 'is_first_reply_with_content': is_first_reply_with_content, 'is_last_reply_with_content': is_last_reply_with_content, 'my_post': comment.is_author(request.user), 'stickered_by_viewer': bool(CommentSticker.get_sticker_from_user(comment.id, request.user)), 'sticker_name': sticker_name, 'has_content': bool(comment.reply_content_id), 'viewer_following_thread': viewer_following_thread, } return Markup( render_jinja_to_string('threads_new/_thread_comment.html', ctx))
def explore_tile(context, tile, render_options={}, nav_category={}): comment = TemplateComment(tile.comment, request_context=context) sticky_text = getattr(tile, 'text', None) viewer_sticker = tile.viewer_sticker viewer = context['request'].user remixer_count = comment.thread.author_count return Markup(render_jinja_to_string('/comment/explore_tile.html', locals()))
def image_tile(context, tile, render_options, nav_category, template='comment/explore_tile.html'): request = context['request'] user = request.user if not hasattr(tile, 'comment'): raise TypeError("A tile should be an instance of TileDetails or LastReplyTileDetails; something that " "has a .comment. Received a %s" % type(tile)) comment = tile.comment new_activity = False if render_options.get('show_activity') and render_options.get('show_pins') and comment.last_reply_time: pinned_lastviewed = user.kv.pinned_lastviewed.get() or 1303426306. # When we launched pinned. new_activity = pinned_lastviewed < float(comment.last_reply_time) def nav_aware(url): default_category = Category.get_default(request.user).details() # If we don't have a nav_category (user / about page) it should be the default for this user. _nav_category = nav_category or default_category if _nav_category['name'] != default_category['name']: url += "?nav=%s" % _nav_category['name'] return url #TODO probably change TemplateComment to TemplateTile instead. # This is weird - we pass tile to the template too, but tile.comment != comment now. comment = TemplateComment(tile.comment, request_context=context) float_sticker = False sticky_text = getattr(tile, 'text', None) return render_jinja_to_string(template, locals())
def jinja_thread_comment(context, comment_details, fullsize): context.update({ 'comment' : TemplateComment(comment_details, request_context=context, is_op=bool(fullsize)), 'fullsize' : fullsize, 'show_tags' : True, }) return render_jinja_to_string('comment/jinja_thread_comment.html', context)
def monster_image_tile(context, tile, render_options={}, nav_category={}, display_monster=True): request = context['request'] user = request.user viewer_sticker = tile.viewer_sticker top = TemplateComment(tile.top, request_context=context) bottom = TemplateComment(tile.bottom, request_context=context) float_sticker = False template = 'monster/monster_explore_tile.html' return Markup(render_jinja_to_string(template, locals()))
def jinja_thread_reply(context, comment_details, template='threads/reply.html', is_expanded=False): opts = { 'reply': TemplateComment(comment_details, request_context=context), 'is_expanded': is_expanded, } return Markup( render_jinja_to_string(template, update_context(context, opts)))
def thread_new_comment( context, comment, is_op=False, is_current=False, is_first_reply_with_content=False, is_last_reply_with_content=False, ignore_lazy_load=False, ): from canvas.templatetags.helpers import TemplateComment from canvas.models import CommentSticker request = context["request"] comment = TemplateComment(comment, request_context=context, is_op=is_op) try: sticker_name = comment.top_sticker()["name"] except TypeError: sticker_name = None try: viewer_following_thread = request.user.redis.followed_threads.sismember(comment.id) except AttributeError: viewer_following_thread = False ctx = { "request": request, "comment": comment, "can_edit_tags": request.user.is_staff or comment.is_author(request.user), "lazy_content": features.lazy_content(request), "is_current": is_current, "ignore_lazy_load": ignore_lazy_load, "is_first_reply_with_content": is_first_reply_with_content, "is_last_reply_with_content": is_last_reply_with_content, "my_post": comment.is_author(request.user), "stickered_by_viewer": bool(CommentSticker.get_sticker_from_user(comment.id, request.user)), "sticker_name": sticker_name, "has_content": bool(comment.reply_content_id), "viewer_following_thread": viewer_following_thread, } return Markup(render_jinja_to_string("threads_new/_thread_comment.html", ctx))
def test_timestamp_url(self): user = self.user cmt = create_comment() ctx = self._ctx(user, cmt) cmt = TemplateComment(cmt.details(), request_context=ctx) self.assertTrue(cmt.timestamp_link_url())
def _metadata(self, **cmt_kwargs): group = create_group() cmt = TemplateComment(self.post_comment(category=group.name, **cmt_kwargs).details()) return (cmt, cmt.shareable_metadata(),)
def thread_context(self): top_reply_ids = self.top_reply_ids(force_show=features.thread_new(self.request)) ctx = { 'short_id': self.short_id, 'page': self.page, 'gotoreply': self.gotoreply, 'viewer_is_staff': self.request.user.is_authenticated() and self.request.user.is_staff, 'viewer_is_thread_author': self.is_author, 'root': '/p/', 'op_content': self.op_content, 'op_category': self.op_category, 'page': self.page, 'per_page': self.per_page, 'num_replies': self.num_replies, 'reply_ids': self.reply_ids, 'pages': self.pages, 'page_reply_ids': self.page_reply_ids, 'page_current': self.page_current, 'page_next': self.page_next, 'page_last': self.page_last, 'page_penultimate': self.page_penultimate, 'explicit_page_view': self.explicit_page_view, # Recent replies. 'recent_reply_ids': self.recent_reply_ids, 'top_reply_ids': top_reply_ids, } # Get all the replies in one query, then create the appropriate lists. _all_replies = Comment.visible.in_bulk(self.page_reply_ids + self.recent_reply_ids + top_reply_ids) _recent_replies = [_all_replies[cid] for cid in self.recent_reply_ids] _top_replies = filter(bool, [_all_replies.get(cid) for cid in top_reply_ids]) _replies = [_all_replies[cid] for cid in self.page_reply_ids] replyable = [self._op_comment] + _replies + _recent_replies + _top_replies # Get all comment ids (ids so 0 queries) that any of these comments are replies to, that aren't in this # page, so we can render them on hover. replied_ids = ([reply.replied_comment_id for reply in replyable if (reply.replied_comment_id and reply.replied_comment_id not in [r.id for r in replyable])]) ctx.update({ 'replied_ids': replied_ids, 'replyable': replyable, }) recent_replies = [reply.details for reply in _recent_replies] replies = [Comment.details_by_id(reply.id) for reply in _replies] replied = [Comment.details_by_id(cid) for cid in replied_ids] top_replies = [reply.details for reply in _top_replies] if self.request.user.is_authenticated(): ctx['user_infos'] = {'pinned': self.request.user.id in self._op_comment.pins()} if self.request.user.is_staff: ctx['admin_infos'] = {self._op_comment.id: self._op_comment.admin_info} # For replies we only use the username, so grab those all in one query and put them in admin_infos. ctx['usernames'] = Comment.visible.filter(id__in=_all_replies.keys()).values('id', 'author__username') for reply_dict in ctx['usernames']: ctx['admin_infos'][reply_dict['id']] = {'username': reply_dict['author__username']} ctx['replies_channel'] = self._op_comment.replies_channel.sync() # Get relevant sidebar data remix_of, reply_to = [], [] # Remix of if self._op_content and self._op_content.remix_of: op_remix_of_caption = self._op_content.remix_of.first_caption if op_remix_of_caption: remix_of = [op_remix_of_caption.details] ctx['op_remix_of_caption'] = op_remix_of_caption # Reply to if self._op_comment.parent_comment and self._op_comment.parent_comment.is_visible(): reply_to = [self._op_comment.parent_comment.details] ( (op_comment,), (linked_comment,), remix_of, reply_to, replies, recent_replies, top_replies, replied, ) = CachedCall.many_multicall( [self.op_comment], [self.linked_comment], remix_of, reply_to, replies, recent_replies, top_replies, replied, ) op_comment.is_my_post = bool(self._op_comment.author == self.request.user) op_comment.moderated = op_comment.visibility not in Visibility.public_choices linked_comment = TemplateComment(linked_comment, is_op=(linked_comment.id == op_comment.id), request=self.request, title=op_comment.title) if self.page_current == 1 and op_comment.has_content(): first_comment_with_content = op_comment else: first_comment_with_content = None for reply in replies: if reply.has_content(): first_comment_with_content = reply break last_comment_with_content = None for reply in reversed(replies): if reply.has_content(): last_comment_with_content = reply break comment_to_expand = first_comment_with_content if self.gotoreply: comment_to_expand = linked_comment ctx.update({ 'op_comment': op_comment, 'linked_comment': linked_comment, 'remix_of': remix_of, 'reply_to': reply_to, 'replies': replies, 'recent_replies': recent_replies, 'top_replies': top_replies, 'top_remixes': [reply for reply in top_replies if reply.has_content()], 'replied': replied, 'linked_comment': linked_comment, 'large_thread_view': len(replies) >= 50, 'title': getattr(op_comment, 'title', None), 'first_comment_with_content': first_comment_with_content, 'last_comment_with_content': last_comment_with_content, 'comment_to_expand': comment_to_expand, }) return ctx
def thread_context(self): top_reply_ids = self.top_reply_ids(force_show=features.thread_new(self.request)) ctx = { 'short_id': self.short_id, 'page': self.page, 'gotoreply': self.gotoreply, 'viewer_is_staff': self.request.user.is_authenticated() and self.request.user.is_staff, 'viewer_is_thread_author': self.is_author, 'root': '/p/', 'op_content': self.op_content, 'op_category': self.op_category, 'page': self.page, 'per_page': self.per_page, 'num_replies': self.num_replies, 'reply_ids': self.reply_ids, 'pages': self.pages, 'page_reply_ids': self.page_reply_ids, 'page_current': self.page_current, 'page_next': self.page_next, 'page_last': self.page_last, 'page_penultimate': self.page_penultimate, 'explicit_page_view': self.explicit_page_view, # Recent replies. 'recent_reply_ids': self.recent_reply_ids, 'top_reply_ids': top_reply_ids, } # Get all the replies in one query, then create the appropriate lists. _all_replies = Comment.visible.in_bulk(self.page_reply_ids + self.recent_reply_ids + top_reply_ids) _recent_replies = [_all_replies[cid] for cid in self.recent_reply_ids] _top_replies = filter(bool, [_all_replies.get(cid) for cid in top_reply_ids]) _replies = [_all_replies[cid] for cid in self.page_reply_ids] replyable = [self._op_comment] + _replies + _recent_replies + _top_replies # Get all comment ids (ids so 0 queries) that any of these comments are replies to, that aren't in this # page, so we can render them on hover. replied_ids = ([reply.replied_comment_id for reply in replyable if (reply.replied_comment_id and reply.replied_comment_id not in [r.id for r in replyable])]) ctx.update({ 'replied_ids': replied_ids, 'replyable': replyable, }) recent_replies = [reply.details for reply in _recent_replies] replies = [Comment.details_by_id(reply.id) for reply in _replies] replied = [Comment.details_by_id(cid) for cid in replied_ids] top_replies = [reply.details for reply in _top_replies] if self.request.user.is_authenticated(): ctx['user_infos'] = {'pinned': self.request.user.id in self._op_comment.pins()} if self.request.user.is_staff: ctx['admin_infos'] = {self._op_comment.id: self._op_comment.admin_info} # For replies we only use the username, so grab those all in one query and put them in admin_infos. ctx['usernames'] = Comment.visible.filter(id__in=_all_replies.keys()).values('id', 'author__username') for reply_dict in ctx['usernames']: ctx['admin_infos'][reply_dict['id']] = {'username': reply_dict['author__username']} ctx['replies_channel'] = self._op_comment.replies_channel.sync() # Get relevant sidebar data remix_of, reply_to = [], [] # Remix of if self._op_content and self._op_content.remix_of: op_remix_of_caption = self._op_content.remix_of.first_caption if op_remix_of_caption: remix_of = [op_remix_of_caption.details] ctx['op_remix_of_caption'] = op_remix_of_caption # Reply to if self._op_comment.parent_comment and self._op_comment.parent_comment.is_visible(): reply_to = [self._op_comment.parent_comment.details] ( (op_comment,), (linked_comment,), remix_of, reply_to, replies, recent_replies, top_replies, replied, ) = CachedCall.many_multicall( [self.op_comment], [self.linked_comment], remix_of, reply_to, replies, recent_replies, top_replies, replied, ) op_comment.is_my_post = bool(self._op_comment.author == self.request.user) op_comment.moderated = op_comment.visibility not in Visibility.public_choices linked_comment = TemplateComment(linked_comment, is_op=(linked_comment.id == op_comment.id), request=self.request, title=op_comment.title) if self.page_current == 1 and op_comment.has_content(): first_comment_with_content = op_comment else: first_comment_with_content = None for reply in replies: if reply.has_content(): first_comment_with_content = reply break last_comment_with_content = None for reply in reversed(replies): if reply.has_content(): last_comment_with_content = reply break comment_to_expand = first_comment_with_content if self.gotoreply: comment_to_expand = linked_comment ctx.update({ 'op_comment': op_comment, 'linked_comment': linked_comment, 'remix_of': remix_of, 'reply_to': reply_to, 'replies': replies, 'recent_replies': recent_replies, 'top_replies': top_replies, 'top_remixes': [reply for reply in top_replies if reply.has_content()], 'replied': replied, 'linked_comment': linked_comment, 'fb_metadata': linked_comment.shareable_metadata(), 'large_thread_view': len(replies) >= 50, 'title': getattr(op_comment, 'title', None), 'first_comment_with_content': first_comment_with_content, 'last_comment_with_content': last_comment_with_content, 'comment_to_expand': comment_to_expand, }) return ctx