def comment_block(request, object): """ Returns the info needed to be used with the comments template Params: request: The request instance object: The object to comment on (for instance an Event) """ if 'new_comment' in request.POST: comment_form = CommentForm(object, request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.user = request.user comment.save() comment_form = CommentForm(object) request.message_success(_("Comment added successfully!")) else: comment_form = CommentForm(object) try: object.comments except: raise IntegrityError( "Models using comments should have a generic relation named 'comments' to the Comment model!" ) return {'form': comment_form, 'list': object.comments.all()}
def article_detail( request, pk, template_name='accounts/articles_related/article_details.html'): article = get_object_or_404(Article, pk=pk) # article = Article.objects.get(pk=pk) comments = Comment.objects.filter(article=article, reply=None).order_by('-id') if request.method == 'POST': comment_form = CommentForm(request.POST or None) if comment_form.is_valid(): content = request.POST.get('content') reply_id = request.POST.get('comment_id') comment_qs = None if reply_id: comment_qs = Comment.objects.get(id=reply_id) comment = Comment.objects.create(article=article, user=request.user, content=content, reply=comment_qs) comment.save() return redirect('my_articles') comment_form = CommentForm() context = { 'object': article, 'comments': comments, 'comment_form': comment_form } return render(request, template_name, context)
def post_detail(request, slug): post = get_object_or_404(Post, slug=slug) comments = post.comments.filter(active=True) new_comment = None # Comment posted if request.method == 'POST': comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # Create comment object but don't save database yet new_comment = comment_form.save(commit=False) # Assign the current post to the comment new_comment.post = post # Save the comment to the database new_comment.save() else: comment_form = CommentForm() return render( request, "core/post_detail.html", { "post": post, "comments": comments, "new_comment": new_comment, "comment_form": comment_form })
def comment(request, slug): if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.post = Post.objects.get(slug=slug) comment.save() return redirect('post_detail', slug=slug) else: form = CommentForm() return render(request, 'comment_new.html', {'form': form})
def add_comment_to_post(request, pk): post = get_object_or_404(Post, pk=pk) form = CommentForm() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('post_detail', pk=post.pk) return render(request, 'core/comment_form.html', context={'form': form})
def new_comment(request, slug): post = get_object_or_404(UserPost, slug=slug) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.user = request.user comment.save() # Django Tutorial https://tutorial.djangogirls.org/en/django_templates/ post.comments.add(comment) return redirect('post_detail', slug=slug) else: form = CommentForm() return render(request, 'add_comment.html', {'form': form})
def add_comment_to_post(request, pk): cats = Category.objects.all() post = get_object_or_404(Post, pk=pk) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('core:post', pk=post.pk) else: form = CommentForm() return render(request, 'core/commentpost.html', { 'form': form, 'cats': cats })
def get(self, request, id): # fetch video from DB by ID video_by_id = Video.objects.get(id=id) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) video_by_id.path = 'http://localhost:5000/get_video/' + video_by_id.path print(video_by_id) print(video_by_id.path) context = {'video': video_by_id} if request.user.is_authenticated: print('user signed in') comment_form = CommentForm() context['form'] = comment_form comments = Comment.objects.filter( video__id=id).order_by('-datetime')[:5] print(comments) context['comments'] = comments try: channel = Channel.objects.filter( user__username=request.user).get().channel_name != "" print(channel) context['channel'] = channel except Channel.DoesNotExist: channel = False return render(request, self.template_name, context)
def create_comment(request, slug): post = get_object_or_404(Post, slug=slug) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.posted_by = request.user comment.save() return redirect(post.get_absolute_url()) else: form = CommentForm() template = 'core/add_comment.html' context = {'form': form} return render(request, template, context)
def test_comment_valid(self): """test_comment_form_validation() checks form with is_valid() method.""" form = CommentForm(data={ 'name': 'Tester', 'email': '*****@*****.**', 'body': 'testoo' }) self.assertTrue(form.is_valid())
def post_detail(request, post_id): post = Post.objects.get(pk=post_id) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid: comment = form.save(commit=False) comment.author = request.user comment.post = post comment.pk = comment.id comment.save() return redirect('post_detail', post_id=post.pk) else: form = CommentForm() return render(request,'posts/post_detail.html', { 'post': post, 'form': CommentForm(), 'comments': post.comments.all(), })
def post_detail(request, year, month, day, post): post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) comments = post.comments.filter(active=True) new_comment = None # List of similar posts post_tags_ids = post.tags.values_list('id', flat=True) similar_posts = Post.published.filter(tags__in=post_tags_ids).exclude( id=post.id) similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by( '-same_tags', '-publish')[:4] if request.method == 'POST': # A comment was posted comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # Create Comment object but don't save to database yet new_comment = comment_form.save(commit=False) # Assign the current post to the comment new_comment.post = post # Save the comment to the database new_comment.save() else: comment_form = CommentForm() return render( request, 'blog/post_detail.html', { 'post': post, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form, 'similar_posts': similar_posts })
def post(self, request): # pass filled out HTML-Form from View to CommentForm() form = CommentForm(request.POST) if form.is_valid(): # create a Comment DB Entry text = form.cleaned_data['text'] video_id = request.POST['video'] video = Video.objects.get(id=video_id) new_comment = Comment(text=text, user=request.user, video=video) new_comment.save() return HttpResponseRedirect('/video/{}'.format(str(video_id))) return HttpResponse('This is Register view. POST Request.')
def comments(request, post_id): """View function for home page of site.""" comments = Comment.objects.filter(post__id=post_id) post = Post.objects.get(id=post_id) form = CommentForm() # Render the HTML template index.html with the data in the context variable response = render( request, 'core/comment.html', { "comments": comments, "post_id": post_id, "post": post, "comment_form": form }) return response
def new_comment(request, post_id): post = Post.objects.get(id=post_id) if post is None: raise Http404('No task matches the given query.') form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.commenter = request.user comment.save() else: messages.error(request, 'We had a problem saving your comment.') return redirect('comments', post_id=post_id)
def Detail(request, pk): post = Post.objects.get(pk=pk) comments = Comment.objects.filter(post=post) form = CommentForm() context = { 'post': post, 'comments': comments, 'form': form, } if request.is_ajax() and request.POST: text = request.POST.get('comment') comment = Comment(text=text, post=post) comment.save() data = {'message': request.POST.get('comment')} return HttpResponse(json.dumps(data), content_type='application/json') return render(request, 'core/detail.html', context)
def get_context_data(self, *args, **kwargs): context_data = super(IssueDetailView, self).get_context_data(*args, **kwargs) context_data.update({ 'comment_form': CommentForm(), 'user_proposals': self.object.user_documents(self.request.user) }) context_data["delegation"] = self.object.get_delegation( self.request.user) context_data["polities"] = list( set([t.polity for t in self.object.topics.all()])) # HACK! As it happens, there is only one polity... for now if not settings.FRONT_POLITY: raise NotImplementedError('NEED TO IMPLEMENT!') context_data["polity"] = context_data['polities'][0] return context_data
def sendcomment(request): user = request.user form = CommentForm(request.POST) if form.is_valid(): data = form.cleaned_data classname = data['objectclass'] objclass = { 'Mix': Mix, 'Group': Group, 'Tag': Tag, 'Platform': Platform }[classname] obj = objclass.objects.get(id=data['objectid']) comment = Comment(content_object=obj, text=data['text'], user=user) comment.save() return HttpResponseRedirect(comment.get_absolute_url()) else: #2do error message return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def test_comment_invalid(self): """test_comment_form_validation() checks form with is_valid() method.""" form = CommentForm(data={'name': '', 'email': '', 'body': 'testoo'}) self.assertFalse(form.is_valid())
def test_comment_form_field_name_input(self): form = CommentForm(initial={'name': 'Tester'}) self.assertEqual(form['name'].value(), 'Tester')
def test_comment_form_field_email_input(self): form = CommentForm(initial={'email': '*****@*****.**'}) self.assertEqual(form['email'].value(), '*****@*****.**')
def test_comment_form_field_text_input(self): form = CommentForm(initial={'body': 'Test test test.'}) self.assertEqual(form['body'].value(), 'Test test test.')
def test_form_renders_item_input(self): """test_form_renders() tests input attributes render by HTML.""" form = CommentForm() # test Bootstrap CSS classes self.assertIn('class="form-control"', form.as_p())