コード例 #1
0
    def like_or_unlike(request):
        """
        User like a post or unlikes a post
        :param request: HttpResponse
        :return: JsonResoponse
        """
        status, msg = False, "Invalid Request"

        if request.user.is_authenticated:
            post_id = json.loads(request.body)["post_id"]
            type_of_post = json.loads(request.body)["type_of_post"].upper()

            l = Like_DB.objects.filter(username=request.user.username,
                                       post_id=post_id).first()

            if l:
                l.delete()
                status, msg = True, "Success"
            else:
                l = Like_DB(username=request.user.username,
                            type_of_post=type_of_post,
                            post_id=post_id)
                l.save()
                status, msg = True, "Success"

        return JsonResponse({"status": status, "msg": msg})
コード例 #2
0
def handle_likes():
    payload = json.loads(request.data)
    likes = Likes(**payload)

    db.session.add(likes)
    db.session.commit()

    return jsonify(likes.serialize()), 201
コード例 #3
0
ファイル: views.py プロジェクト: IvoPintodaSilva/ubiChallenge
def likes_add(request):
	user_email = request.GET.get('dropdown_user', '')
	user = get_object_or_404(User, pk = user_email)
	song_id = request.GET.get('dropdown_song', '')
	song = get_object_or_404(Song, pk = int(song_id))
	likes = Likes(user = user, song = song)
	likes.save()
	context={}
	return render(request, 'webapp/likes_added.html', context)
コード例 #4
0
ファイル: views.py プロジェクト: IvoPintodaSilva/ubiChallenge
def likes_add_onuserdetails(request):
	user_email = request.GET.get('user_email', '')
	user = get_object_or_404(User, pk = user_email)
	song_id = request.GET.get('dropdown_song','')
	song = get_object_or_404(Song, pk = int(song_id))
	likes = Likes(user = user, song = song)
	"""  Check if like already exists  """
	if len(Likes.objects.all().filter(song__id__iexact = song_id).filter(user__email__iexact = user_email)) == 0:
		likes.save()
	likes_list = Likes.objects.filter(user = user)
	songs_to_exclude = [l.song.id for l in likes_list]
	song_list = Song.objects.all().exclude(id__in = songs_to_exclude)
	context = {'user':user, 'likes_list':likes_list, 'song_list':song_list}
	return render(request, 'webapp/user_details.html', context)