示例#1
0
文件: views.py 项目: bdobosevych/SN
def view_profile(request, username):
    """
    :param request:
    :param username:
    :return:
    """
    user = ProfileService.get_profile(username)
    current = request.user.username == username
    is_following = request.user.username in user['followers']
    if request.method == "POST":
        if not current:
            if is_following:
                ProfileService.unfollow_user(
                    follower_name=request.user.username, user_name=username)
            else:
                ProfileService.follow_user(follower_name=request.user.username,
                                           user_name=username)

    user = ProfileService.get_profile(username)
    is_following = request.user.username in user['followers']
    distance = ProfileService.get_distance(user_from=request.user.username,
                                           user_to=username)
    return render(
        request, 'profile.html', {
            "user": user,
            "posts": PostService.get_posts_by_author(username),
            "current": current,
            "is_following": is_following,
            "distance": distance,
            "current_user": request.user.username,
        })
示例#2
0
def feed(request):
    if request.method == "GET":
        return render(
            request, 'feed.html', {
                "posts": PostService.get_posts_for_user(request.user.username),
                "username": request.user.username
            })
    title = request.POST.get('title')
    content = request.POST.get('content')
    if title and content:
        PostService.create_post(author=request.user.username,
                                title=title,
                                content=content)
    return render(
        request, 'feed.html', {
            "posts": PostService.get_posts_for_user(request.user.username),
            "username": request.user.username
        })
示例#3
0
def leave_comment(request, title, author):
    """
    leave comment to post by title and author
    :param request:
    :param title:
    :param author:
    :return:
    """
    result = {'added': False}
    if request.method == "POST":
        comment = request.POST.get('comment')
        if len(comment) > 0:
            PostService.add_comment(post_author=author,
                                    post_title=title,
                                    comment_author=request.user.username,
                                    comment=comment)
            result['added'] = True
            result['author'] = request.user.username
    return JsonResponse(result)
示例#4
0
def get_post(request, title, author):
    """
    get single post route
    :param request:
    :param title:
    :param author:
    :return:
    """
    post = PostService.get_post(title=title, author=author)
    return JsonResponse({'post': post})