Exemplo n.º 1
0
def follow(request, username, option):
    following = get_object_or_404(User, username=username)

    try:
        f, created = Follow.objects.get_or_create(follower=request.user,
                                                  following=following)

        if int(option) == 0:
            f.delete()
            Stream.objects.filter(following=following,
                                  user=request.user).all().delete()
        else:
            posts = Post.objects.all().filter(author=following)[:25]

            with transaction.atomic():
                for post in posts:
                    stream = Stream(post=post,
                                    user=request.user,
                                    date=post.created_date,
                                    following=following)
                    stream.save()

        return HttpResponseRedirect(reverse('profile', args=[username]))
    except User.DoesNotExist:
        return HttpResponseRedirect(reverse('profile', args=[username]))
Exemplo n.º 2
0
def follow_user(request, username):
    following = get_object_or_404(User, username=username)
    follow = Follow(follower=request.user, following=following)
    follow.save()
    posts = Post.objects.all().filter(user=following)[:10]
    for post in posts:
        stream = Stream(user=request.user,
                        post=post,
                        date=post.posted,
                        following=following)
        stream.save()
    return redirect(reverse('authentification:profile', args=[username]))
Exemplo n.º 3
0
def follow(request, username, option):
	user=request.user
	following = get_object_or_404(User, username=username)

	try:
		f, created = Follow.objects.get_or_create(follower=user, following=following)

		if int(option) == 0:
			f.delete()
			Stream.objects.filter(following=following, user=user).all().delete()
		else:
			 posts = Post.objects.all().filter(user=following)[:25]

			#database transaction using atomic
			 with transaction.atomic():
				 #creates multiple objects for the posts when we follow a user
			 	for post in posts:
			 		stream = Stream(post=post, user=user, date=post.posted, following=following)
			 		stream.save()

		return HttpResponseRedirect(reverse('profile', args=[username]))
	except User.DoesNotExist:
		return HttpResponseRedirect(reverse('profile', args=[username]))