Пример #1
0
def fake_comments(count=500):
    for i in range(count):
        comment = Comment(
            author = fake.name(),
            email = fake.email(),
            site = fake.url(),
            timestamp=fake.date_time_this_year(),
            reviewed = True,
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)

    salt = int(count * 0.1)
    for i in range(salt):
        # unreviewed comments
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=False,
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)

        # from admin
        comment = Comment(
            author='施珊珊',
            email='*****@*****.**',
            site='163.com',
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            from_admin=True,
            reviewed=True,
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)

    for i in range(salt):
        comment = Comment(
            author=fake.name(),
            email=fake.email(),
            site=fake.url(),
            body=fake.sentence(),
            timestamp=fake.date_time_this_year(),
            reviewed=True,
            replied=Comment.query.get(random.randint(1, Comment.query.count())),
            post=Post.query.get(random.randint(1, Post.query.count()))
        )
        db.session.add(comment)
    db.session.commit()
Пример #2
0
    def test_create_comment(self):
        Comment.post = Post(title='Post being commented on')
        Comment.author = User(username='******')
        comment = Comment(author='James', text='This is my first test comment')

        self.assertEqual(comment.author, 'James')
        self.assertEqual(comment.text, 'This is my first test comment')
Пример #3
0
    def post(self, request, slug):
        comment = request.POST.get('comment')

        print(comment)
        blog = Blog.objects.get(is_active=True, slug=slug)
        comment = Comment(blog=blog, user=self.request.user)
        comment.save()

        return render(request, self.template_name)
    def post(self, username, post_id):
        post = self.get_post_by_id_or_404(post_id)
        data = request.get_json()

        content = data.get('content', '')
        if content:
            comment = Comment(user=g.user.key,
                              post=post.key,
                              content=content)
            comment.put()
        return None, 201
Пример #5
0
    def post(self, request):
        post_slug = request.POST['slug']

        name = request.POST['name']
        email = request.POST['email']
        message = request.POST['message']
        parent = request.POST['parent']

        c = Comment(name=name, email=email, message=message, parent=parent)
        c.save()

        return redirect('/blogs/' + post_slug, {'comment_added': True})
Пример #6
0
def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)
    form = CommentForm()
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment(author=form.cleaned_data['author'],
                              body=form.cleaned_data['body'],
                              post=post)
            comment.save()

    comments = Comment.objects.filter(post=post)
    context = {'post': post, 'comments': comments, 'form': form}
    return render(request, 'blog_detail.html', context)
Пример #7
0
def blog_detail(request, pk):
    post = Post.objects.get(pk=pk)
    comments = Comment.objects.filter(post=post)

    form = CommentForm()
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = Comment(
                author=form.cleaned_data["author"],
                body=form.cleaned_data["body"],
                post=post,
            )
            comment.save()

    context = {"post": post, "comments": comments, "form": form}
    return render(request, "blog_detail.html", context)
Пример #8
0
def comment(request):
    if request.method == 'POST':
        comment = request.POST['comment']
        Comment(comment=comment).save()

    return render(request, 'postadded.html')
Пример #9
0
 def post(self, request, pk) :
     f = get_object_or_404(Blog, id=pk)
     comment = Comment(text=request.POST['comment'], owner=request.user, blog=f)
     comment.save()
     return redirect(reverse('blogs:blog_detail', args=[pk]))
Пример #10
0
def createComment_AJAX(request):
    if not (request.method == "POST" and request.is_ajax()):
        return JsonResponse({'message': f'Not authorized'})
    blog_id = request.POST['blog_id']
    if blog_id is None or blog_id == "":
        return JsonResponse({'message': f'Invalid Data'})
    try:
        blog = Blog.objects.get(id=int(blog_id))
    except Blog.DoesNotExist:
        return JsonResponse({'message': f'Blog does not exist'})
    content = request.POST['content']
    if content is None or content == "" or len(content) > 500:
        return JsonResponse({'message': f'Invalid content'})
    if request.user.is_authenticated:
        comment = Comment(blog=blog, user=request.user, content=content)
        comment.save()
        message = "Successfully added Comment"
        data = {
            "added":
            True,
            "message":
            message,
            "comment_html":
            render_to_string('blogs/comment.html', {
                'comment': comment,
                'request': request,
            })
        }
        return JsonResponse(data)
    else:
        name = request.POST['name']
        email = request.POST['email']
        website = request.POST['website']
        if name is None or name == "" or len(name) > 50:
            return JsonResponse({'message': f'Invalid name'})
        if email is None or email == "" or len(email) > 50:
            return JsonResponse({'message': f'Invalid email'})
        try:
            validate_email(email)
        except ValidationError as e:
            return JsonResponse({'message': f'Invalid email'})
        if len(website) > 50:
            return JsonResponse({'message': f'Invalid website'})
        comment = Comment(blog=blog,
                          name=name,
                          email=email,
                          website=website,
                          content=content)
        comment.save()
        message = "Successfully added Comment"
        data = {
            "added":
            True,
            "message":
            message,
            "comment_html":
            render_to_string('blogs/comment.html', {
                'comment': comment,
                'request': request,
            })
        }
        return JsonResponse(data)