def post_details(request, pk): le_post = get_object_or_404(Post, id = pk) if request.method == 'POST': comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): parent_obj = None try: parent_id = int(request.POST.get('parent_id')) except: parent_id = None if parent_id: parent_obj = Comment.objects.get(id=parent_id) if parent_obj: replay_comment = comment_form.save(commit=False) replay_comment.parent = parent_obj new_comment = comment_form.save(commit=False) new_comment.name = request.user new_comment.post = le_post new_comment.save() return redirect('post_details', pk = le_post.id) le_post = get_object_or_404(Post, id = pk) comments = Comment.objects.all() post_comments = [] for comment in comments: if comment.post == le_post: post_comments.append(comment) comment_form = CommentForm() return render(request, 'forum/post_details.html',{'post': le_post, 'comments': post_comments,'comment_form': comment_form})
def Addcomment1(request,article_id): if request.method == 'POST' and ("pause" not in request.session): form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.comments_article = Article.objects.get(id=article_id) qwe = auth.get_user(request).username comment.comments_from = User.objects.get_by_natural_key(qwe) form.save() #request.session.set_expiry(60) #request.session['pause'] = True return HttpResponseRedirect('/forum/forum_article/%s' % article_id)
def post(self, request, context={}, *args, **kwargs): if not 'id' in self.kwargs: return HttpResponseRedirect(reverse('_finding')) id = self.kwargs['id'] try: thread = Thread.objects.get(id=id) except Exception as e: return _Http404(request) comment_form = CommentForm(request.POST) if comment_form.is_valid(): m = comment_form.save(commit=False) m.create_user = request.user m.thread_id = id m.save() try: t = Thread.objects.get(id=id) t.reply = t.reply + 1 t.save() except Exception as e: pass # get comment list from databases comment_list = Comment.objects.filter(thread_id=id) # get comment list from databases return super().post( request, { 'thread': thread, 'tid': id, 'comment_form': comment_form, "comment_list": comment_list }, *args, **kwargs)
def thread(request, thread_id): thread = get_object_or_404(Thread, pk=thread_id) if request.POST and request.user.is_authenticated(): form = CommentForm(request.POST) if form.is_valid(): form.save(thread, request.user) thread.last_comment_date = datetime.now() thread.save() return HttpResponseRedirect(reverse('forum_thread', args=[thread_id])) else: form = CommentForm() return render_to_response(request, 'forum/thread.html', {'thread': thread, 'user': request.user.is_authenticated() and request.user or None, 'comments': Comment.objects.filter(thread=thread).order_by('date_created'), 'form': form, })
def post_comment(request, pk): 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('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'forum/post_comment.html', {'form': form})
def ajax_view(request): form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.user = request.user comment.save() return JsonResponse({ "comment": '{} - {}'.format(datetime.now(), comment.content), 'pk': comment.pk })
def add_comment_to_post(request, pk): """Allow adding a comment to a post""" post = get_object_or_404(Post, pk=pk) user = request.user if request.method == "POST": form = CommentForm(request.POST or None) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.author = user comment.save() messages.success(request, "Your comment is pending approval...") return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'forum/comment_form.html', {'form': form})
def get_one_article(request, **kwargs): if request.method == 'GET': form = CommentForm() article = Article.objects.get(pk=kwargs['pk']) comments = article.comment_article.all() return render(request, 'forum/detail_article.html', {"article": article, 'comments': comments, 'form': form}) if request.method == 'POST': form = CommentForm(request.POST) article = Article.objects.get(pk=kwargs['pk']) if form.is_valid(): comment = form.save(commit=False) comment.article = article comment.publisher = UserProfile.objects.get(user=request.user) comment.save() return redirect('one_article', pk=kwargs['pk']) return redirect('one_article', pk=kwargs['pk'])
def query_detail(request, pk): ''' function to show a single post detail, also displays any comments associated with a post and allows logged in users to add comments to post ''' query_detail = get_object_or_404(Post, pk=pk) # getting current session user user = request.user # Creating comment form comment_form = CommentForm() # if form is submitted if request.method == 'POST': # checking user is logged in logged_user = request.user.id # dont let non logged in user add comment if not logged_user: messages.warning(request, f'You must be logged in to comment') return redirect('posts') # take submitted form comment_form = CommentForm(request.POST or None) # if submitted form is valid save it to db if comment_form.is_valid(): comment = comment_form.save(commit=False) # link query foregin key comment.query = query_detail # adding query detail to title field comment.title = query_detail.title # adding user to foreign key field comment.comment_by = user comment.save() # message user and reload post detail page messages.success(request, f'Comment added sucessfully') return redirect('post-detail', pk=query_detail.pk) # finding comments related to current post comments = Comment.objects.filter(query_id=pk) # adding to context and returning to post detail page context = { "post": query_detail, "comments": comments, "form": comment_form } return render(request, "forum/post_detail.html", context)
def PostComment(request): commentform = CommentForm(request.POST) message = {'message': 'something wrong!'} print(request.POST) if(commentform.is_valid()): comment=commentform.save(commit=False) if request.user.is_authenticated(): comment.author = request.user comment.end=request.POST['end'] comment.start=request.POST['start'] comment.title=request.POST['title'] comment.object_id=request.POST['object_id'] comment.content_type=ContentType.objects.get_for_model(Timelike) if (request.POST['isBasic']=='on'): comment.isBasic=True else: comment.isBasic=False comment.save() newComment = Comment.objects.latest('pubDate') message = newComment.singlejson() print(message) return HttpResponse(json.dumps(message))
def newcomment(request, cat_id, thd_id, pst_id): if not request.user.is_authenticated(): return redirect(reverse(index)) else: try: post = Post.objects.get(pk=pst_id) except Post.DoesNotExist: url = ''.join(('/agora/', str(cat_id), '/', str(thd_id), '/')) return redirect(url, request) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): new = form.save(commit=False) new.author = request.user new.date = datetime.datetime.now() new.parent = post new.save() url = ''.join(('/agora/', str(cat_id), '/', str(thd_id), '/')) return redirect(url, request) else: form = CommentForm() return render(request, 'newcomment.html', locals())
def create_comment(request, module_name_slug, question_page_name_slug, question_post=None): context_dict = {} module = Module.objects.get(slug=module_name_slug) question_page = QuestionPage.objects.get(slug=question_page_name_slug) question_posts = QuestionPost.objects.filter(page=question_page) comments = Comment.objects.filter(post__in=question_posts) if request.method == 'POST': post_form = QuestionPostForm comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # Save comment instance comment = comment_form.save(commit=False) q_post = question_post # request.POST.get('question_post') comment.post = get_object_or_404(QuestionPost, pid=q_post) comment.user_profile = UserProfile.objects.get(user=request.user) comment.save() else: # Invalid form(s): Print errors to console/log print(comment_form.errors) comment_form = CommentForm else: comment_form = CommentForm post_form = QuestionPostForm context_dict['question_posts'] = question_posts context_dict['question_page'] = question_page context_dict['comments'] = comments context_dict['module'] = module context_dict['post_form'] = post_form context_dict['comment_form'] = comment_form return HttpResponseRedirect('/forum/questions/%s/%s/' % (module.slug, question_page.slug))