コード例 #1
0
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/')
コード例 #2
0
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/')