Your users have the option (visibility) to have their emails public, private or view by members only.
How do you go about only showing the email when the visibility is set to public or if user is logged in.
class ProfileListSerializer(serializers.ModelSerializer):
"""
Profile serializer for retrieving a user instance.
"""
email = serializers.SerializerMethodField('get_email')
class Meta:
model = User
fields = ('id', 'first_name', 'last_name', visibility', 'email', )
def get_email(self, obj):
email = ''
if obj.visibility == User.VISIBILITY_PUBLIC:
email = obj.email
elif obj.visibility == User.VISIBILITY_MEMBERS:
req = self.context.get('request', None)
if req is not None and req.user.is_authenticated():
email = obj.email
return email