Ejemplo n.º 1
0
def follow(request, following_id):
	if request.user.is_authenticated():
		visited_user = User.objects.get(id=following_id)
		follower_user = request.user
		following_user = User.objects.get(id=following_id)
		f = Follower(follower=follower_user, followed=following_user)
		f.save()
		return render(request, 'users/user.html', {'current_user': request.user, 'visited_user': visited_user, 'microposts': visited_user.micropost_set.order_by('-pub_date')[:5], 'followers_num': len(visited_user.profile.followers()), 'following_num': len(visited_user.profile.following()), 'microposts_num': visited_user.micropost_set.all().count})
	else:
		return render(request, 'users/index.html')
Ejemplo n.º 2
0
 def get(self, request, *args, **kwargs):
     followed_id = int(self.kwargs.get('id', ''))
     if followed_id and self.request.user.id != followed_id:
         try:
             obj = Follower.objects.get(user=self.request.user,
                                        followed_id=followed_id)
         except Follower.DoesNotExist:
             obj = Follower(user=self.request.user, followed_id=followed_id)
             obj.save()
             return redirect('settings')
     return redirect('settings')
Ejemplo n.º 3
0
def update_status(request, pk):
    action = request.POST.get('sbr', '')

    if action == 'Follow':
        following = User.objects.get(id=pk)
        follower = Follower(follower=request.user, following=following)
        follower.save()
    else:
        following = User.objects.get(id=pk)
        Follower.objects.filter(follower=request.user,
                                following=following).delete()

    user = User.objects.all().get(id=pk)
    context = {'user': user, 'followers': Follower.objects.all()}
    return render(request, 'users/user_details.html', context)
Ejemplo n.º 4
0
def follow(request):
    if request.method == 'GET':
        user_id = request.GET['user_id']
        usertofollow = User.objects.get(
            pk=user_id)  # getting the user to follow

        if Follower.objects.filter(being_followed=usertofollow,
                                   follower=request.user).exists():
            Follower.objects.filter(being_followed=usertofollow,
                                    follower=request.user).delete()
        else:
            m = Follower(being_followed=usertofollow,
                         follower=request.user)  # creating like object
            m.save()  # saves into database
        return HttpResponse(usertofollow.being_followeds.count())
    else:
        return HttpResponse("Request method is not a GET")
Ejemplo n.º 5
0
 def post(self, request, *args, **kwargs):
     if 'follow_user' in request.POST:
         id_user = request.POST.get('id_user')
         profile = Profile.objects.filter(pk=id_user)
         if profile.count() != 0:
             follower = Follower(user=request.user.profile,
                                 account=profile[0])
             follower.save()
             return redirect('/users/%s/' % profile[0].user)
     elif 'unfollow_user' in request.POST:
         id_user = request.POST.get('id_user')
         profile = Profile.objects.filter(pk=id_user)
         if profile.count() != 0:
             follower = Follower.objects.filter(user=request.user.profile,
                                                account=profile[0])
             if follower.count() != 0:
                 follower[0].delete()
             return redirect('/users/%s/' % profile[0].user)
Ejemplo n.º 6
0
 def post(self, request, username, *args, **kwargs):
     if request.method == "POST":
         if request.is_ajax():
             data = request.POST.get('type', None)
             if data == "follow-button":
                 user_in_question = request.POST.get(
                     'user_in_question', None)
                 user_in_question = User.objects.get(
                     username=user_in_question)
                 try:
                     follow = Follower.objects.get(
                         follower=request.user, followed=user_in_question)
                     follow.delete()
                     return HttpResponse("unfollowed")
                 except:
                     follow = Follower(follower=request.user,
                                       followed=user_in_question)
                     follow.save()
                     return HttpResponse("followed")
Ejemplo n.º 7
0
 def post(self, request, pk, *args, **kwargs):
     if request.method == "POST" and request.is_ajax():
         data = request.POST.get('type', None)
         if data == "info-button":
             group = Group.objects.get(pk=pk)  #Gets the group
             html = render_to_string('blog/message_info.html', {
                 'group': group,
                 'request': request
             })
             return HttpResponse(html)
         elif data == "follow-button":
             user_in_question = request.POST.get('user_in_question', None)
             user_in_question = User.objects.get(username=user_in_question)
             try:
                 follow = Follower.objects.get(follower=request.user,
                                               followed=user_in_question)
                 follow.delete()
                 return HttpResponse("unfollowed")
             except:
                 follow = Follower(follower=request.user,
                                   followed=user_in_question)
                 follow.save()
                 return HttpResponse("followed")
Ejemplo n.º 8
0
def profileFollowButton(self, request, username):
    if request.method == "POST" and request.is_ajax():
        user = User.objects.get(username=username)
        try:
            follow = Follower.objects.get(follower=request.user, followed=user)
            follow.delete()
            return "unfollowed"
        except:
            follow = Follower()
            follow.follower = request.user
            follow.followed = user
            follow.save()
            return "followed"
    return "INCORRECT"