jtktyler
jtktyler

Reputation: 39

Django. How to restrict "profile" page to only "friends"

I currently have a "profile" page that displays specific users information they have uploaded. Currently it does so by

objects.filter(user = request.user)

Im trying to figure out how I can allow a, for lack of a better description, a "friend" to view someone else's profile. Cause right now, the user who makes the request gets their own information. I believe I know how to build the ability to "friend" another user... i just dont know how to display another users info since all ive been filtering on so far is "request.user"

Upvotes: 1

Views: 283

Answers (2)

briancaffey
briancaffey

Reputation: 2559

I would like to share how I implemented this in a project of mine. This may be somewhat specific for how I have implemented friend relationships, but I think the main idea should be the same.

Here is the view for view_profile

def view_profile(request, username):
    if request.user.username == username:
        return HttpResponseRedirect(reverse('accounts:profile'))
    #get the user we are looking at
    person = get_object_or_404(User, username=username)
    #get the userprofile
    person = person.userprofile
    person_friend_object, person_created = Friend.objects.get_or_create(current_user=person)
    user_friends = [friend for friend in person_friend_object.users.all()]
    follower_count = len(user_friends)

    friend = False

    context = {
        'person':person,
        'friend':friend,
        'follower_count':follower_count,
        'user_friends':user_friends,
    }

    if request.user.is_authenticated():
        friend_object, created = Friend.objects.get_or_create(current_user=request.user.userprofile)
        friends = [friend for friend in friend_object.users.all()]
        if person in friends:
            friend = True
        else:
            friend = False

    context['friend'] = friend

    return render(request, 'users/user_profile_view.html', context)

Then, in the template you can control what friend can see of a given user's profile with template logic. Here's a basic example:

{% if not friend %}
<p>You are not friends with this user</p><button>Add friend</button>
{% else %}
<p>You are friends with this user. Here is information about this user...(here you can show data on the user through by accessing the `person` context variable)</p><button>Unfriend</button>
{% endif %}

So everything is controlled by the friend variable which is either True or False.

There are many ways to do what you are describing, this would be just one way I believe. Hope this helps with your project.

Upvotes: 1

Anton Akhramovich
Anton Akhramovich

Reputation: 91

You can do this using Many-to-many relationships

You object should look like this

class Profile(models.Model):
    friends = models.ManyToManyField(Profile)

To check whether target profile belongs to your friend you can modify your code following way:

Profile.objects.filter(friends = request.user)

Upvotes: 1

Related Questions