def follow(request): # called when any user follows other user if request.user.is_authenticated: print("Followings user...") if request.POST: user_id = request.POST.get('id') print("user id is :", user_id) user_profile = get_object_or_404(Profile, user=user_id) print("check 2") logged_in_user_profile = get_object_or_404(Profile, user=request.user.id) print("This page belongs to ", user_profile) print("Loged in User is ", logged_in_user_profile) if logged_in_user_profile.follows.filter(id=user_profile.id).exists(): logged_in_user_profile.follows.remove(user_profile) logger.info(str(request.user) + " Stopped following " + str(user_profile)) remove_notify(logged_in_user_profile, user_profile, "Started Following You", 30, reverse('profile', args=[logged_in_user_profile.id])) else: print("Not folloed") logged_in_user_profile.follows.add(user_profile) logger.info(str(request.user) + " Started following " + str(user_profile)) notify(logged_in_user_profile, user_profile, "Started Following You", 30, reverse('profile', args=[logged_in_user_profile.id])) context = { 'user_profile': user_profile, 'logged_in_user_profile': logged_in_user_profile, } print("End of Post request") if request.is_ajax(): follow_button = render_to_string('follow_section.html', context, request=request) return JsonResponse({'follow_button': follow_button}) else: logger.critical(" Unauthenticated user tries to access the secured URL") return HttpResponseRedirect(reverse('login'))
def like_post(request): if request.user.is_authenticated: all_posts = Posts.objects.all() print("Insisde Like Post") print('ID coming from form is', request.POST.get('id')) post = get_object_or_404(Posts, id=request.POST.get('id')) # for AJAX call user_profile = Profile.objects.get(user=request.user) comments = Comment.objects.all().filter(post=post) context = {'all_posts': all_posts, 'post': post} if post.likes.filter(id=request.user.id).exists(): post.likes.remove(request.user) # Disliking The Post logger.info( str(request.user) + " Removed like from Thought : " + str(post.title)) remove_notify(user_profile, post.user_profile, "Liked Your Post", 30, reverse('details_post', args=[post.id])) update_trending_ratio(post, comments) update_level_by_like(post, "decrease") # print("DisLiking the post") else: post.likes.add(request.user) logger.info( str(request.user) + " Liked a Thought : " + str(post.title)) notify(user_profile, post.user_profile, "Liked Your Post", 30, reverse('details_post', args=[post.id])) update_trending_ratio(post, comments) update_level_by_like(post, "increase") post.dis_likes.remove(request.user) # print("Liking the post") if request.is_ajax(): print('Hey its an AJAX calls') # TEsting AJAX request html = render_to_string('like_section.html', context, request=request) html2 = render_to_string('dis_like_section.html', context, request=request) return JsonResponse({'like_form': html, 'dis_like_form': html2}) else: logger.critical( " Unauthenticated user tries to access the secured URL ") return HttpResponseRedirect(reverse('login'))
def update_level_by_post(all_posts, user_profile, status): print("Inside update level by post...") post_count = all_posts.filter( user_profile=user_profile).values("likes").count() print("User : "******" has ", post_count, " Posts") print("I have ", post_count, "Right now...") if post_count > 20: return elif status == "increase": if post_count == 1: print("Increaseing level by 1") user_profile.level += 1 logger.info("Increaseing Level of " + str(user_profile.user) + " by 1") notify(user_profile, user_profile, "Your Level is Updated By 1", 40, reverse('home')) elif post_count == 5: print("Increaseing level by 5") user_profile.level += 5 logger.info("Increaseing Level of " + str(user_profile.user) + " by 5") notify(user_profile, user_profile, "Your Level is Updated By 5", 40, reverse('home')) elif post_count == 20: print("Increaseing level by 10") user_profile.level += 10 logger.info("Increaseing Level of " + str(user_profile.user) + " by 10") notify(user_profile, user_profile, "Your Level is Updated By 10", 40, reverse('home')) elif status == "decrease": if user_profile.level > 2: if post_count == 0: print("Decreaseing level by 1") user_profile.level -= 1 logger.info("Decreaseing Level of " + str(user_profile.user) + " by 1") elif post_count == 4: print("Decreaseing level by 5") logger.info("Decreaseing Level of " + str(user_profile.user) + " by 2") user_profile.level -= 5 elif post_count == 19: print("Decreaseing level by 10") user_profile.level -= 10 logger.infso("Decreaseing Level of " + str(user_profile.user) + " by 10") user_profile.save()
def update_level_by_like(post, status): print("Inside update level by like...") user_profile = post.user_profile likes = ( Posts.objects.filter( user_profile=user_profile) # filtering the post of specific user .annotate(likes_count=Count('likes')) # counting likes on each post .aggregate(total_likes=Sum( 'likes_count')) # Summing likes on each post to give total likes ) print("I have ", likes.get('total_likes'), "Right now...") if likes.get('total_likes') > 25: return elif status == "increase": if likes.get('total_likes') == 1: print("Increaseing level by 2") user_profile.level += 2 logger.info("Increaseing Level of " + str(user_profile.user) + " by 2") notify(user_profile, user_profile, "Your Level is Updated By 2", 40, reverse('home')) elif likes.get('total_likes') == 10: print("Increaseing level by 5") user_profile.level += 5 logger.info("Increaseing Level of " + str(user_profile.user) + " by 5") notify(user_profile, user_profile, "Your Level is Updated By 5", 40, reverse('home')) elif likes.get('total_likes') == 25: print("Increaseing level by 10") user_profile.level += 10 logger.info("Increaseing Level of " + str(user_profile.user) + " by 10") notify(user_profile, user_profile, "Your Level is Updated By 10", 40, reverse('home')) elif likes.get('total_likes') % 10 == 0: print("Increaseing level by 1") user_profile.level += 1 logger.info("Increaseing Level of " + str(user_profile.user) + " by 1") notify(user_profile, user_profile, "Your Level is Updated By 1", 40, reverse('home')) user_profile.save() elif status == "decrease": if likes.get('total_likes') == 0: print("Decreaseing level by 2") user_profile.level -= 2 logger.info("Decreaseing Level of " + str(user_profile.user) + " by 2") remove_notify(user_profile, user_profile, "Your Level is Updated By 2", 40, reverse('home')) elif likes.get('total_likes') == 9: print("Decreaseing level by 5") user_profile.level -= 5 logger.info("Decreaseing Level of " + str(user_profile.user) + " by 5") remove_notify(user_profile, user_profile, "Your Level is Updated By 5", 40, reverse('home')) elif likes.get('total_likes') == 24: print("Decreaseing level by 10") user_profile.level -= 10 logger.info("Decreaseing Level of " + str(user_profile.user) + " by 10") remove_notify(user_profile, user_profile, "Your Level is Updated By 10", 40, reverse('home')) elif likes.get('total_likes') % 9 == 0: print("Decreaseing level by 1") user_profile.level -= 1 logger.info("Decreaseing Level of " + str(user_profile.user) + " by 1") remove_notify(user_profile, user_profile, "Your Level is Updated By 1", 40, reverse('home')) user_profile.save()