Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block user feature #175

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions feed/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

from .models import Mumble, MumbleVote
from .serializers import MumbleSerializer

# Create your views here.


@api_view(['GET'])
@permission_classes((IsAuthenticated,))
@permission_classes((IsAuthenticated, ))
def mumbles(request):
query = request.query_params.get('q')
if query == None:
query = ''

user = request.user
following = user.following.select_related('user')
blocked_users_by_request_user = user.userprofile.blocked_users.all()

following = user.following.all()
following = user.following.all().exclude()

ids = []
ids = [i.user.id for i in following]
Expand All @@ -43,15 +43,21 @@ def mumbles(request):
#Add top ranked mumbles to feed after prioritizing follow list
index = 0
for mumble in recentMumbles:
if mumble not in mumbles:
mumbles.insert(index, mumble)
index += 1
# check if user is not blocked
if mumble.user not in blocked_users_by_request_user:
if user not in mumble.user.userprofile.blocked_users.all():
if mumble not in mumbles:
mumbles.insert(index, mumble)
index += 1


#Add top ranked mumbles to feed after prioritizing follow list
for mumble in topMumbles:
if mumble not in mumbles:
mumbles.append(mumble)
# check if user is not blocked
if mumble.user not in blocked_users_by_request_user:
if user not in mumble.user.userprofile.blocked_users.all():
if mumble not in mumbles:
mumbles.append(mumble)


paginator = PageNumberPagination()
Expand Down
20 changes: 20 additions & 0 deletions users/migrations/0005_userprofile_blocked_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2 on 2021-06-09 17:06

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('users', '0004_auto_20210517_1436'),
]

operations = [
migrations.AddField(
model_name='userprofile',
name='blocked_users',
field=models.ManyToManyField(blank=True, null=True, related_name='blocked', to=settings.AUTH_USER_MODEL),
),
]
20 changes: 20 additions & 0 deletions users/migrations/0006_alter_userprofile_blocked_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 3.2 on 2021-06-10 09:29

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('users', '0005_userprofile_blocked_users'),
]

operations = [
migrations.AlterField(
model_name='userprofile',
name='blocked_users',
field=models.ManyToManyField(blank=True, related_name='blocked', to=settings.AUTH_USER_MODEL),
),
]
1 change: 1 addition & 0 deletions users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class UserProfile(models.Model):
skills = models.ManyToManyField(SkillTag, related_name='personal_skills', blank=True)
interests = models.ManyToManyField(TopicTag, related_name='topic_interests', blank=True)
followers = models.ManyToManyField(User, related_name='following', blank=True)
blocked_users = models.ManyToManyField(User, related_name='blocked', blank=True)
email_verified = models.BooleanField(default=False)
id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
"""
Expand Down
2 changes: 1 addition & 1 deletion users/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class UserSerializer(serializers.ModelSerializer):
profile = serializers.SerializerMethodField(read_only=True)
class Meta:
model = User
fields = ['id', 'profile', 'username', 'is_superuser', 'is_staff']
fields = ['id', 'profile', 'username', 'is_superuser', 'is_staff',]

def get_profile(self, obj):
profile = obj.userprofile
Expand Down
1 change: 1 addition & 0 deletions users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
path('profile_update/interests/', views.update_interests, name='update_interests'),
path('profile_update/photo/', views.ProfilePictureUpdate.as_view(), name="profile_update_photo"),
path('<str:username>/follow/', views.follow_user, name="follow-user"),
path('<str:username>/block/', views.block_user, name="block-user"),
path('delete-profile/', views.delete_user, name="delete-user"),
path('profile_update/delete/', views.ProfilePictureDelete, name="profile_delete_photo"),
path('<str:username>/', views.user, name="user"),
Expand Down
38 changes: 36 additions & 2 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ def users(request):
Q(userprofile__name__icontains=query) |
Q(userprofile__username__icontains=query)
).order_by('-userprofile__followers_count')
# check if user is not blocked
for user in list(users):
if user in request.user.userprofile.blocked_users.all():
if request.user in user.userprofile.blocked_users.all():
users.remove(user)
paginator = PageNumberPagination()
paginator.page_size = 10
result_page = paginator.paginate_queryset(users,request)
Expand All @@ -137,6 +142,13 @@ def users_recommended(request):
def user(request, username):
user = User.objects.get(username=username)

# if request user is trying to search for a user who has blocked him/her. We won't let them find their profile.
if request.user in user.userprofile.blocked_users.all():
return Response({'detail':'Account not Found'},status=status.HTTP_200_OK)

if user in request.user.userprofile.blocked_users.all():
return Response({'detail':'Unblock Account to view'},status=status.HTTP_200_OK)

if(request.user.username == username):
serializer = CurrentUserSerializer(user, many=False)
return Response(serializer.data)
Expand All @@ -147,13 +159,21 @@ def user(request, username):
@api_view(['GET'])
def user_mumbles(request, username):
user = User.objects.get(username=username)
if request.user in user.userprofile.blocked_users.all():
return Response({'detail':'Account not Found'},status=status.HTTP_200_OK)
if user in request.user.userprofile.blocked_users.all():
return Response({'detail':'Unblock Account to view its mumbles'},status=status.HTTP_200_OK)
mumbles = user.mumble_set.filter(parent=None)
serializer = MumbleSerializer(mumbles, many=True)
return Response(serializer.data)

@api_view(['GET'])
def user_articles(request, username):
user = User.objects.get(username=username)
if request.user in user.userprofile.blocked_users.all():
return Response({'detail':'Account not Found'},status=status.HTTP_200_OK)
if user in request.user.userprofile.blocked_users.all():
return Response({'detail':'Unblock Account to view its articles'},status=status.HTTP_200_OK)
articles = user.article_set
serializer = ArticleSerializer(articles, many=True)
return Response(serializer.data)
Expand Down Expand Up @@ -208,7 +228,8 @@ def follow_user(request, username):

if user == user_to_follow:
return Response('You can not follow yourself')

if user in user_to_follow_profile.blocked_users.all():
return Response('User Not Found')
if user in user_to_follow_profile.followers.all():
user_to_follow_profile.followers.remove(user)
user_to_follow_profile.followers_count = user_to_follow_profile.followers.count()
Expand Down Expand Up @@ -289,7 +310,20 @@ def ProfilePictureDelete(request):
user.profile_pic.url = 'default.png'
return Response({'detail':'Profile picture deleted '})


@api_view(['POST'])
@permission_classes((IsAuthenticated,))
def block_user(request, username):
user = request.user
toblock_user = User.objects.get(username=username)
# Block User
if toblock_user not in user.userprofile.blocked_users.all():
user.userprofile.followers.remove(toblock_user)
user.userprofile.blocked_users.add(toblock_user)
return Response({'detail':'Account blocked successfully'},status=status.HTTP_200_OK)
# Unblock User
if toblock_user in user.userprofile.blocked_users.all():
user.userprofile.blocked_users.remove(toblock_user)
return Response({'detail':'Account un-blocked successfully'},status=status.HTTP_200_OK)

@api_view(['POST'])
@permission_classes((IsAuthenticated,))
Expand Down