def handle(self, *args, **options): num_comments = 500 users = User.objects.filter(admin=False) articles = Article.objects.all() filter_words = Filter_Words.objects.all() content = open("dictionaries/comments").readlines() count = 0 for i in range(0, num_comments): comments = Comment.objects.all() c = Comment() c.user = random.choice(users) c.article = random.choice(articles) c.content = random.choice(content).rstrip() c.pub_date = timezone.now() for word in filter_words: if (re.search(str(word), c.content, re.IGNORECASE)): c.flag = True if (i > num_comments / 2): c.parent = random.choice(comments) c.article = c.parent.article c.save() count = i print(str(count) + " comments added")
def post(self, request, article_id): comment = Comment() user = UserFree() content = request.POST.get('content', '') nickname = request.POST.get('nickname', '') email = request.POST.get('email', '') if nickname == '' or email == '': user.nickname = '匿名用户' user.email = '' else: user.nickname = nickname user.email = email user.save() reply_comment_id = request.POST.get('reply_comment_id') if int(reply_comment_id) < 0: return HttpResponse('{"status":"fail", "msg":"回复出错"}', content_type='application/json') elif int(reply_comment_id) == 0: comment.parent = None comment.root = None comment.article_id = article_id comment.content = content comment.user = user comment.save() return HttpResponse('{"status":"success", "msg":"评论成功"}', content_type='application/json') elif int(reply_comment_id) > 0: parent = Comment.objects.get(id=int(reply_comment_id)) if parent: if parent.root is None: comment.root_id = parent.id else: comment.root_id = parent.root_id comment.article_id = article_id comment.parent = parent comment.content = content comment.reply_to = parent.user comment.user = user comment.save() return HttpResponse('{"status":"success", "msg":"回复成功"}', content_type='application/json') else: return HttpResponse('{"status":"fail", "msg":"回复出错"}', content_type='application/json')
def comment(request): comment = Comment( entry=Entry.objects.get(pk=request.POST['entry_id']), author=User.objects.get(pk=request.POST['author']), comment=request.POST['comment'], ) # if this is a reply to a comment, not to a post if request.POST['parent_id'] != '': comment.parent = Comment.objects.get(pk=request.POST['parent_id']) comment.save() return HttpResponseRedirect(reverse('articles:article', args=(comment.entry.id,)))