def build_posts_social(posts, user): for post in posts: from interactions.CommentUtilities import CommentsBuilder from django.contrib.contenttypes.models import ContentType comment_ct = ContentType.objects.get_for_model(Comment) comments_builder = CommentsBuilder(post, comment_ct) (post.comments, flatted_post_descendants) = comments_builder.build_tree() # Set post upvotes & downvotes from interactions.VoteUtilities import VoteUtilities post.upvotes = VoteUtilities.get_upvotes_of(post) post.downvotes = VoteUtilities.get_downvotes_of(post) post.user_in_upvotes = False for vote in post.upvotes.all(): if user == vote.user: post.user_in_upvotes = True break post.user_in_downvotes = False for vote in post.downvotes.all(): if user == vote.user: post.user_in_downvotes = True break
def add_comment_data_to_element(node, node_element, user, root, host_type): # Set the ID of the <li> holding the comment to the ID of the node node_element.set('id', 'comment-' + str(node.id)) # Add the user thumbnail to the comment block node_thumbnail_wrapper = ElementTree.SubElement(node_element, 'div') node_thumbnail_wrapper.set('class', 'post-comment-user-thumbnail') #node_thumbnail = ElementTree.SubElement(node_thumbnail_wrapper, 'div') node_thumbnail = ElementTree.Element('div') node_thumbnail.set('class', 'discussion-response-thumbnail') node_thumbnail.set('style', 'background-image: url(\'%s\')' % ( settings.MEDIA_URL + node.user.get_profile().profile_pic.name )) node_thumbnail.text = ' ' node_thumbnail_wrapper.append(node_thumbnail) # Add post body to comment block commentor = ElementTree.Element('a') commentor.set('href', reverse('user:user_profile', kwargs={ 'username': node.user.username })) commentor.text = node.user.get_full_name() markdown_html = ElementTree.fromstring( '<div class=\'post-comment-body\'>' + ElementTree.tostring(commentor).encode('utf-8') + node.body_markdown_html.encode('utf-8') + '</div>') # Add comment actions (reply, upvote, downvote) to the comment node_actions = ElementTree.SubElement(markdown_html, 'div') node_actions.set('class', 'comment-actions') # Add 'reply' action to the comment actions block node_reply = ElementTree.SubElement(node_actions, 'div') node_reply.set('class', 'reply-to-comment') node_reply.text = settings.STRINGS['comments']['REPLY'] # Add 'upvote' action to the comment actions block node_upvote = ElementTree.SubElement(node_actions, 'div') upvotes = VoteUtilities.get_upvotes_of(node) node_upvote.text = str(upvotes.count()) node_upvote.set('class', 'upvote-comment') for upvote in upvotes.all(): if user == upvote.user: node_upvote.set('class', 'upvote-comment user-upvoted') break # Add 'downvote' action to the comment actions block node_downvote = ElementTree.SubElement(node_actions, 'div') downvotes = VoteUtilities.get_downvotes_of(node) node_downvote.text = str(downvotes.count()) node_downvote.set('class', 'downvote-comment') for downvote in downvotes.all(): if user == downvote.user: node_downvote.set('class', 'downvote-comment user-downvoted') break # If comment is created by the user or the requester is the admin, # add a delete button if (host_type.name == 'project' and user in root.admins.all()) or ( host_type.name == 'resource' and user == node.user): delete_button = ElementTree.SubElement(node_element, 'div') delete_button.set('class', 'delete-button') delete_button.set('title', 'Delete comment') delete_button.text = ' ' node_element.append(markdown_html)