def post_vote(): conn = mysql.get_db() cursor = conn.cursor() try: req_json = request.get_json() except BadRequest: return jsonify(code=2, response="Cant parse json") if not ('post' in req_json and 'vote' in req_json): return jsonify(code=3, response="Wrong request") post_id = req_json['post'] vote_value = req_json['vote'] cursor.execute("SELECT likes, dislikes, points FROM Post WHERE id='%s'" % post_id) likes_info = cursor.fetchall() if not likes_info: return jsonify(code=1, response="No such thread") likes = likes_info[0][0] dislikes = likes_info[0][1] points = likes_info[0][2] if vote_value == 1: likes += 1 points += 1 elif vote_value == -1: dislikes += 1 points -= 1 else: return jsonify(code=3, response="Wrong request") sql_update = (likes, dislikes, points, post_id) cursor.execute("UPDATE Post SET likes=%s, dislikes=%s, points=%s WHERE id=%s", sql_update) conn.commit() cursor.execute("SELECT * FROM Post WHERE id='%s'" % post_id) updated_thread = cursor.fetchall() resp = get_post_info_by_post(cursor, updated_thread[0]) return jsonify(code=0, response=resp)
def post_update(): conn = mysql.get_db() cursor = conn.cursor() try: req_json = request.get_json() except BadRequest: return jsonify(code=2, response="Cant parse json") if not ('post' in req_json and 'message' in req_json): return jsonify(code=3, response="Wrong request") post_id = req_json['post'] new_message = req_json['message'] cursor.execute("UPDATE Post SET message=%s WHERE id=%s", (new_message, post_id)) conn.commit() cursor.execute("SELECT * FROM Post WHERE id='%s'" % post_id) post_info = cursor.fetchall()[0] if not post_info: return jsonify(code=1, response="Can't find this post") resp = get_post_info_by_post(cursor, post_info) return jsonify(code=0, response=resp)
def post_list(): conn = mysql.get_db() cursor = conn.cursor() forum_short_name = request.args.get('forum') thread_id = request.args.get('thread') if not (forum_short_name or thread_id): return jsonify(code=3, response="Wrong request") since = request.args.get('since') if not since: since = "1970-01-01 00:00:00" limit = "" if request.args.get('limit'): limit = "LIMIT " + request.args.get('limit') order = request.args.get('order') if not order: order = "DESC" if forum_short_name: query_first = "SELECT * FROM Post WHERE forum='%s'" % forum_short_name elif thread_id: query_first = "SELECT * FROM Post WHERE thread='%s'" % thread_id query_second = " AND date >= '%s' ORDER BY date %s %s" % (since, order, limit) full_query = query_first + query_second try: cursor.execute(full_query) except Exception: return jsonify(code=3, response="Wrong request") all_posts = cursor.fetchall() if not all_posts: return jsonify(code=0, response={}) end_list = [] for x in all_posts: end_list.append(get_post_info_by_post(cursor, x)) return jsonify(code=0, response=end_list)
def post_vote(): conn = mysql.get_db() cursor = conn.cursor() try: req_json = request.get_json() except BadRequest: return jsonify(code=2, response="Cant parse json") if not ('post' in req_json and 'vote' in req_json): return jsonify(code=3, response="Wrong request") post_id = req_json['post'] vote_value = req_json['vote'] cursor.execute("SELECT likes, dislikes, points FROM Post WHERE id='%s'" % post_id) likes_info = cursor.fetchall() if not likes_info: return jsonify(code=1, response="No such thread") likes = likes_info[0][0] dislikes = likes_info[0][1] points = likes_info[0][2] if vote_value == 1: likes += 1 points += 1 elif vote_value == -1: dislikes += 1 points -= 1 else: return jsonify(code=3, response="Wrong request") sql_update = (likes, dislikes, points, post_id) cursor.execute( "UPDATE Post SET likes=%s, dislikes=%s, points=%s WHERE id=%s", sql_update) conn.commit() cursor.execute("SELECT * FROM Post WHERE id='%s'" % post_id) updated_thread = cursor.fetchall() resp = get_post_info_by_post(cursor, updated_thread[0]) return jsonify(code=0, response=resp)