def forum_create(): cursor = db.get_cursor() try: data = request.get_json() name = data['name'] short_name = data['short_name'] user = data['user'] cursor.execute( """insert into forums (name, shortname, user) values (%s, %s, %s)""", (name, short_name, user)) db.commit() cursor.close() return jsonify(code=0, response={ 'id': cursor.lastrowid, 'name': name, 'short_name': short_name, 'user': user }) except KeyError: return jsonify(code=2, response='invalid json') except DoesNotExist: return jsonify(code=1, response='user does not exist') except IntegrityError, e: if e[0] == 1062: return jsonify(code=0, response=get_forum_info(short_name, db)) elif e[0] == 1452: return jsonify(code=1, response='user not found')
def forum_create(): cursor = db.get_cursor() try: data = request.get_json() name = data['name'] short_name = data['short_name'] user = data['user'] cursor.execute("""insert into forums (name, shortname, user) values (%s, %s, %s)""",(name, short_name, user)) db.commit() cursor.close() return jsonify(code=0, response={ 'id':cursor.lastrowid, 'name':name, 'short_name': short_name, 'user': user}) except KeyError: return jsonify(code=2, response='invalid json') except DoesNotExist: return jsonify(code=1, response='user does not exist') except IntegrityError, e: if e[0] == 1062: return jsonify(code=0, response=get_forum_info(short_name, db)) elif e[0] == 1452: return jsonify(code=1, response='user not found')
def forum_details(): try: forum = request.args['forum'] related = request.args.getlist('related') return jsonify(code=0, response=get_forum_info(forum, db, related)) except KeyError: return jsonify(code=2, response='invalid json') except DoesNotExist: return jsonify(code=1, response="not found") except: return jsonify(code=4, response='opps')
def forum_listPosts(): cursor = db.get_cursor(MySQLdb.cursors.DictCursor) try: forum = request.args['forum'] limit = request.args.get('limit', '') order = request.args.get('order', 'desc') since_date = request.args.get('since', False) related = request.args.getlist('related') query = "" query_params = () if since_date: query = "select * from posts where forum = %s and date >= %s order by date " + order query_params += ( forum, since_date, ) else: query = "select * from posts where forum = %s order by date " + order query_params += (forum, ) if limit != '': query += " limit " + limit cursor.execute(query, query_params) posts = cursor.fetchall() for post in posts: if 'user' in related: post.update({'user': get_user_info(post['user'], db)}) if 'thread' in related: post.update({'thread': get_thread_info(post['thread'], db)}) if 'forum' in related: post.update({'forum': get_forum_info(post['forum'], db)}) post.update({"date": str(post["date"])}) cursor.close() return jsonify(code=0, response=posts) except DoesNotExist: return jsonify(code=1, response='does not exist') except IntegrityError, e: return jsonify(code=1, response='something not found')
def forum_listPosts(): cursor = db.get_cursor(MySQLdb.cursors.DictCursor) try: forum = request.args['forum'] limit = request.args.get('limit', '') order = request.args.get('order', 'desc') since_date = request.args.get('since', False) related = request.args.getlist('related') query = "" query_params = () if since_date: query = "select * from posts where forum = %s and date >= %s order by date " + order query_params += (forum, since_date, ) else: query = "select * from posts where forum = %s order by date " + order query_params += (forum,) if limit != '': query += " limit " + limit cursor.execute(query, query_params) posts = cursor.fetchall() for post in posts: if 'user' in related: post.update({'user': get_user_info(post['user'], db)}) if 'thread' in related: post.update({'thread': get_thread_info(post['thread'], db)}) if 'forum' in related: post.update({'forum': get_forum_info(post['forum'], db)}) post.update({"date": str(post["date"])}) cursor.close() return jsonify(code=0, response=posts) except DoesNotExist: return jsonify(code=1, response='does not exist') except IntegrityError, e: return jsonify(code=1, response='something not found')