def thread_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 ('thread' in req_json and 'vote' in req_json): return jsonify(code=3, response="Wrong request") thread_id = req_json['thread'] vote_value = req_json['vote'] cursor.execute("SELECT likes, dislikes, points FROM Thread WHERE id='%s'" % thread_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, thread_id) cursor.execute( "UPDATE Thread SET likes=%s, dislikes=%s, points=%s WHERE id=%s", sql_update) conn.commit() cursor.execute("SELECT * FROM Thread WHERE id='%s'" % thread_id) updated_thread = cursor.fetchall() resp = get_thread_info(cursor, updated_thread[0]) return jsonify(code=0, response=resp)
def thread_list(): conn = mysql.get_db() cursor = conn.cursor() forum_short_name = request.args.get('forum') user_email = request.args.get('user') if not (forum_short_name or user_email): return jsonify(code=3, response="Wrong request") since = request.args.get('since') if not since: since = "1970-01-01" 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: first_part = "SELECT * FROM Thread WHERE forum='%s'" % forum_short_name else: first_part = "SELECT * FROM Thread WHERE user='******'" % user_email second_part = " AND date >= '%s' ORDER BY date %s %s" % (since, order, limit) query = first_part + second_part try: cursor.execute(query) except Exception: return jsonify(code=3, response="Wrong request") all_threads = cursor.fetchall() if not all_threads: return jsonify(code=0, response=[]) end_list = [] for x in all_threads: end_list.append(get_thread_info(cursor, x)) return jsonify(code=0, response=end_list)
def thread_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 ('thread' in req_json and 'message' in req_json and 'slug' in req_json): return jsonify(code=3, response="Wrong request") thread_id = req_json['thread'] new_thread_msg = req_json['message'] new_thread_slug = req_json['slug'] sql_update = (new_thread_msg, new_thread_slug, thread_id) cursor.execute("UPDATE Thread SET message=%s, slug=%s WHERE id=%s", sql_update) conn.commit() cursor.execute("SELECT * FROM Thread WHERE id='%s'" % thread_id) updated_thread = cursor.fetchall() resp = get_thread_info(cursor, updated_thread[0]) return jsonify(code=0, response=resp)
def thread_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 ('thread' in req_json and 'vote' in req_json): return jsonify(code=3, response="Wrong request") thread_id = req_json['thread'] vote_value = req_json['vote'] cursor.execute("SELECT likes, dislikes, points FROM Thread WHERE id='%s'" % thread_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, thread_id) cursor.execute("UPDATE Thread SET likes=%s, dislikes=%s, points=%s WHERE id=%s", sql_update) conn.commit() cursor.execute("SELECT * FROM Thread WHERE id='%s'" % thread_id) updated_thread = cursor.fetchall() resp = get_thread_info(cursor, updated_thread[0]) return jsonify(code=0, response=resp)