我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用models.Comment()。
def test_for_user_doesnt_return_muted_conversations(self): """ When we create a Comment, conversation gets created for the sharedfile owner and the commenter. The conversation should appear for the commenter, unless the conversation gets muted, in which case it doesn't get returned. """ comment = Comment(sharedfile_id=self.sharedfile.id, user_id=self.another_user.id, body='test') comment.save() another_user_conversation = Conversation.get('user_id = %s', self.another_user.id) self.assertEqual(0, another_user_conversation.muted) self.assertEqual(1, len(Conversation.for_user(self.another_user.id))) another_user_conversation.muted = 1 another_user_conversation.save() self.assertEqual(0, len(Conversation.for_user(self.another_user.id)))
def comment(request): form = CommentForm(request.POST) if form.is_valid(): comment = Comment() comment.content = form.cleaned_data.get('content') comment.user = request.user id=form.cleaned_data.get('article_id') try: article = Article.objects.get(id=id) comment.article = article comment.save() article.save() rtn = {'status':True,'redirect':reverse('blog:show', args=[id])} except Article.DoesNotExist: rtn = {'status':False,'error':'Article is not exist'} else: rtn = {'status':False,'error':form.errors} return JsonResponse(rtn)
def dashboard(request, condition='recent'): """Dashboard""" post_count = settings.DASHBOARD_POST_COUNT comment_count = settings.DASHBOARD_COMMENT_COUNT if condition == 'recent': order = '-id' elif condition == 'view': order = '-view_count' elif condition == 'like': order = '-like_count' elif condition == 'comment': order = '-comment_count' else: return error_page(request) posts = Blog.objects.filter(status='1normal').order_by(order)[:post_count] comments = Comment.objects.filter( status='1normal').order_by('-id')[:comment_count] total_posts = Blog.objects.filter(status='1normal').count() total_comments = Comment.objects.filter(status='1normal').count() total_spams = Comment.objects.filter(status='7spam').count() total_users = User.objects.count() return render( request, "blogs/dashboard.html", { 'posts': posts, 'comments': comments, 'condition': condition, 'total_posts': total_posts, 'total_comments': total_comments, 'total_spams': total_spams, 'total_users': total_users, } )
def delete_comment(request, id): """Delete comment""" comment = get_object_or_404(Comment, pk=id) comment.status = '6deleted' comment.save() referer = get_referer(request) return redirect(referer)
def spam_comment(request, id): """Spam comment""" comment = get_object_or_404(Comment, pk=id) comment.status = '7spam' comment.save() referer = get_referer(request) return redirect(referer)
def restore_comment(request, id): """Restore comment""" comment = get_object_or_404(Comment, pk=id) if comment.status == '6deleted' or comment.status == '7spam': comment.status = '1normal' comment.save() else: return error_page(request) referer = get_referer(request) return redirect(referer)
def empty_comment(request, status): """Empty comment""" if status == '6deleted' or status == '7spam': q = Q(status__iexact=status) Comment.objects.filter(q).all().delete() referer = get_referer(request) return redirect(referer)
def test_a_non_logged_in_user_cant_delete_comments(self): """ Posting to delete a comment if you're not logged in should result in a 403 error. All comments remain intact. """ sharedfile = test.factories.sharedfile(self.admin) comment = models.Comment(user_id=self.admin.id, sharedfile_id=sharedfile.id, body="just a comment") comment.save() response = self.post_url("/p/%s/comment/%s/delete" % (sharedfile.share_key, comment.id)) self.assertEqual(response.code, 403) self.assertEqual(comment.id, models.Comment.get("id = %s and deleted = 0", comment.id).id) self.assertEqual(None, models.Comment.get("id = %s and deleted = 1", comment.id))
def test_can_delete_own_comment(self): """ A user should be able to delete their comment on another's sharedfile. """ admins_sharedfile = test.factories.sharedfile(self.admin) comment = models.Comment(user_id=self.bob.id, sharedfile_id=admins_sharedfile.id, body="just a comment") comment.save() self.sign_in('bob', 'asdfasdf') self.assertEqual(None, models.Comment.get("id = %s and deleted = 1", comment.id)) response = self.post_url("/p/%s/comment/%s/delete" % (admins_sharedfile.share_key, comment.id)) self.assertEqual(comment.id, models.Comment.get("id = %s and deleted = 1", comment.id).id)
def test_delete_anothers_comment_on_own_file(self): """ A user can delete another's comment on their own sharedfile. """ admins_sharedfile = test.factories.sharedfile(self.admin) comment = models.Comment(user_id=self.bob.id, sharedfile_id=admins_sharedfile.id, body="just a comment") comment.save() self.sign_in('admin', 'asdfasdf') self.assertEqual(None, models.Comment.get("id = %s and deleted = 1", comment.id)) response = self.post_url("/p/%s/comment/%s/delete" % (admins_sharedfile.share_key, comment.id)) self.assertEqual(comment.id, models.Comment.get("id = %s and deleted = 1", comment.id).id)
def test_can_not_delete_anothers_sharedfile(self): """ A user can not delete someone else's comment if it's on a sharedfile they don't own. """ admins_sharedfile = test.factories.sharedfile(self.admin) comment = models.Comment(user_id=self.bob.id, sharedfile_id=admins_sharedfile.id, body="just a comment") comment.save() self.sign_in('tom', 'asdfasdf') self.assertEqual(None, models.Comment.get("id = %s and deleted = 1", comment.id)) response = self.post_url("/p/%s/comment/%s/delete" % (admins_sharedfile.share_key, comment.id)) self.assertEqual(None, models.Comment.get("id = %s and deleted = 1", comment.id))
def test_muting_conversation(self): """ Add a comment, which will create a conversation for the commenter (user2) and sharedfile owner (admin). When user2 tries to mute admin's conversation, it should fail and admin's conversation state will remain unchanged. When muting own converastion, "muted" flag should change to true. Contingent on user2 being signed in. (see setUp) """ comment = Comment(sharedfile_id=self.shf.id, user_id=self.user2.id, body='test') comment.save() admin_conversation = Conversation.get('user_id = %s', self.admin.id) user2_conversation = Conversation.get('user_id = %s', self.user2.id) self.assertEqual(admin_conversation.muted, 0) self.assertEqual(user2_conversation.muted, 0) request = HTTPRequest(self.get_url('/conversations/%s/mute' % admin_conversation.id), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "_xsrf=%s" % (self.xsrf)) self.http_client.fetch(request, self.stop) response = self.wait() request = HTTPRequest(self.get_url('/conversations/%s/mute' % user2_conversation.id), 'POST', {'Cookie':'_xsrf=%s;sid=%s' % (self.xsrf, self.sid)}, "_xsrf=%s" % (self.xsrf)) self.http_client.fetch(request, self.stop) response = self.wait() # refetch from DB, and verify mute flags remain 0. admin_conversation = Conversation.get('user_id = %s', self.admin.id) user2_conversation = Conversation.get('user_id = %s', self.user2.id) self.assertEqual(admin_conversation.muted, 0) self.assertEqual(user2_conversation.muted, 1)
def test_deleting_sharedfile_also_mutes_conversations(self): new_comment = Comment(user_id=self.user.id, sharedfile_id=self.sharedfile.id) new_comment.save() self.sharedfile.delete() muted_conversation = Conversation.get('user_id=%s and sharedfile_id=%s and muted = 1', self.user.id, self.sharedfile.id) self.assertTrue(muted_conversation)
def test_comment_count(self): """ Comment count should return the number of comments belonging to shard file. Should not count deleted comments. """ self.assertEqual(0, self.sharedfile.comment_count()) comment = Comment(sharedfile_id=self.sharedfile.id, user_id=self.user.id, body="just a comment") comment.save() self.assertEqual(1, self.sharedfile.comment_count()) comment = Comment(sharedfile_id=self.sharedfile.id, user_id=self.user.id, body="just a comment", deleted=True) comment.save() self.assertEqual(1, self.sharedfile.comment_count())
def test_new_comment_notification(self): new_comment = Comment(user_id=self.user2.id, sharedfile_id = self.sharedfile.id, body="Testing comment") new_comment.save() sent_notification = Notification.get("id=%s", 1) self.assertEqual(sent_notification.sender_id, self.user2.id) self.assertEqual(sent_notification.receiver_id, self.user.id) self.assertEqual(sent_notification.action_id, 1) self.assertEqual(sent_notification.type, 'comment')
def test_new_comment_doesnt_store_for_same_user(self): new_comment = Comment(user_id=self.user.id, sharedfile_id = self.sharedfile.id, body="Testing comment") new_comment.save() sent_notification = Notification.get("id=%s", 1) self.assertFalse(sent_notification)
def dashboard_comment(request, status='all', page=1): """Dashboard comment""" list_count = settings.DASHBOARD_LIST_COUNT if int(page) < 1: return redirect('blogs:dashboard_comment', status, 1) current_page = int(page) - 1 start_at = current_page * list_count end_at = start_at + list_count if status == 'all': q = Q() else: q = Q(status__iexact=status) total = Comment.objects.filter(q).count() lists = Comment.objects.filter(q).order_by('-id')[start_at:end_at] count_all = Comment.objects.count() count_normal = Comment.objects.filter(status__iexact='1normal').count() count_deleted = Comment.objects.filter(status__iexact='6deleted').count() count_spam = Comment.objects.filter(status__iexact='7spam').count() index_total = int(ceil(float(total) / list_count)) index_begin = (current_page / 10) * 10 + 1 index_end = mindex_end = index_total if index_end - index_begin >= 10: index_end = index_begin + 9 mindex_begin = (current_page / 5) * 5 + 1 if mindex_end - mindex_begin >= 5: mindex_end = mindex_begin + 4 return render( request, "blogs/dashboard_comment.html", { 'lists': lists, 'total': total, 'page': current_page + 1, 'index_begin': index_begin, 'index_end': index_end + 1, 'mindex_begin': mindex_begin, 'mindex_end': mindex_end + 1, 'index_total': index_total, 'status': status, 'count_all': count_all, 'count_normal': count_normal, 'count_deleted': count_deleted, 'count_spam': count_spam, } )