Example #1
0
def comment(request, blog_name):
  usr = request.user
  contnt = request.POST['content']
  pst = Post.objects.get(id=request.POST['post_id'])
  c = Comment(user=usr, post=pst, content=contnt)
  c.save()
  return redirect('/' + blog_name + '/' + request.POST['post_id'] + '/')
Example #2
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
Example #4
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})
Example #5
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()
Example #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)
Example #7
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')
Example #8
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)
Example #9
0
def blog(request, id):
    id = int(id)
    blog = Blog.objects.get(id=id) # get the id from url.
    comment = Comment.objects.filter(blog_id=id) # ??Is there any problem?

    if request.method == 'POST':
        form = UserInfo(request.POST)
        user = User(form.name, form.email, form.website)
        user.save()
    # exception
        now = datetime.now()
        comment = Comment(user, blog, form.comment, now)
        comment.save()
        c = RequestContext(request, {'blog': blog, 'comments': comment, 'form': form }) # add form
        c.update(csrf(request))
        return render_to_response('blog.html', c)
    return render_to_response('blog.html', {'blog': blog, 'comments': comment})# To invoke insert func there.
Example #10
0
 def delete_cascade(self, user):
     # delete comments
     user_comments = Comment.query(Comment.user == user.key)
     CommentDeleteMixin.delete_cascade_multi(self, user_comments)
     # delete posts
     user_posts = Post.query(Post.author == user.key)
     for post in user_posts:
         post.delete_cascade()
     # delete reactions
     user_reactions = Reaction.query(Reaction.user == user.key)
     ndb.delete_multi(user_reactions.fetch(keys_only=True))
     # finally, the user itself
     user.key.delete()
Example #11
0
def comment(request):
    if request.method == 'POST':
        comment = request.POST['comment']
        Comment(comment=comment).save()

    return render(request, 'postadded.html')
Example #12
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]))
Example #13
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)