def comment_view(request): user = check_validation(request) if user and request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): post=form.cleaned_data.get('post') post_id = form.cleaned_data.get('post').id comment_text = form.cleaned_data.get('comment_text') comment = CommentModel.objects.create(user=user, post_id=post_id, comment_text=comment_text) comment.save() print "Comment is make on post" sg = sendgrid.SendGridAPIClient(apikey=API_KEY) from_email = Email("*****@*****.**") to_email = Email(post.user.email) subject = "InstaClone" content = Content("text/plain", "Comment is make on your post") mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post(request_body=mail.get()) print response print post.user.email return redirect('/feed/') else: return redirect('/feed/') else: return redirect('/login/')
def detail(request, pk): d = Post.objects.get(pk=pk) comments = Comment.objects.filter(post=d) # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: form = CommentForm(request.POST) # check whether it's valid: if form.is_valid(): comment = form.cleaned_data['comment'] # print(comment,pk) #save comment in Comment model that is in data base c = Comment(post=d, comment=comment, user=request.user) c.save() # process the data in form.cleaned_data as required # ... # redirect to a new URL: context = {'post': d, 'form': form, 'comments': comments} return render(request, 'detail.html', context) # if a GET (or any other method) we'll create a blank form else: form = CommentForm() context = {'post': d, 'form': form, 'comments': comments} return render(request, 'detail.html', context)
def comment_view( request ): # this function used for posting comment on the post of the user user = check_validation(request) if user and request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): post_id = form.cleaned_data.get('post').id comment_text = form.cleaned_data.get('comment_text') comment = CommentModel.objects.create(user=user, post_id=post_id, comment_text=comment_text) comment.save() sg = sendgrid.SendGridAPIClient(apikey=(SENDGRID_API_KEY)) from_email = Email("*****@*****.**") to_email = Email(comment.post.user.email) subject = "Welcome to Review book" content = Content("text/plain", "someone just commented on your post. Go check") mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post(request_body=mail.get()) print(response.status_code) print(response.body) print(response.headers) ctypes.windll.user32.MessageBoxW(0, u"comment posted successfully", u"SUCCESS", 0) return redirect('/feed/ ') else: return redirect('/feed/') else: return redirect('/login')
def detail(request, pk): p = Post.objects.get(pk=pk) comments = Comment.objects.filter(post_id=pk) # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: form = CommentForm(request.POST) # check whether it's valid: if form.is_valid(): # process the data in form.cleaned_data as required # ... comment = form.cleaned_data['comment'] c = Comment(post_id=pk, comment=comment, user=request.user) c.save() # redirect to a new URL: return HttpResponseRedirect( reverse('myapp:detail', kwargs={'pk': pk})) # if a GET (or any other method) we'll create a blank form else: form = CommentForm() return render(request, 'detail.html', { 'form': form, 'p': p, 'comments': comments })
def PostDetail(request, slug): posts = Blog.objects.filter(status=1).order_by('?')[:4] ads = Ads.objects.all() blog_detail = Blog.objects.filter(slug=slug)[0] tags = blog_detail.tags.all() pythonpost = Blog.objects.filter(status=1, Category='Python').reverse() djangopost = Blog.objects.filter(status=1, Category='Django') post = get_object_or_404(Blog, slug=slug) comments = post.comments.filter(active=True) sitelogo = Logo.objects.all().last() pages = Pages.objects.all() post = Blog.objects.filter(status=1)[:3] sliderimage1 = HomepageSlider.objects.first() sliderimage2 = HomepageSlider.objects.last() title = TitleandTag.objects.all() about = SiteDescription.objects.first() footer = Footer.objects.first() footerlink = Footer.objects.last() video = Video.objects.first() 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 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() return HttpResponseRedirect(reverse('PostDetail', args=[slug])) else: comment_form = CommentForm() context = { 'post': post, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form, 'blog': blog_detail, 'pythonpost': pythonpost, 'djangopost': djangopost, 'tags': tags, 'posts': posts, 'ads': ads, 'send': False, 'pages': pages, 'Logo': sitelogo, 'post': post, 'first': sliderimage1, 'second': sliderimage2, 'title': title, 'about': about, 'footer': footer, 'footerlink': footerlink, 'video': video } return render(request, 'blog_detail.html', context)
def addcomment(request, articles_id=1, user_id=1): if request.POST: newcomm = CommentForm(request.POST) if newcomm.is_valid(): comment = newcomm.save(commit=False) comment.article = Articles.objects.get(id=articles_id) comment.user = User.objects.get(id=user_id) newcomm.save() return redirect('/article/%s/' % articles_id)
def add_comment_to_post(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, 'myapp/comment_form.html', {'form': form})
def addcomment(request, pk): a = get_object_or_404(Post, pk=pk) u = request.user form = CommentForm() if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): com = form.save(commit=False) com.post = a com.author = u com.save() return redirect('myapp:detail', pk=a.pk) return render(request, 'myapp/comment.html', {'form': form})
def comment_view(request): user = check_validation(request) if user and request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): post_id = form.cleaned_data.get('post').id comment_text = form.cleaned_data.get('comment_text') comment = Comment.objects.create(user=user, post_id=post_id, comment_text=comment_text) comment.save() return redirect('/feed/') else: return redirect('/feed/') else: return redirect('/login')
def post_detail_view(request,year,month,day,post): post=get_object_or_404(Post,slug=post,publish__year=year,publish__month=month,publish__day=day) comments=post.comments.filter(active=True) csubmit=False form=CommentForm() if request.method=='POST': form=CommentForm(request.POST) if form.is_valid(): newcomment=form.save(commit=False) newcomment.post=post newcomment.save() csubmit=True d={'post':post,'form':form,'csubmit':csubmit,'comments':comments} return render(request,'myApp/post_detail.html',d)
def comment_view(request): # ----------------------------------------------here is the function logic------------------------------------------------------- user = check_validation(request) if user and request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): post_id = form.cleaned_data.get('post').id comment_text = form.cleaned_data.get('comment_text') comment = CommentModel.objects.create(user=user, post_id=post_id, comment_text=comment_text) comment.save() # TODO: ADD MESSAGE TO INDICATE SUCCESS return redirect('/feed/') else: # TODO: ADD MESSAGE FOR FAILING TO POST COMMENT return redirect('/feed/') else: return redirect('/login')
def article(request, category_name, article_id): category = get_object_or_404(Category, name=category_name) article = get_object_or_404(Article, id=article_id, category=category) comment_list = ArticleComment.objects.filter( article=article).order_by('-pub_date') posted = False context = RequestContext(request) if request.method == 'POST': comment_form = CommentForm(data=request.POST) user = request.user if comment_form.is_valid(): comment_text = comment_form.cleaned_data['comment_text'] comment = ArticleComment(user=user, article=article, comment_text=comment_text, pub_date=timezone.now()) comment.save() article.comments = article.comments + 1 article.save() posted = True else: print comment_form.errors else: comment_form = CommentForm() #Search bar methods if request.method == 'GET' and 'search_category_button' in request.GET: search_category = request.GET.get('search_category') category_name = request.GET.get('search_name_c') if (category_name is not None) and (search_category is not None): return search_bar(request, search_category, category_name) if request.method == 'GET' and 'search_user_button' in request.GET: user_name = request.GET.get('search_name') if user_name is not None: return user_search(request, user_name) return render_to_response('myapp/base_article.html', { 'posted': posted, 'article': article, 'comment_list': comment_list }, context)
def comment_edit(request, song_id, comment_id=None): """コメントの編集""" song = get_object_or_404(Song, pk=song_id) # 親の曲を読む # comment_idが指定されている(修正時) if comment_id: comment = get_object_or_404(Comment, pk=comment_id) # comment_idが指定されていない(追加時) else: comment = Comment() if request.method == 'POST': # POSTされたrequestデータからフォームを作成 form = CommentForm(request.POST, instance=comment) if form.is_valid(): comment = form.save(commit=False) comment.song = song comment.save() return redirect('myapp:comment_list', song_id=song_id) else: # GETの時 form = CommentForm(instance=comment) return render(request, 'myapp/comment_edit.html', dict(form=form, song_id=song_id, comment_id=comment_id))
def comment_view(request): user = check_validation(request) if user and request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): post = form.cleaned_data.get('post') email = post.user.email comment_text = form.cleaned_data.get('comment_text') comment = CommentModel.objects.create(user=user, post=post, comment_text=comment_text) comment.save() sg = sendgrid.SendGridAPIClient(apikey=sendgrid_api) from_email = Email("*****@*****.**") to_email = Email(email) subject = "Upload to win" content = Content("text/plain", user.name + " has commented on your post") mail = Mail(from_email, subject, to_email, content) sg.client.mail.send.post(request_body=mail.get()) return redirect('/feed/') else: return redirect('/feed/') else: return redirect('/login')
def comment_view(request): user = check_validation(request) if user and request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): post_id = form.cleaned_data.get('post').id comment_text = form.cleaned_data.get('comment_text') comment = CommentModel.objects.create(user=user, post_id=post_id, comment_text=comment_text) comment.save() poster = PostModel.objects.filter(id=post_id).first() subject = "Comment on your photo" message = str( user.username ) + " " + "commented on your photo" + " " + comment_text from_email = EMAIL_HOST_USER to_email = [poster.user.email] send_mail(subject, message, from_email, to_email) return redirect('/feed/') else: return redirect('/feed/') else: return redirect('/login/')
def article_vote(request, category_name, article_id, article_value): article = Article.objects.get(id=article_id) user = request.user voted = True if request.method == 'POST': comment_form = CommentForm(data=request.POST) user = request.user if comment_form.is_valid(): comment_text = comment_form.cleaned_data['comment_text'] comment = ArticleComment(user=user, article=article, comment_text=comment_text, pub_date=timezone.now()) comment.save() article.comments = article.comments + 1 article.save() posted = True else: print comment_form.errors else: comment_form = CommentForm() if request.method == 'GET' and 'search_category_button' in request.GET: search_category = request.GET.get('search_category') category_name = request.GET.get('search_name_c') if (category_name is not None) and (search_category is not None): return search_bar(request, search_category, category_name) if request.method == 'GET' and 'search_user_button' in request.GET: user_name = request.GET.get('search_name') if user_name is not None: return user_search(request, user_name) try: article_vote = ArticleVote.objects.get(user=user, article=article) already_voted = True comment_list = ArticleComment.objects.filter( article=article).order_by('-pub_date') posted = False context = { 'already_voted': already_voted, 'posted': posted, 'comment_list': comment_list, 'article': article, 'voted': voted } return render(request, 'myapp/base_article.html', context) except: article_vote = ArticleVote(user=user, article=article, pub_date=timezone.now(), value=int(article_value)) article_vote.save() article.votes = article.votes + int(article_value) article.save() already_voted = False comment_list = ArticleComment.objects.filter( article=article).order_by('-pub_date') posted = False context = { 'already_voted': already_voted, 'posted': posted, 'comment_list': comment_list, 'article': article, 'voted': voted } return render(request, 'myapp/base_article.html', context) return article(request, category_name, article_id)