def like_post(post_id,ip): db = get_db() cursor = db.cursor() query = "SELECT id from likes WHERE post_id='{0}' and user_ip='{1}'" likes = cursor.execute(query.format(post_id, ip)).fetchall() if len(likes) == 0: db.execute( "INSERT INTO likes (post_id, user_ip) VALUES (?, ?)", (post_id, ip) ) db.commit()
def like(): if request.method == "POST": session_post_user = request.form["session_post_user"] post_id = request.form["post_id"] db = get_db() db.execute( "INSERT INTO post_likes (session_post_user, post_id) VALUES (?, ?)", (session_post_user, post_id)) db.commit() return redirect("/")
def get_posts(to_json = False): db = get_db() if to_json: db.row_factory = dict_factory cursor = db.cursor() query = "SELECT rowid, id, title, description, color, created, (SELECT COUNT(id) from likes WHERE post_id=post.id) as likes,(SELECT COUNT(id) from likes ) as total_likes FROM post ORDER BY created DESC" rows = cursor.execute(query).fetchall() sorted = sorting(rows) return sorted
def like_post(): if request.method == "POST": user_session = request.form["user_session"] post_id = request.form["post_id"] db = get_db() db.execute( "INSERT INTO likes (user_session, post_id) VALUES (?, ?)", (user_session, post_id) ) db.commit() posts = get_posts(user_session, True) return json.dumps(posts)
def add(): if request.method == "GET": return render_template("board/add.html") else: title = request.form["title"] description = request.form["description"] color = request.form["color"] db = get_db() db.execute( "INSERT INTO post (title, description, color) VALUES (?, ?, ?)", (title, description, color)) db.commit() return redirect(url_for("board.list"))
def get_posts(session, to_json=False): db = get_db() if to_json: db.row_factory = db_factory cursor = db.cursor() query = """SELECT rowid, *, (SELECT COUNT(*) from post_likes WHERE post_id=post.rowid) as likes, (SELECT COUNT(*) from post_likes WHERE post_id=post.rowid AND session_post_user='******') as my_likes FROM post ORDER BY created DESC""" rows = cursor.execute(query.format(session)).fetchall() sorting = sort_posts(rows) return sorting
def get_posts(session, to_json = False): db = get_db() if to_json: db.row_factory = dict_factory cursor = db.cursor() query = """SELECT rowid, *, (SELECT COUNT(*) from likes WHERE post_id=post.rowid) as like_count, (SELECT COUNT(*) from likes WHERE post_id=post.rowid AND user_session='{0}') as liked_by_me FROM post ORDER BY created DESC""" rows = cursor.execute(query.format(session)).fetchall() sorted_by_popularity = popularity_sorting(rows) return sorted_by_popularity
def add_like(): if request.method == "GET": db = get_db() user = str(os.getlogin()) #Problem: Konnte die postId bzw den Loop Index nicht von jinja/html extrahieren #post_id = request.args.get("{{loop.index}}") post_id = 2 db.execute("UPDATE post SET likes = likes + 1 WHERE id = ?", [post_id]) db.execute("UPDATE post SET liked_by_user = ? WHERE id = ?", [user, post_id]) db.commit() return redirect(url_for("board.list")) else: return render_template("board/list.html")
def list(): db = get_db() posts = get_all_posts(db) liked_posts_str = {} if request.cookies.get("ids"): liked_posts_str = json.loads(request.cookies.get("ids")) liked_posts_int = [] for id in liked_posts_str: liked_posts_int.append(int(id)) if request.method == "GET": posts.sort(key=evaluate_prio, reverse=True) return render_template("board/list.html", posts=posts, liked_posts=liked_posts_int) else: if request.form["post_id"] in liked_posts_str: del liked_posts_str[request.form["post_id"]] db.execute("UPDATE post SET likes = likes -1 WHERE id= ? ", request.form["post_id"]) else: liked_posts_str[request.form["post_id"]] = request.form["post_id"] db.execute("UPDATE post SET likes = likes +1 WHERE id= ? ", request.form["post_id"]) liked_posts_int = [] for id in liked_posts_str: liked_posts_int.append(int(id)) posts = get_all_posts(db) db.commit() posts.sort(key=evaluate_prio, reverse=True) resp = make_response( render_template("board/list.html", posts=posts, liked_posts=liked_posts_int)) resp.set_cookie("ids", json.dumps(liked_posts_str)) return resp
def add(): if request.method == "GET": return render_template("board/add.html") else: title = request.form["title"] description = request.form["description"] color = request.form["color"] #likes = request.form["likes"] if request.args.get("vote"): post.likes = post.likes + 1 post.save() #likes = request.form["likes"] return redirect("/add/{post_id}".format(post_id=post_id)) db = get_db() db.execute( "INSERT INTO post (title, description, color) VALUES (?, ?, ?)", (title, description, color) ) db.commit() return redirect(url_for("board.list"))
def list(): db = get_db() #likes = db.IntegerField(default=0) posts = db.execute("SELECT * FROM post ORDER BY created DESC").fetchall() return render_template("board/list.html", posts=posts)
def list(): db = get_db() posts = db.execute("SELECT * FROM post").fetchall() return render_template("board/list.html", posts=posts)
def delete_user_session(): db = get_db() posts = db.execute("UPDATE post SET liked_by_user = '******' ") return redirect(url_for("board.list"))
def sort_like(): db = get_db() posts = db.execute("SELECT * FROM post ORDER BY likes DESC").fetchall() return redirect(url_for("board.list"))