def replyCreate_view(request): if request.method == "POST": parent_obj = None try: account = Account.objects.get(user=request.user) parent_id = int(request.POST.get('parent_id')) content = request.POST.get('reply') post = Post.objects.get(slug=request.POST.get('post_slug')) except: parent_id = None if parent_id: parent_obj = Comment.objects.get(id=parent_id) if parent_obj: reply_comment = Comment() reply_comment.account = account reply_comment.post = post reply_comment.content = content reply_comment.parent = parent_obj reply_comment.save() post.comments += 1 post.save() # notify to the comment owner if parent_obj.account != reply_comment.account: content = reply_comment.account.user.first_name + ' ' + reply_comment.account.user.last_name + ' replied to your comment "' + reply_comment.content[: 20] + '"' notf = Notification() notf.account = parent_obj.account notf.post = post notf.content = content notf.save() # notify others who also replied to the comment avoiding repeatation... marked = [] replies = parent_obj.replies.all() for replied in replies: if replied.account.user.email not in marked: if reply_comment.account != replied.account and parent_obj.account != replied.account: # don't notify the replier him/her-self content = reply_comment.account.user.first_name + ' ' + reply_comment.account.user.last_name + ' also replied on ' + parent_obj.account.user.first_name + "'s comment " + '"' + reply_comment.content[: 20] + '"' notf = Notification() notf.account = replied.account notf.post = post notf.content = content notf.save() marked.append(replied.account.user.email) return HttpResponseRedirect(post.get_absolute_url) return HttpResponseRedirect('/posts/')
def like_view(request, slug): if request.method == "POST": post = Post.objects.get(slug=slug) account = Account.objects.get(user=request.user) qs = Like.objects.filter(post=post).filter(account=account) if not qs.exists(): like = Like() like.account = account like.post = post like.save() post.likes += 1 post.save() if like.account != post.account: notf = Notification() notf.account = post.account notf.post = post content = like.account.user.first_name + ' ' + like.account.user.last_name + ' liked your post.' notf.content = content notf.save() else: like = Like.objects.get(post=post, account=account) like.delete() post.likes -= 1 post.save() return HttpResponseRedirect(post.get_absolute_url) return HttpResponseRedirect('/posts/')
def like(request): post_id = request.POST.get('post_id', False) customer_id = request.POST.get('customer_id', False) post = Post.objects.get(id=post_id) customer = Customer.objects.get(id=customer_id) like = Like.objects.filter(post=post, customer=customer_id) if like.exists(): like.delete() nlikes = len(Like.objects.filter(post_id=post_id)) data = nlikes notifi2 = Notification.objects.filter(customer_id=customer_id, post_id=post_id, status=2) notifi2.delete() else: like2 = Like() like2.customer = customer like2.post = post like2.save() nlikes = len(Like.objects.filter(post_id=post_id)) data = nlikes notifi = Notification() notifi.customer_id = post.customer.id notifi.createtime = timezone.now() notifi.customer_user = customer notifi.description = customer.fullname + " đã yêu thích bài viết của bạn..." notifi.post = post notifi.status = 2 notifi.save() return HttpResponse(data)
def create_view(request): if request.method == "POST": try: post = Post.objects.get(slug=request.POST.get('slug')) account = Account.objects.get(user=request.user) content = request.POST.get('comment') except: return HttpResponseRedirect('/posts/') comment = Comment() comment.account = account comment.post = post comment.content = content comment.save() post.comments += 1 post.save() # notify the owner of the post if post.account != comment.account: notf = Notification() notf.account = post.account notf.post = post content = comment.account.user.first_name + ' ' + comment.account.user.last_name + ' commented on your post "' + comment.content[: 20] + '"' notf.content = content notf.save() commented_all = Comment.objects.all().filter(post=post) marked = [] # notify others who also commented for commented in commented_all: if commented.account.user.email not in marked: if comment.account != post.account and comment.account != commented.account and commented.account != post.account: notf = Notification() notf.account = commented.account notf.post = post content = comment.account.user.first_name + ' ' + comment.account.user.last_name + ' also commented ' + post.account.user.first_name + "'s post " + '"' + comment.content[: 20] + '"' notf.content = content notf.save() marked.append(commented.account.user.email) return HttpResponseRedirect(post.get_absolute_url) return HttpResponseRedirect('/posts/')
def comments(request, post_id, id_customer): post = get_object_or_404(Post, pk=post_id) customer = get_object_or_404(Customer, pk=id_customer) fullname = customer.fullname if request.method == 'POST' and request.POST['text']: cmt = request.POST['text'] if len(cmt) <= 1: data = { 'Erro': "There was an error logging you in. Please Try again" } return JsonResponse(data) if request.method == 'POST' and request.POST['text']: comment = Comment() comment.createtime = timezone.now() comment.updatetime = timezone.now() comment.post = post comment.text = request.POST['text'] comment.customer = customer comment.save() if id_customer != post.customer.id: notifi = Notification() notifi.customer_id = post.customer.id notifi.customer_user = customer notifi.createtime = timezone.now() notifi.description = customer.fullname + " đã bình luận bài viết của bạn..." notifi.post = post notifi.status = 1 notifi.save() newcoments = comment.text fullname = customer.fullname createtime = comment.createtime data = { 'datacmt': newcoments, 'dataname': fullname, 'Erro': "", 'createtime': str(createtime) } return JsonResponse(data) else: data = {'Erro': "There was an error logging you in. Please Try again"} return JsonResponse(data)