def dispatch(self, request, *args, **kwargs): #@todo: Sprawdzic, czy uzytkownik moze komentowac meta = UserMetaData.objects.get( user = request.user ) if request.POST and meta.canComment: form = CommentForm( request.POST ) if form.is_valid(): obj = form.save( commit = False ) if obj.text.strip() == "": return HttpResponse( 0 ) obj.author = request.user obj.date = datetime.now() obj.save() else: return HttpResponse( 0 ) form = CommentForm( initial={"content" : Content.objects.get( id = kwargs["id"] ) }) comments = Comment.objects.filter( content = Content.objects.get( id = kwargs["id"] ) ) data = { "MEDIA_URL": MEDIA_URL, "user" : request.user, "comments" : comments, "form" : form, "id" : kwargs["id"] } data.update( csrf( request ) ) return render_to_response( "core/comments.html", data )
def serve(self, request): from core.forms import CommentForm if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): if request.user.is_authenticated(): inst = form.save(commit=False) inst.user = request.user inst.save() messages.info(request, 'Сообщенее добавлено.Оно будет доступно для всеобщего обозрения после проверки администратором.') else: messages.error(request, 'Коментарии только для авторизированых пользователей.') # send message return render(request, self.template, { 'self': self, 'form': form, }) else: form = CommentForm() return render(request, self.template, { 'self': self, 'form': form, })
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 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 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 post(self, request, *args, **kwargs): print(request.POST) # create a form instance and populate it with data from the request: form = CommentForm(request.POST, auto_id=True) # check whether it's valid: if form.is_valid(): form.instance.user = request.user form.instance.issue = Issue.objects.get(pk=self.kwargs['pk']) form.save() return redirect( reverse('project:repository:issue:overview', kwargs={ 'project_title': kwargs['project_title'], 'repository_title': kwargs['repository_title'], 'pk': kwargs['pk'] } ) ) else: return HttpResponseBadRequest
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 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 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 add_comment(request, pk): """Add a new comment.""" p = request.POST if p.has_key("body") and p["body"]: author = "Anonymous" if p["author"]: author = p["author"] comment = Comment(post=Post.objects.get(pk=pk)) cf = CommentForm(p, instance=comment) cf.fields["author"].required = False comment = cf.save(commit=False) comment.author = author comment.save() return HttpResponseRedirect(reverse("core.views.post", args=[pk]))
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 view_post(request, pk): post = get_object_or_404(Post, pk=pk) form = CommentForm(request.POST or None) recommend_post_form = CommentForm(request.POST or None) if form.is_valid(): comment = form.save(commit=False) comment.publisher = request.user comment.post = post comment.save() return redirect(request.path) form.initial['publisher'] = request.user.pk return render_to_response('core/post_detail.html', { 'post': post, 'form': form, }, context_instance=RequestContext(request))
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 })