def unfollow(): result = "Error: get" if request.method == 'GET': #user = () args = (session['id'], int(request.args.get('user_id', None))) if not valid_args(args): return "invalid arguments", 400 #return redirect(url_for('search')) if args[1] not in session['user_follow']: return "user not followed", 404 #return redirect(url_for('search')) result = database.DB.insert( "DELETE FROM public.user_follow WHERE user_follow_id = %s AND user_followed_id = %s;", args) if result == -1: return errorDB, 500 if result != 1: return "user not found", 404 if session_followers() == -1: return errorDB, 500 session['post_likes'].append(int(args[1])) session['follow'] -= 1 return "user unfollowed", 200 return str(result), 405
def follow(): result = "Error: get" if request.method == 'GET': args = (session['id'], int(request.args.get('user_id', None))) if not valid_args(args): return 'invalid arguments', 400 #return redirect(url_for('search')) if args[1] in session['user_follow']: return "Error: already followed", 400 result = database.DB.insert( "INSERT INTO public.user_follow (user_follow_id, user_followed_id) VALUES (%s, %s);", args) if result == -1: return errorDB, 500 if result != 1: return "user not found", 404 if session_followers() == -1: return errorDB, 500 session['post_likes'].append(int(args[1])) session['follow'] += 1 args = (args[1], ) result = database.DB.select( "SELECT public.user.* FROM public.user WHERE public.user.id = %s;", args) if result == -1: return errorDB, 500 return render_template("single_user_follow.html", user=result), 200 #return redirect(url_for('back_to_search')), status_code return str(result), 405
def post_user(): status_code = OK args = (str(request.args.get('user_id', None)),) sql = "SELECT public.posts.*, public.user.name FROM public.posts LEFT JOIN public.user ON public.user.id = public.posts.user_id WHERE public.posts.user_id = %s ORDER BY public.posts.post_time DESC;" #print("ARGS:", args) if not valid_args(args): return redirect(url_for('dashboard')), ERROR result = database.DB.select(sql, args, "all") if result == -1: return errorDB, 500 return render_template('user_posts.html', posts = result)
def do_unfollow(user_id): args = (session['id'], user_id) if not valid_args(args): return -1 if args[1] not in session['user_follow']: return -2 result = database.DB.insert( "DELETE FROM public.user_follow WHERE user_follow_id = %s AND user_followed_id = %s;", args) if result == -1: return -1 #session['post_likes'].append(int(args[1])) session['follow'] -= 1 return (result)
def delete_comment(): result = 'Error: get' if request.method == 'GET': args = (session.get('id', None), request.args.get('comment_id', None), request.args.get('post_id', None)) if not valid_args(args): return ("Error: argument"), 400 result = database.DB.insert("DELETE FROM public.comments WHERE user_id = %s AND comment_id = %s AND post_id = %s;", args) if result == -1: return errorDB, 500 if result != 1: return "comment not found", 404 return "comment deleted", 200 #return redirect(url_for('blog_comment', post_id = args[2])), status_code return str(result), 405
def post_delete(): result = "ERROR: get" status_code = OK if request.method == 'GET': args = (session.get('id', None), request.args.get('post_id', None)) if not valid_args(args): return 'invalid arguments', 400 result = database.DB.insert("DELETE FROM public.posts WHERE user_id = %s AND post_id = %s;", args) if result == -1: return errorDB, 500 if result != 1: return 'post not found', 404 return "post deleted", 200 return str(result), 405
def update_comment(): result = "Error: post" status_code = OK if request.method == 'POST': args = (request.form.get('content', None), session.get('id', None), request.form.get('comment_id', None), request.form.get('post_id', None)) if not valid_args(args): return ("Error: argument"), 400 if args[0] == "": return "Empty content", 400 result = database.DB.insert("UPDATE public.comments SET comment_content = %s WHERE user_id = %s AND comment_id = %s AND post_id = %s;", args) if result == -1: return errorDB, 500 if result != 1: return "comment not found.", 404 return ("comment " + args[2] + " updated"), 200 return str(result), 400
def post_edit(): result = "Error: post" status_code = OK if request.method == 'POST': args = (request.form.get('content', None), session.get('id', None), request.form.get('post_id', None)) if not valid_args(args): return 'ERROR: arguments', 400 result = database.DB.insert("UPDATE public.posts SET post_content = %s WHERE user_id = %s AND post_id = %s;", args) if result == -1: return errorDB, 500 if result != 1: status_code = ERROR return 'ERROR: edition', 404 return "post edited", 200 #return redirect(url_for('blog')), status_code return result, 405
def session_followers(): result = "Error: get" args = (str(session.get('id', None)), ) if not valid_args(args): return 'Error: argument' result = database.DB.select( "SELECT user_followed_id FROM public.user_follow WHERE user_follow_id = %s;", args, "all") if result == -1: return -1 followed = [] if result: for row in result: followed.append(int(row[0])) session['user_follow'] = followed return 0
def post_like(): result = "Error: get" status_code = OK if request.method == 'GET': args = (str(session.get('id', None)), request.args.get('post_id', None)) if not valid_args(args): return 'Error: argument', 400 if int(args[1]) in session['post_likes']: return "Error: already liked", 400 result = database.DB.insert("INSERT INTO public.post_like (user_id, post_id) VALUES (%s, %s);", args) if result == -1: return errorDB, 500 if result != 1: status_code = ERROR session_post_likes() return 'post liked', 200 return str(result), 405
def comment_like(): result = "Error: get" status_code = OK if request.method == 'GET': post_id = request.args.get('post_id', None) args = (session.get('id', None), request.args.get('comment_id', None)) if not valid_args(args) or post_id == None: return ("Error: arguments"), 400 if int(args[1]) in session['comment_likes']: return "Error: already liked", 400 result = database.DB.insert("INSERT INTO public.comment_like (user_id, comment_id) VALUES (%s, %s);", args) session_comment_likes() #session['comment_likes'].append(args[1]) if result == -1: return errorDB, 500 if result != 1: return "comment not found", 404 return "comment " + args[1] + " liked", 200 #return redirect(url_for('blog_comment', post_id = post_id)), status_code return str(result), 405
def comment_unlike(): result = "Error: get" status_code = OK if request.method == 'GET': post_id= request.args.get('post_id', None) args = (session.get('id', None), request.args.get('comment_id', None)) if not valid_args(args) or post_id == None: return ("Error: arguments"), 400 result = database.DB.insert("DELETE FROM public.comment_like WHERE user_id = %s AND comment_id = %s;", args) if result == -1: return errorDB, 500 if result != 1: return "comment not found", 404 session_comment_likes() """if args[1] in session['comment_likes']: session['comment_likes'].remove(args[1])""" return "comment " + args[1] + " liked", 200 #return redirect(url_for('blog_comment', post_id = post_id)), status_code return str(result), 405
def post_add(): result = "error post" if request.method == 'POST': args = (session.get('id', None), request.form.get('content', None),) if not valid_args(args): return "ERROR: invalid argument", 400 if args[1] == "": return "empty post", 400 #return redirect(url_for('blog')) result = database.DB.insert("INSERT INTO public.posts (user_id, post_content) VALUES (%s, %s);", args) if result == -1: return errorDB, 500 if result == 0: return "post not found", 404 session['posts_number'] += 1 result = database.DB.select("SELECT public.posts.*, public.user.name FROM public.posts LEFT JOIN public.user ON public.user.id = public.posts.user_id ORDER BY public.posts.post_id DESC", ()) if result == -1: return errorDB, 500 return render_template("single_post.html", post = result), 200 #return redirect(url_for('blog'), code=status_code) return str(result), 405
def follows_unfollow(): result = "Error: get" status_code = 200 if request.method == 'GET': args = (session['id'], int(request.args.get('user_id', None))) if not valid_args(args): return redirect(url_for('my_follows')) if args[1] not in session['user_follow']: return redirect(url_for('my_follows')) result = database.DB.insert( "DELETE FROM public.user_follow WHERE user_follow_id = %s AND user_followed_id = %s;", args) if result == -1: return errorDB, 500 if result != 1: status_code = 404 else: session['post_likes'].append(int(args[1])) session['follow'] -= 1 return redirect(url_for('my_follows')), status_code return str(result), 405
def post_comment(): result = 'Error: post' status_code = OK if request.method == 'POST': post_id = request.form.get('post_id', None) args = (session['id'], post_id, request.form.get('comment_content', None),) if not valid_args(args): return "invalid arguments", 400 if args[2] == "": return "no comment content", 400 result = database.DB.insert("INSERT INTO public.comments (user_id, post_id, comment_content) VALUES (%s, %s, %s);", args) if result == -1: return errorDB, 500 if result != 1: return "insertion failed", 404 args = (post_id,) result = database.DB.select("SELECT public.comments.*, public.user.name FROM public.comments LEFT JOIN public.user ON public.user.id = public.comments.user_id WHERE public.comments.post_id = %s ORDER BY public.comments.created_at DESC", args) if result == -1: return errorDB, 500 return render_template('single_comment.html', post_id=post_id, comment=result), 200 return str(result), 405
def post_unlike(): result = "Error: get" status_code = OK if request.method == 'GET': args = (session.get('id', None), request.args.get('post_id', None)) if not valid_args(args): return 'Error: argument', 400 result = database.DB.insert("DELETE FROM public.post_like WHERE user_id = %s AND post_id = %s;", args) if result == -1: return errorDB, 500 if result != 1: return "post not found", 404 session_post_likes() #print("POST UNLIKE: post_id=", args[1], "ET mes likes=", session['post_likes']) '''if int(args[1]) in session['post_likes']: #print("MATCH!!!") session['post_likes'].remove(int(args[1])) if str(args[1]) in session['post_likes']: #print("MATCH!!!") session['post_likes'].remove(str(args[1])) #print("RETIRE: ", session['post_likes'])''' return 'post unliked', 200 #return redirect(url_for('blog')), status_code return str(result), 405