def add_like(): id = int(request.args.get('id')) u = current_user() t = Topic.one(id=id) user = User.one(id=t.user_id) form = {'topic_id': t.id, 'num': 1} l = Like.one(topic_id=t.id, user_id=u.id) if l is None: Like.new(form, u.id) send_like(u, user, id, '') else: Like.delete(l.id) return redirect(url_for('.detail_like', id=t.id))
def delete_like(post_id): try: # gets the post the like is related to post = Post.get(Post.id == post_id) # gets the like instance by the post and current user Like.delete().where(Like.post == post, Like.user == current_user.id).execute() return jsonify(data={}, status={ 'code': 200, 'message': 'Successfully unliked post.' }) # exception thrown if the like doesnt exist except DoesNotExist: return jsonify(data={}, status={ 'code': 404, 'message': 'Failure to get resource.' })