Skip to content

Commit

Permalink
follow action fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii committed Jan 1, 2024
1 parent 0843c49 commit db75e12
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 54 deletions.
3 changes: 0 additions & 3 deletions web/actions/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@

class ActionsConfig(AppConfig):
name = 'actions'

def ready(self):
import actions.signals # noqa: F401
9 changes: 1 addition & 8 deletions web/api/v1/actions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ class UserFollowSerializer(serializers.ModelSerializer):

profile_url = serializers.URLField(source='get_absolute_url')

follow = serializers.SerializerMethodField('get_follow_status')

def get_follow_status(self, obj) -> Optional[FollowStatus]:
user = self.context['request'].user
if user == obj:
return None
is_follow = FollowService(user=user, user_id=obj.id).is_user_subscribed()
return FollowStatus.UNFOLLOW.value if is_follow else FollowStatus.FOLLOW.value
follow = serializers.BooleanField()

class Meta:
model = User
Expand Down
14 changes: 0 additions & 14 deletions web/api/v1/actions/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,6 @@ def subscribe(self) -> bool:
return follow_status.value


class FollowersQueryService:
def get_user_by_id(self, user_id: int) -> User:
try:
return User.objects.get(id=user_id)
except User.DoesNotExist:
raise NotFound(_('Requested user does not exist'))

def get_user_followers(self, user: User) -> QuerySet[User]:
return user.followers.all()

def get_user_following(self, user: User) -> QuerySet[User]:
return user.following.all()


class ActivityService:
def __init__(self, action: ActionFeed, user: User, content_object):
self.action = action
Expand Down
9 changes: 5 additions & 4 deletions web/api/v1/actions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

from actions.models import Action

from ..profile.services import UserQueryService
from . import serializers
from .services import FollowersQueryService, FollowService, LikeService
from .services import FollowService, LikeService
from main.pagination import BasePageNumberPagination

swagger_tags = ['Like']
Expand Down Expand Up @@ -47,16 +48,16 @@ class UserFollowersView(ListModelMixin, GenericViewSet):
pagination_class = BasePageNumberPagination

def get_queryset(self):
service = FollowersQueryService()
service = UserQueryService()
if self.action == 'user_followers':
return service.get_user_followers(self.request.user)
elif self.action == 'user_following':
return service.get_user_following(self.request.user)
elif self.action == 'user_followers_by_id':
user = service.get_user_by_id(self.kwargs['user_id'])
user = service.get_simple_user(self.kwargs['user_id'])
return service.get_user_followers(user)
elif self.action == 'user_following_by_id':
user = service.get_user_by_id(self.kwargs['user_id'])
user = service.get_simple_user(self.kwargs['user_id'])
return service.get_user_following(user)

def user_followers(self, request):
Expand Down
18 changes: 15 additions & 3 deletions web/api/v1/profile/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,23 @@ def user_profile_queryset(self) -> 'QuerySet[User]':
user_likes = Count('likes')
return self.get_queryset(is_active=True).annotate(user_posts=user_articles, user_likes=user_likes)

@staticmethod
def exist_annotation(user) -> Exists:
return Exists(Follower.objects.filter(subscriber=user, to_user_id=OuterRef('pk')))

def user_list_queryset(self, current_user: User) -> 'QuerySet[User]':
return self.user_profile_queryset().annotate(
follow=Exists(Follower.objects.filter(subscriber=current_user, to_user_id=OuterRef('pk')))
)
return self.user_profile_queryset().annotate(follow=self.exist_annotation(current_user))

@except_shell((User.DoesNotExist,), raise_404=True)
def get_user_profile(self, user_id: int) -> User:
return self.user_profile_queryset().get(id=user_id)

@except_shell((User.DoesNotExist,), raise_404=True)
def get_simple_user(self, user_id: int) -> User:
return self.get_queryset().get(id=user_id)

def get_user_followers(self, user: User) -> 'QuerySet[User]':
return user.followers.all().annotate(follow=self.exist_annotation(user))

def get_user_following(self, user: User) -> 'QuerySet[User]':
return user.following.all().annotate(follow=self.exist_annotation(user))
39 changes: 17 additions & 22 deletions web/user_profile/static/user_profile/js/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,23 @@ function renderModal(data, button) {
}

function followBodyRender(data, button) {
user_list = data.results
let body = $('#followModalBody')
let followUrl = button.data('follow-actions')

const body = $('#followModalBody')
const followUrl = button.data('follow-actions')
body.empty()
$.each(user_list, function(i){
let isShowFollowButton = !!user_list[i].follow
console.log(isShowFollowButton)
var templateString = `
<div class="user">
<p>
<img src="${user_list[i].avatar}" class="avatar img-circle img-thumbnail" width=50px>
<a href='${user_list[i].profile_url}'> ${user_list[i].full_name} </a>
${isShowFollowButton ? `
<button class="btn btn-primary followMe" data-id="${user_list[i].id}" data-href='${followUrl}'> ${user_list[i].follow} </button>
` : ''}
</p>
</div>
`
body.append(templateString);
})
$(".followMe").click(followMe);
const template = data.results.map((user) => userModalTemplate(user)).join('');
body.append(template);
$(".followMe").click(followMe);
}


function userModalTemplate(user) {
return `
<div class="user">
<p>
<img src="${user.avatar}" class="avatar img-circle img-thumbnail" width=50px>
<a href='${user.profile_url}'> ${user.full_name} </a>
<button class="btn btn-primary followMe" data-id="${user.id}"> ${getButtonText(user.follow)} </button>
</p>
</div>
`
}

0 comments on commit db75e12

Please sign in to comment.