예제 #1
0
def follow(request, slug):
    """Follow or unfollow a user."""
    user = user_exists(slug)
    change_count = 1
    # if request method is POST and
    # the follow/unfollow user is not the current user
    if request.method == 'POST' and user.pk != request.user.pk:
        # if request POST has follow in it
        if request.POST.get('follow'):
            # follow user
            follow = user.followed_set.create(follower=request.user)
            # add rep
            add_rep(request, fw=follow)
            # create notification
            notify(follow=follow)
        # if request POST has unfollow in it
        elif request.POST.get('unfollow'):
            # unfollow user
            follow = user.followed_set.filter(follower=request.user)[0]
            # del rep
            del_rep(request, fw=follow)
            follow.delete()
        # if current user is on another user's page
        if user.pk == int(request.POST.get('current_pk', 0)):
            followers_count = str(user.profile.followers_count())
            following_count = str(user.profile.following_count())
        # if current user is on their own page
        elif request.user.pk == int(request.POST.get('current_pk', 0)):
            followers_count = str(request.user.profile.followers_count())
            following_count = str(request.user.profile.following_count())
        # if current user is not on anyone's page
        else:
            followers_count = 0
            following_count = 0
            change_count = 0
        d = {
                'current_pk': request.POST.get('current_pk'),
                'profile_user': user,
        }
        t = loader.get_template('follows/follow_form.html')
        c = RequestContext(request, add_csrf(request, d))
        data = { 
                    'change_count': change_count,
                    'follow_form': t.render(c),
                    'followers_count': followers_count,
                    'following_count': following_count,
                    'user_id': str(user.pk),
        }
        return HttpResponse(json.dumps(data), mimetype='application/json')
    else:
        return HttpResponseRedirect(reverse('readings.views.list_user', 
            args=[profile.slug]))
예제 #2
0
def show_follows(request, slug, action):
    """Show all followers for user."""
    user = user_exists(slug)
    if action == 'followers':
        a = { 
            'users': user.profile.followers(),
        }
    elif action == 'following':
        a = { 
            'users': user.profile.following(),
        }
    elif action == 'topics':
        a = {
            'topics': user.profile.topics(),
        }
    d = {
            'current_pk': user.pk,
            'profile_user': user,
            'title': "%s's %s" % (user.first_name, action.capitalize()),
    }
    d.update(a)
    return render_to_response('follows/follows.html', d, 
        context_instance=RequestContext(request))