Privacy settings like Facebook in Django.

46 views
Skip to first unread message

Alex Richards

unread,
Feb 22, 2017, 7:14:48 AM2/22/17
to Django users
I want to implement privacy settings on my extended UserProfile Model where the user can decide who can see his field (say email for now).
The options looks something like this,

public - any one can view his email
friends_of_friends - only friends and friends of friends can view his email
friends - only his friends can view his email
friend_list - only a list of friends (pre defined) can view his email.

I want to implement the same for n fields in the Model (phone number, work, education, etc..).

I don't need code but some directions to solve it. Code examples won't hurt. 

C. Kirby

unread,
Mar 1, 2017, 4:14:51 AM3/1/17
to Django users
I would create an abstract class to hold the share values, extend from that for your n shared fields, use those as OneToOne in your user profile, and then have methods in the userprofile to determine if the field should be shown. Something like this (assumes you have the code to figure out if a user is public, fof, friend, or friend list)

class SharedField(models.Model):
    share_with
= models.IntegerField(choices=((1,'Public'),(2,'FoF'),(3,'Friend'),(4,'Friend List')))

   
class Meta:
       
abstract = True


class Email(SharedField):
    email
= models.EmailField()


class UserProfile(models.Model):
    email
= models.OneToOne(Email)

   
def get_relationship_level(self, request_user):
       
if request_user in self.friend_list:
       
return 4
       
elif request_user in self.friends:
           
return 3
       
elif request.user in self.fof:
           
return 2
       
else:
           
return 1

   
def get _email_for_user(self, request_user):
       friend_level
= self.get_relationship_level(request_user)
       
if friend_level >= self.email.share_with:
           
return self.email.email
       
return None
Reply all
Reply to author
Forward
0 new messages