def get_user_list_posts(data): code = 0 posts_list = [] user_email = data.get('user')[0] since = data.get('since', [0, ])[0] limit = data.get('limit', [0, ])[0] sort_by = data.get('sort', 'flat') is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': ['email'], 'columns_values': [user_email], 'table': 'User' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) # check if exists if not res: code = 5 else: sql_scheme = { 'columns_names': ['user'], 'columns_values': [user_email], 'table': 'Post' } larger = -1 if since != 0: larger = {'date': since} sql = build_sql_select_all_query(sql_scheme, is_desc, limit, larger) else: sql = build_sql_select_all_query(sql_scheme, is_desc, limit) posts_list = open_sql_all(sql) resp_keys = ['date', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'message', 'parent', 'thread', 'user', 'likes', 'dislikes', 'points'] resp_dict = [] final_resp = make_response(code=code) if code == 0 and posts_list: for res in posts_list: resp_values = [str(res['date']), res['forum'], res['id'], bool(res['isApproved']), bool(res['isDeleted']), bool(res['isEdited']), bool(res['isHighlighted']), bool(res['isSpam']), res['message'], res['parent'], res['thread'], res['user'], res['likes'], res['dislikes'], res['points']] resp_dict.append(make_response(resp_keys, resp_values, code)['response']) final_resp = {'code': code, 'response': resp_dict} return final_resp
def create_post(data): code = 0 keys = ['id', 'date', 'thread', 'message', 'user', 'forum', 'parent', 'isApproved', 'isHighlighted', 'isEdited', 'isSpam', 'isDeleted'] values = [1, '2000-01-01', 1, 'message', 'email', 'forum', None, 0, 0, 0, 0, 0] if not data: resp_dict = make_response(keys, values, code=0) return flask.jsonify(resp_dict) date = data.get('date', '2000-01-01') thread = data.get('thread', 1) message = data.get('message', 'omg') forum = data.get('forum', 'omg') email = data.get('user', '*****@*****.**') parent = data.get('parent', None) isapproved = data.get('isApproved', False) ishighlighted = data.get('isHighlighted', False) isedited = data.get('isEdited', False) isspam = data.get('isSpam', False) isdeleted = data.get('isDeleted', False) # Matirialized path """ if parent: parent_data = {'post': [parent, ], 'only_mp': [True, ]} mp_parent = get_details_post(parent_data)['response'].get('mp') sql_mp = "select count(*) as count from Post where mp like'" + mp_parent + "._'" sim_count = open_sql(sql_mp).get('count') suffix = sim_count + 1 mp = str(mp_parent) + '.' + str(suffix) else: sql_mp = "select count(*) as count from Post where parent is NULL " sim_count = open_sql(sql_mp).get('count') suffix = sim_count + 1 mp = str(suffix) """ sql_scheme = { 'columns_names': ['date', 'thread', 'message', 'user', 'forum', 'parent', 'isapproved', 'ishighlighted', 'isedited', 'isspam', 'isdeleted'], 'columns_values': [date, thread, message, email, forum, parent, int(isapproved), int(ishighlighted), int(isedited), int(isspam), int(isdeleted)], 'table': 'Post' } sql_post = build_sql_insert_query(sql_scheme) sql_thread = " update Thread set posts=posts+1 where id = %s ;" % thread exec_message2 = exec_sql(sql_thread) exec_message1 = exec_sql(sql_post) if exec_message1 >= 0: values = [exec_message1, date, thread, message, email, forum, parent, isapproved, ishighlighted, isedited, isspam, isdeleted] resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def get_details_thread(data): code = 0 keys = [] values = [] if not data: resp_dict = make_response(keys, values, code=4) return flask.jsonify(resp_dict) thread_id = data.get('thread', None)[0] related = data.get('related', None) if related and 'thread' in related: code = 3 sql_scheme = { 'columns_names': ['id'], 'columns_values': [thread_id], 'table': 'Thread' } is_closing = data.get('is_closing', [1, ])[0] cursor = data.get('cursor', [1, ])[0] sql_check = build_sql_select_all_query(sql_scheme) if is_closing == 1 and cursor == 1: res = open_sql(sql_check) # check if exists else: res = open_sql(sql_check, first=False, is_closing=False, cursor=cursor)['result'] if not res: code = 2 else: if related and cursor == 1: forum_data = {'forum': [res['forum']], } forum_resp = get_details_forum(forum_data) res['forum'] = forum_resp['response'] user_data = {'user': [res['user'], ]} user_resp = get_details_user(user_data) res['user'] = user_resp['response'] keys = ['id', 'date', 'dislikes', 'forum', 'isClosed', 'isDeleted', 'likes', 'message', 'points', 'posts', 'slug', 'title', 'user'] #if res['isDeleted']: # res['posts'] = 0 values = [res['id'], str(res['date']), res['dislikes'], res['forum'], bool(res['isClosed']), bool(res['isDeleted']), res['likes'], res['message'], res['points'], res['posts'], res['slug'], res['title'], res['user']] resp_dict = make_response(keys, values, code) return resp_dict
def create_user(data): code = 0 keys = ['id', 'username', 'about', 'name', 'email', 'isAnonymous'] values = [1, 'username', 'about', 'name', 'email', 'isanon'] if not data: resp_dict = make_response(keys, values, code=0, sql='User: Data not found') return flask.jsonify(resp_dict) username = data['username'] about = data['about'] name = data['name'] email = data['email'] isanon = data.get('isAnonymous', False) sql_scheme = { 'columns_names': ['email'], 'columns_values': [email], 'table': 'User' } sql_check = build_sql_select_all_query(sql_scheme, limit=1, what=' id ') #res = open_sql(sql_check) # check if exists res = False if not res: sql_scheme = { 'columns_names': ['username', 'about', 'name', 'email', 'isAnonymous'], 'columns_values': [username, about, name, email, int(isanon)], 'table': 'User' } sql = build_sql_insert_query(sql_scheme) exec_message = exec_sql(sql) if exec_message >= 0: #res = open_sql(sql_check) values = [exec_message, username, about, name, email, isanon] else: #code = 4 a = 0 else: #code = 5 a = 0 resp_dict = make_response(keys, values, code, sql_check) # add this after load return flask.jsonify(resp_dict) # add this after load
def close_thread(data): code = 0 thread_id = data['thread'] sql_scheme = { 'columns_names': ['id'], 'columns_values': [thread_id], 'table': 'Thread' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql_scheme = { 'columns_names': ['isClosed'], 'columns_values': [1], 'condition': {'id': thread_id}, 'table': 'Thread' } exec_message = exec_sql(build_sql_update_query(sql_scheme)) if exec_message != 0: code = 4 else: code = 1 keys = ['thread'] values = [thread_id] resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def remove_thread(data): code = 0 thread = data['thread'] sql_scheme = { 'columns_names': ['id'], 'columns_values': [thread], 'table': 'Thread' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql_scheme_posts_count = { 'columns_names': ['thread'], 'columns_values': [thread], 'table': 'Post' } sql_posts_count = build_sql_select_all_query(sql_scheme_posts_count) res_posts = open_sql(sql_posts_count) posts_count = 0 if res_posts: posts_count = len([res_posts]) """ TODO sql_scheme_thread_up = { 'columns_names': ['isDeleted', 'posts'], 'columns_values': [1, 'posts-'+str(posts_count)], 'condition': {'id': thread}, 'table': 'Thread' } """ sql_thread_up = 'update Thread set posts=0 , isDeleted=1 where id = %s' % thread exec_sql(sql_thread_up) sql_scheme_post_rm = { 'columns_names': ['isDeleted'], 'columns_values': [1], 'condition': { 'thread': thread }, 'table': 'Post' } sql_post_rm = build_sql_update_query(sql_scheme_post_rm) exec_sql(sql_post_rm) sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if not res: code = 1 keys = ['thread'] values = [thread] resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def restore_post(data): code = 0 post = data['post'] sql_scheme = { 'columns_names': ['id'], 'columns_values': [post], 'table': 'Post' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql_scheme = { 'columns_names': ['isDeleted'], 'columns_values': [0], 'condition': {'id': post}, 'table': 'Post' } sql1 = build_sql_update_query(sql_scheme) sql2 = " update Thread set posts=posts+1 where id= %s ;" % res['thread'] exec_sql([sql1, sql2], multi=True) if not res: code = 1 keys = ['post'] values = [post] resp_dict = make_response(keys=keys, values=values, code=code) return flask.jsonify(resp_dict)
def unsubscribe_thread(data): code = 0 keys = [] values = [] thread_id = data['thread'] email = data['user'] sql_scheme = { 'columns_names': ['thread', 'user'], 'columns_values': [thread_id, email], 'table': 'Subscribe' } sql_check = build_sql_select_all_query(sql_scheme) res_dict = open_sql_all(sql_check, first=True, is_closing=False) # check if exists res = res_dict['result'][0] if res == -1 or not res: return make_response([], [], code=4) db = res_dict['db'] crs = res_dict['cursor'] if res: sql_scheme = { 'columns_names': ['isDeleted'], 'columns_values': [1], 'condition': { 'id': res['id'] }, 'table': 'Subscribe' } exec_sql(build_sql_update_query(sql_scheme), first=False, cursor=crs, is_closing=False) keys = ['thread', 'user'] values = [res['thread'], res['user']] else: code = 1 db.close() resp_dict = make_response(keys=keys, values=values, code=code) return flask.jsonify(resp_dict)
def get_details_post(data): code = 0 resp_keys = [] resp_values = [] post_id = data.get('post', None)[0] related = data.get('related', None) attributes = ' * ' only_mp = data.get('only_mp', None) if only_mp: attributes = ' mp ' sql_scheme = { 'columns_names': ['id'], 'columns_values': [post_id], 'table': 'Post' } sql_check = build_sql_select_all_query(sql_scheme, what=attributes) is_closing = data.get('is_closing', [1, ])[0] cursor = data.get('cursor', [1, ])[0] if is_closing == 1 and cursor == 1: res = open_sql(sql_check) # check if exists else: res = open_sql(sql_check, first=False, is_closing=False, cursor=cursor)['result'] if not res: code = 1 else: if related and is_closing == 1: forum_data = {'forum': [res['forum']], } forum_resp = get_details_forum(forum_data) res['forum'] = forum_resp['response'] user_data = {'user': [res['user'], ]} user_resp = get_details_user(user_data) res['user'] = user_resp['response'] thread_data = {'thread': [res['thread'], ]} thread_resp = get_details_thread(thread_data) res['thread'] = thread_resp['response'] if not only_mp: resp_keys = ['date', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'message', 'parent', 'thread', 'user', 'likes', 'dislikes', 'points'] resp_values = [str(res['date']), res['forum'], int(post_id), bool(res['isApproved']), bool(res['isDeleted']), bool(res['isEdited']), bool(res['isHighlighted']), bool(res['isSpam']), res['message'], res['parent'], res['thread'], res['user'], res['likes'], res['dislikes'], res['points']] else: resp_keys = ['mp'] resp_values = [res['mp']] resp_dict = make_response(resp_keys, resp_values, code) return resp_dict
def remove_thread(data): code = 0 thread = data['thread'] sql_scheme = { 'columns_names': ['id'], 'columns_values': [thread], 'table': 'Thread' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql_scheme_posts_count = { 'columns_names': ['thread'], 'columns_values': [thread], 'table': 'Post' } sql_posts_count = build_sql_select_all_query(sql_scheme_posts_count) res_posts = open_sql(sql_posts_count) posts_count = 0 if res_posts: posts_count = len([res_posts]) """ TODO sql_scheme_thread_up = { 'columns_names': ['isDeleted', 'posts'], 'columns_values': [1, 'posts-'+str(posts_count)], 'condition': {'id': thread}, 'table': 'Thread' } """ sql_thread_up = 'update Thread set posts=0 , isDeleted=1 where id = %s' % thread exec_sql(sql_thread_up) sql_scheme_post_rm = { 'columns_names': ['isDeleted'], 'columns_values': [1], 'condition': {'thread': thread}, 'table': 'Post' } sql_post_rm = build_sql_update_query(sql_scheme_post_rm) exec_sql(sql_post_rm) sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if not res: code = 1 keys = ['thread'] values = [thread] resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def create_user(data): code = 0 keys = ['id', 'username', 'about', 'name', 'email', 'isAnonymous'] values = [1, 'username', 'about', 'name', 'email', 'isanon'] if not data: resp_dict = make_response(keys, values, code=0, sql='User: Data not found') return flask.jsonify(resp_dict) username = data['username'] about = data['about'] name = data['name'] email = data['email'] isanon = data.get('isAnonymous', False) sql_scheme = { 'columns_names': ['email'], 'columns_values': [email], 'table': 'User' } sql_check = build_sql_select_all_query(sql_scheme, limit=1, what=' id ') #res = open_sql(sql_check) # check if exists res = False if not res: sql_scheme = { 'columns_names': ['username', 'about', 'name', 'email', 'isAnonymous'], 'columns_values': [username, about, name, email, int(isanon)], 'table': 'User' } sql = build_sql_insert_query(sql_scheme) exec_message = exec_sql(sql) if exec_message >= 0: #res = open_sql(sql_check) values = [exec_message, username, about, name, email, isanon] else: #code = 4 a = 0 else: #code = 5 a = 0 resp_dict = make_response(keys, values, code, sql_check) # add this after load return flask.jsonify(resp_dict) # add this after load
def unsubscribe_thread(data): code = 0 keys = [] values = [] thread_id = data['thread'] email = data['user'] sql_scheme = { 'columns_names': ['thread', 'user'], 'columns_values': [thread_id, email], 'table': 'Subscribe' } sql_check = build_sql_select_all_query(sql_scheme) res_dict = open_sql_all(sql_check, first=True, is_closing=False) # check if exists res = res_dict['result'][0] if res == -1 or not res: return make_response([], [], code=4) db = res_dict['db'] crs = res_dict['cursor'] if res: sql_scheme = { 'columns_names': ['isDeleted'], 'columns_values': [1], 'condition': {'id': res['id']}, 'table': 'Subscribe' } exec_sql(build_sql_update_query(sql_scheme), first=False, cursor=crs, is_closing=False) keys = ['thread', 'user'] values = [res['thread'], res['user']] else: code = 1 db.close() resp_dict = make_response(keys=keys, values=values, code=code) return flask.jsonify(resp_dict)
def subscribe_thread(data): code = 0 keys = ['id', 'thread', 'user'] values = [1, 1, '*****@*****.**'] if not data: resp_dict = make_response(keys, values, code=0) return flask.jsonify(resp_dict) thread_id = data.get('thread', 1) email = data.get('user', '*****@*****.**') sql_scheme = { 'columns_names': ['thread', 'user'], 'columns_values': [thread_id, email], 'table': 'Subscribe' } sql_check = build_sql_select_all_query(sql_scheme, limit=1, what=' id ') #res = open_sql(sql_check) # check if exists res = False if not res: sql_scheme = { 'columns_names': ['thread', 'user'], 'columns_values': [thread_id, email], 'table': 'Subscribe' } sql = build_sql_insert_query(sql_scheme) exec_message = exec_sql(sql) if exec_message >= 0: values = [exec_message, thread_id, email] else: code = 0 # 4 resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def create_forum(data): code = 0 keys = ['id', 'name', 'short_name', 'user'] values = [1, 'name', 'shn', 'email'] if not data: resp_dict = make_response(keys, values, code=0) return flask.jsonify(resp_dict) name = data['name'].encode("utf-8") shn = data['short_name'].encode("utf-8") email = data['user'].encode("utf-8") sql_scheme = { 'columns_names': ['name'], 'columns_values': [name], 'table': 'Forum' } sql_check = build_sql_select_all_query(sql_scheme, limit=1, what=' id ') #res = open_sql(sql_check) # check if exists res = False if not res: sql_scheme = { 'columns_names': ['name', 'short_name', 'user'], 'columns_values': [name, shn, email], 'table': 'Forum' } sql = build_sql_insert_query(sql_scheme) exec_message = exec_sql(sql) if exec_message >= 0: values = [exec_message, name, shn, email] else: code = 0#4 #code = 4 resp_dict = make_response(keys, values, code, sql_check) return flask.jsonify(resp_dict)
def create_forum(data): code = 0 keys = ['id', 'name', 'short_name', 'user'] values = [1, 'name', 'shn', 'email'] if not data: resp_dict = make_response(keys, values, code=0) return flask.jsonify(resp_dict) name = data['name'].encode("utf-8") shn = data['short_name'].encode("utf-8") email = data['user'].encode("utf-8") sql_scheme = { 'columns_names': ['name'], 'columns_values': [name], 'table': 'Forum' } sql_check = build_sql_select_all_query(sql_scheme, limit=1, what=' id ') #res = open_sql(sql_check) # check if exists res = False if not res: sql_scheme = { 'columns_names': ['name', 'short_name', 'user'], 'columns_values': [name, shn, email], 'table': 'Forum' } sql = build_sql_insert_query(sql_scheme) exec_message = exec_sql(sql) if exec_message >= 0: values = [exec_message, name, shn, email] else: code = 0 #4 #code = 4 resp_dict = make_response(keys, values, code, sql_check) return flask.jsonify(resp_dict)
def vote_post(data): code = 0 post_id = data['post'] vote = data['vote'] resp_keys = [] resp_values = [] vote_column = determinate_vote_column(vote) sql_scheme = { 'columns_names': ['id'], 'columns_values': [post_id], 'table': 'Post' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql1 = "update Post set %s = %s+1 where id=%s" % (vote_column, vote_column, post_id) sql2 = "update Post set points=likes-dislikes where id=%s" % post_id exec_sql([sql1, sql2], multi=True) res = open_sql(sql_check) # refresh resp_keys = [ 'date', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'message', 'parent', 'thread', 'user' ] resp_values = [ str(res['date']), res['forum'], res['id'], bool(res['isApproved']), bool(res['isDeleted']), bool(res['isEdited']), bool(res['isHighlighted']), bool(res['isSpam']), res['message'], res['parent'], res['thread'], res['user'] ] else: code = 1 resp_dict = make_response(resp_keys, resp_values, code) return flask.jsonify(resp_dict)
def update_post(data): code = 0 keys = [] values = [] post_id = data['post'] message = data['message'] sql_scheme = { 'columns_names': ['id'], 'columns_values': [post_id], 'table': 'Post' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql_scheme = { 'columns_names': ['message'], 'columns_values': [message], 'condition': { 'id': post_id }, 'table': 'Post' } exec_sql(build_sql_update_query(sql_scheme)) keys = [ 'date', 'dislikes', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'likes', 'message', 'parent', 'points', 'thread', 'user' ] values = [ res['date'], res['dislikes'], res['forum'], res['id'], res['isApproved'], res['isDeleted'], res['isEdited'], res['isHighlighted'], res['isSpam'], res['likes'], message, res['parent'], res['points'], res['thread'], res['user'] ] values = [str(x) for x in values] else: code = 1 resp_dict = make_response(keys=keys, values=values, code=code) return flask.jsonify(resp_dict)
def vote_thread(data): code = 0 keys = [] values = [] thread_id = data['thread'] vote = data['vote'] vote_column = determinate_vote_column(vote) sql_scheme = { 'columns_names': ['id'], 'columns_values': [thread_id], 'table': 'Thread' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql1 = "update Thread set %s = %s+1 where id=%s" % ( vote_column, vote_column, thread_id) sql2 = "update Thread set points=likes-dislikes where id=%s" % thread_id exec_sql([sql1, sql2], multi=True) res = open_sql(sql_check) # refresh keys = [ 'id', 'date', 'forum', 'isClosed', 'isDeleted', 'message', 'slug', 'title', 'user', 'likes', 'dislikes', 'points' ] values = [ res['id'], str(res['date']), res['forum'], bool(res['isClosed']), bool(res['isDeleted']), res['message'], res['slug'], res['title'], res['user'], res['likes'], res['dislikes'], res['points'] ] else: code = 1 resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def vote_post(data): code = 0 post_id = data['post'] vote = data['vote'] resp_keys = [] resp_values = [] vote_column = determinate_vote_column(vote) sql_scheme = { 'columns_names': ['id'], 'columns_values': [post_id], 'table': 'Post' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql1 = "update Post set %s = %s+1 where id=%s" % (vote_column, vote_column, post_id) sql2 = "update Post set points=likes-dislikes where id=%s" % post_id exec_sql([sql1, sql2], multi=True) res = open_sql(sql_check) # refresh resp_keys = ['date', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'message', 'parent', 'thread', 'user'] resp_values = [str(res['date']), res['forum'], res['id'], bool(res['isApproved']), bool(res['isDeleted']), bool(res['isEdited']), bool(res['isHighlighted']), bool(res['isSpam']), res['message'], res['parent'], res['thread'], res['user']] else: code = 1 resp_dict = make_response(resp_keys, resp_values, code) return flask.jsonify(resp_dict)
def remove_post(data): code = 0 post = data['post'] sql_scheme = { 'columns_names': ['id'], 'columns_values': [post], 'table': 'Post' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql_scheme = { 'columns_names': ['isDeleted'], 'columns_values': [1], 'condition': { 'id': post }, 'table': 'Post' } sql_update = build_sql_update_query(sql_scheme) sql = " update Thread set posts=posts-1 where id= %s " % res['thread'] #return sql exec_message = exec_sql([sql_update, sql], multi=True) if exec_message != 0: code = 4 if not res: code = 1 keys = ['post'] values = [post] resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def update_thread(data): code = 0 keys = [] values = [] thread_id = data['thread'] message = data['message'] slug = data['slug'] sql_scheme = { 'columns_names': ['id'], 'columns_values': [thread_id], 'table': 'Thread' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql_scheme = { 'columns_names': ['message', 'slug'], 'columns_values': [message, slug], 'condition': {'id': thread_id}, 'table': 'Thread' } sql_update = build_sql_update_query(sql_scheme) exec_sql(sql_update) keys = ['id', 'date', 'dislikes', 'forum', 'isClosed', 'isDeleted', 'likes', 'message', 'points', 'posts', 'slug', 'title', 'user'] values = [res['id'], str(res['date']), res['dislikes'], res['forum'], bool(res['isClosed']), bool(res['isDeleted']), res['likes'], res['message'], res['points'], res['posts'], res['slug'], res['title'], res['user']] else: code = 1 resp_dict = make_response(keys=keys, values=values, code=code) return flask.jsonify(resp_dict)
def get_details_forum(data): code = 0 keys = [] values = [] forum_sh_name = data.get('forum', None)[0] #related = data['related'][0] related = data.get('related', None) sql_scheme = { 'columns_names': ['short_name'], 'columns_values': [forum_sh_name], 'table': 'Forum' } is_closing = data.get('is_closing', [1, ])[0] cursor = data.get('cursor', [1, ])[0] sql_check = build_sql_select_all_query(sql_scheme) if is_closing == 1 and cursor == 1: res = open_sql(sql_check) # check if exists else: res = open_sql(sql_check, first=False, is_closing=False, cursor=cursor)['result'] if not res: code = 2 else: if related and cursor == 1: user_data = {'user': [res['user'], ]} user_resp = get_details_user(user_data) res['user'] = user_resp['response'] keys = ['id', 'name', 'short_name', 'user'] values = [res['id'], res['name'], res['short_name'], res['user']] resp_dict = make_response(keys, values, code) return resp_dict
def update_post(data): code = 0 keys = [] values = [] post_id = data['post'] message = data['message'] sql_scheme = { 'columns_names': ['id'], 'columns_values': [post_id], 'table': 'Post' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql_scheme = { 'columns_names': ['message'], 'columns_values': [message], 'condition': {'id': post_id}, 'table': 'Post' } exec_sql(build_sql_update_query(sql_scheme)) keys = ['date', 'dislikes', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'likes', 'message', 'parent', 'points', 'thread', 'user'] values = [res['date'], res['dislikes'], res['forum'], res['id'], res['isApproved'], res['isDeleted'], res['isEdited'], res['isHighlighted'], res['isSpam'], res['likes'], message, res['parent'], res['points'], res['thread'], res['user']] values = [str(x) for x in values] else: code = 1 resp_dict = make_response(keys=keys, values=values, code=code) return flask.jsonify(resp_dict)
def remove_post(data): code = 0 post = data['post'] sql_scheme = { 'columns_names': ['id'], 'columns_values': [post], 'table': 'Post' } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) if res: sql_scheme = { 'columns_names': ['isDeleted'], 'columns_values': [1], 'condition': {'id': post}, 'table': 'Post' } sql_update = build_sql_update_query(sql_scheme) sql = " update Thread set posts=posts-1 where id= %s " % res['thread'] #return sql exec_message = exec_sql([sql_update, sql], multi=True) if exec_message != 0: code = 4 if not res: code = 1 keys = ['post'] values = [post] resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def get_thread_list(data): code = 0 posts_list = [] user_email = data.get('user', [ 0, ])[0] forum_sh_name = data.get('forum', [ 0, ])[0] req_field = { 'name': 'short_name', 'val': forum_sh_name, 'table': 'Forum', 'related': 'forum' } if user_email: req_field = { 'name': 'email', 'val': user_email, 'table': 'User', 'related': 'user' } since = data.get('since', [ 0, ])[0] limit = data.get('limit', [ 0, ])[0] sort_by = data.get('sort', 'flat') is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': [req_field['name']], 'columns_values': [req_field['val']], 'table': req_field['table'] } sql_check = build_sql_select_all_query(sql_scheme) res = open_sql(sql_check) # check if exists if not res: code = 2 else: sql_scheme = { 'columns_names': [req_field['related']], 'columns_values': [req_field['val']], 'table': 'Thread' } if since != 0: larger = {'date': since} sql = build_sql_select_all_query(sql_scheme, is_desc, limit, larger) else: sql = build_sql_select_all_query(sql_scheme, is_desc, limit) posts_list = open_sql_all(sql) keys = [ 'id', 'date', 'forum', 'isClosed', 'isDeleted', 'message', 'slug', 'title', 'user', 'posts' ] resp_dict = [] final_resp = make_response(code=code) if code == 0 and posts_list: for res in posts_list: values = [ res['id'], str(res['date']), res['forum'], bool(res['isClosed']), bool(res['isDeleted']), res['message'], res['slug'], res['title'], res['user'], res['posts'] ] resp_dict.append(make_response(keys, values, code)['response']) final_resp = {'code': code, 'response': resp_dict} return final_resp
def get_forum_threads_list(data): code = 0 thread_list = [] forum_sh_name = data.get('forum')[0] since = data.get('since', [0, ])[0] limit = data.get('limit', [0, ])[0] sort_by = data.get('sort', 'flat') related = data.get('related', [0, ]) is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': ['short_name'], 'columns_values': [forum_sh_name], 'table': 'Forum' } #sql_check = build_sql_select_all_query(sql_scheme) #res = open_sql(sql_check) # check if exists res = True if not res: code = 5 else: sql_scheme = { 'columns_names': ['forum'], 'columns_values': [forum_sh_name], 'table': 'Thread' } if since != 0: larger = {'date': since} sql = build_sql_select_all_query(sql_scheme, is_desc=is_desc, limit=limit, larger=larger, ord_by=' date ') else: sql = build_sql_select_all_query(sql_scheme, is_desc=is_desc, limit=limit, ord_by=' date ') thread_list_dict = open_sql_all(sql, first=True, is_closing=False) thread_list = thread_list_dict['result'] if thread_list == -1 or not thread_list: return make_response([], [], code=4) db = thread_list_dict['db'] crs = thread_list_dict['cursor'] resp_keys = ['id', 'date', 'forum', 'isClosed', 'isDeleted', 'message', 'slug', 'title', 'user'] resp_dict = [] final_resp = make_response(code=code) if code == 0 and thread_list: for res in thread_list: thread_data = get_details_thread({'thread': [res['id'], ], 'is_closing': [False, ], 'cursor': [crs, ]})['response'] resp_dict.append(thread_data) if 'forum' in related: for x in resp_dict: x['forum'] = get_details_forum({'forum': [x['forum'], ], 'is_closing': [False, ], 'cursor': [crs, ]})['response'] if 'user' in related: for x in resp_dict: x['user'] = get_details_user({'user': [x['user'], ], 'is_closing': [False, ], 'cursor': [crs, ]})['response'] final_resp = {'code': code, 'response': resp_dict} db.close() return final_resp
def get_forum_users_list(data): code = 0 posts_list = [] users_res = [] forum_sh_name = data.get('forum')[0] since = data.get('since_id', [0, ])[0] limit = data.get('limit', [1, ])[0] is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': ['short_name'], 'columns_values': [forum_sh_name], 'table': 'Forum' } #sql_check = build_sql_select_all_query(sql_scheme) # res = open_sql(sql_check) # check if exists res = True if not res: code = 2 else: sql_scheme = { 'columns_names': ['forum'], 'columns_values': [forum_sh_name], 'table': 'Post' } if since != 0: larger = {'id': since} sql = build_sql_select_all_query(sql_scheme, group='user', what='user', larger=larger, limit=limit) else: sql = build_sql_select_all_query(sql_scheme, group='user', what='user', limit=limit) posts_list_dict = open_sql_all(sql) posts_list = posts_list_dict if posts_list == -1 or not posts_list: return make_response([], [], code=4) #!db = posts_list_dict['db'] #!crs = posts_list_dict['cursor'] mails = [] for x in posts_list: mails.append(x['user']) if mails: sql_scheme = { 'columns_names': ['email'], 'columns_values': mails, 'table': 'User' } if since != 0: sql = build_sql_select_all_query(sql_scheme, larger={'id': since}, limit=limit, ord_by=' name ', is_desc=is_desc, in_set=True) else: sql = build_sql_select_all_query(sql_scheme, limit=limit, ord_by=' name ', is_desc=is_desc, in_set=True) users_res_dict = open_sql_all(sql)#!, first=False, is_closing=False, cursor=crs) users_res = users_res_dict#!['result'] if users_res == -1 and not users_res: return make_response([], [], code=4) resp_list = [] final_resp = make_response(code=code) if code == 0 and users_res: for res in users_res: #usr_details = get_details_user({'user': [res['email'], ]}) #usr_details = usr_details['response'] res['subscriptions'] = [] #usr_details['subscriptions'] res['followers'] = [] #usr_details['followers'] res['following'] = [] #usr_details['following'] resp_list.append(res) #!db.close() final_resp = {'code': code, 'response': resp_list} return final_resp
def get_forum_threads_list(data): code = 0 thread_list = [] forum_sh_name = data.get('forum')[0] since = data.get('since', [ 0, ])[0] limit = data.get('limit', [ 0, ])[0] sort_by = data.get('sort', 'flat') related = data.get('related', [ 0, ]) is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': ['short_name'], 'columns_values': [forum_sh_name], 'table': 'Forum' } #sql_check = build_sql_select_all_query(sql_scheme) #res = open_sql(sql_check) # check if exists res = True if not res: code = 5 else: sql_scheme = { 'columns_names': ['forum'], 'columns_values': [forum_sh_name], 'table': 'Thread' } if since != 0: larger = {'date': since} sql = build_sql_select_all_query(sql_scheme, is_desc=is_desc, limit=limit, larger=larger, ord_by=' date ') else: sql = build_sql_select_all_query(sql_scheme, is_desc=is_desc, limit=limit, ord_by=' date ') thread_list_dict = open_sql_all(sql, first=True, is_closing=False) thread_list = thread_list_dict['result'] if thread_list == -1 or not thread_list: return make_response([], [], code=4) db = thread_list_dict['db'] crs = thread_list_dict['cursor'] resp_keys = [ 'id', 'date', 'forum', 'isClosed', 'isDeleted', 'message', 'slug', 'title', 'user' ] resp_dict = [] final_resp = make_response(code=code) if code == 0 and thread_list: for res in thread_list: thread_data = get_details_thread({ 'thread': [ res['id'], ], 'is_closing': [ False, ], 'cursor': [ crs, ] })['response'] resp_dict.append(thread_data) if 'forum' in related: for x in resp_dict: x['forum'] = get_details_forum({ 'forum': [ x['forum'], ], 'is_closing': [ False, ], 'cursor': [ crs, ] })['response'] if 'user' in related: for x in resp_dict: x['user'] = get_details_user({ 'user': [ x['user'], ], 'is_closing': [ False, ], 'cursor': [ crs, ] })['response'] final_resp = {'code': code, 'response': resp_dict} db.close() return final_resp
def get_post_list(data): code = 0 posts_list = [] thread_id = data.get('thread', [0, ])[0] forum_sh_name = data.get('forum', [0, ])[0] req_field = {'name': 'short_name', 'val': forum_sh_name, 'table': 'Forum', 'related': 'forum'} if thread_id: req_field = {'name': 'id', 'val': thread_id, 'table': 'Thread', 'related': 'thread'} since = data.get('since', [0, ])[0] limit = data.get('limit', [0, ])[0] sort_by = data.get('sort', 'flat') is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': [req_field['name']], 'columns_values': [req_field['val']], 'table': req_field['table'] } sql_check = build_sql_select_all_query(sql_scheme) res_dict = open_sql_all(sql_check, first=True, is_closing=False) # check if exists res = res_dict['result'][0] if res == -1 or not res: return make_response([], [], code=4) db = res_dict['db'] crs = res_dict['cursor'] if not res: code = 2 else: sql_scheme = { 'columns_names': [req_field['related']], 'columns_values': [req_field['val']], 'table': 'Post' } if since != 0: larger = {'date': since} sql = build_sql_select_all_query(sql_scheme, is_desc, limit, larger) else: sql = build_sql_select_all_query(sql_scheme, is_desc, limit) posts_list = open_sql_all(sql, first=False, cursor=crs, is_closing=False)['result'] if posts_list == -1 or not posts_list: return make_response([], [], code=4) resp_keys = ['date', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'message', 'parent', 'thread', 'user', 'likes', 'dislikes', 'points'] resp_dict = [] final_resp = make_response(code=code) if code == 0 and posts_list: for res in posts_list: resp_values = [str(res['date']), res['forum'], res['id'], bool(res['isApproved']), bool(res['isDeleted']), bool(res['isEdited']), bool(res['isHighlighted']), bool(res['isSpam']), res['message'], res['parent'], res['thread'], res['user'], res['likes'], res['dislikes'], res['points']] resp_dict.append(make_response(resp_keys, resp_values, code)['response']) final_resp = {'code': code, 'response': resp_dict} db.close() return final_resp
def follow_user(data): code = 0 keys = ['id', 'username', 'about', 'name', 'email', 'isAnonymous', 'followers', 'following', 'subscriptions'] values = [1, 'noname', 'qwe', 'lal', '*****@*****.**', 0, 'a', 'b', 'c'] if not data: resp_dict = make_response(keys, values, code=0) return flask.jsonify(resp_dict) email_1 = data['follower'] email_2 = data['followee'] sql_scheme_usr1 = { 'columns_names': ['email'], 'columns_values': [email_1], 'table': 'User' } sql_scheme_usr2 = { 'columns_names': ['email'], 'columns_values': [email_2], 'table': 'User' } sql_scheme_follow = { 'columns_names': ['follower', 'followee'], 'columns_values': [email_1, email_2], 'table': 'Followers' } #sql_check_usr1 = build_sql_select_all_query(sql_scheme_usr1, what=' id ', limit=1) #sql_check_usr2 = build_sql_select_all_query(sql_scheme_usr2, what=' id ', limit=1) sql_check_follow = build_sql_select_all_query(sql_scheme_follow) #res_usr1 = open_sql(sql_check_usr1) # check if exists #res_usr2 = open_sql(sql_check_usr2) res_usr1 = True res_usr2 = True res_follow = open_sql(sql_check_follow) if res_usr1 and res_usr2 and not res_follow: sql = build_sql_insert_query(sql_scheme_follow) exec_message = exec_sql(sql) if exec_message < 0: #code = 4 a = 0 usr_details = get_details_user({'user': [email_1, ]}) usr_details = usr_details['response'] if 'id' in usr_details: values = [int(usr_details['id']), usr_details['username'], usr_details['about'], usr_details['name'], usr_details['email'], bool(usr_details['isAnonymous']), usr_details['followers'], usr_details['following'], usr_details['subscriptions']] resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def get_post_list(data): code = 0 posts_list = [] thread_id = data.get('thread', [ 0, ])[0] forum_sh_name = data.get('forum', [ 0, ])[0] req_field = { 'name': 'short_name', 'val': forum_sh_name, 'table': 'Forum', 'related': 'forum' } if thread_id: req_field = { 'name': 'id', 'val': thread_id, 'table': 'Thread', 'related': 'thread' } since = data.get('since', [ 0, ])[0] limit = data.get('limit', [ 0, ])[0] sort_by = data.get('sort', 'flat') is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': [req_field['name']], 'columns_values': [req_field['val']], 'table': req_field['table'] } sql_check = build_sql_select_all_query(sql_scheme) res_dict = open_sql_all(sql_check, first=True, is_closing=False) # check if exists res = res_dict['result'][0] if res == -1 or not res: return make_response([], [], code=4) db = res_dict['db'] crs = res_dict['cursor'] if not res: code = 2 else: sql_scheme = { 'columns_names': [req_field['related']], 'columns_values': [req_field['val']], 'table': 'Post' } if since != 0: larger = {'date': since} sql = build_sql_select_all_query(sql_scheme, is_desc, limit, larger) else: sql = build_sql_select_all_query(sql_scheme, is_desc, limit) posts_list = open_sql_all(sql, first=False, cursor=crs, is_closing=False)['result'] if posts_list == -1 or not posts_list: return make_response([], [], code=4) resp_keys = [ 'date', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'message', 'parent', 'thread', 'user', 'likes', 'dislikes', 'points' ] resp_dict = [] final_resp = make_response(code=code) if code == 0 and posts_list: for res in posts_list: resp_values = [ str(res['date']), res['forum'], res['id'], bool(res['isApproved']), bool(res['isDeleted']), bool(res['isEdited']), bool(res['isHighlighted']), bool(res['isSpam']), res['message'], res['parent'], res['thread'], res['user'], res['likes'], res['dislikes'], res['points'] ] resp_dict.append( make_response(resp_keys, resp_values, code)['response']) final_resp = {'code': code, 'response': resp_dict} db.close() return final_resp
def get_details_post(data): code = 0 resp_keys = [] resp_values = [] post_id = data.get('post', None)[0] related = data.get('related', None) attributes = ' * ' only_mp = data.get('only_mp', None) if only_mp: attributes = ' mp ' sql_scheme = { 'columns_names': ['id'], 'columns_values': [post_id], 'table': 'Post' } sql_check = build_sql_select_all_query(sql_scheme, what=attributes) is_closing = data.get('is_closing', [ 1, ])[0] cursor = data.get('cursor', [ 1, ])[0] if is_closing == 1 and cursor == 1: res = open_sql(sql_check) # check if exists else: res = open_sql(sql_check, first=False, is_closing=False, cursor=cursor)['result'] if not res: code = 1 else: if related and is_closing == 1: forum_data = { 'forum': [res['forum']], } forum_resp = get_details_forum(forum_data) res['forum'] = forum_resp['response'] user_data = { 'user': [ res['user'], ] } user_resp = get_details_user(user_data) res['user'] = user_resp['response'] thread_data = { 'thread': [ res['thread'], ] } thread_resp = get_details_thread(thread_data) res['thread'] = thread_resp['response'] if not only_mp: resp_keys = [ 'date', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'message', 'parent', 'thread', 'user', 'likes', 'dislikes', 'points' ] resp_values = [ str(res['date']), res['forum'], int(post_id), bool(res['isApproved']), bool(res['isDeleted']), bool(res['isEdited']), bool(res['isHighlighted']), bool(res['isSpam']), res['message'], res['parent'], res['thread'], res['user'], res['likes'], res['dislikes'], res['points'] ] else: resp_keys = ['mp'] resp_values = [res['mp']] resp_dict = make_response(resp_keys, resp_values, code) return resp_dict
def restore_thread(data): code = 0 if not data: resp_dict = make_response(keys=[], values=[], code=4) return flask.jsonify(resp_dict) thread = data['thread'] sql_scheme = { 'columns_names': ['id', 'isDeleted'], 'columns_values': [thread, 1], 'table': 'Thread' } #sql_check = build_sql_select_all_query(sql_scheme) #res = open_sql(sql_check) res = True if res: sql_scheme_post_cont = { 'columns_names': ['thread'], 'columns_values': [thread], 'table': 'Post' } sql_post_count = build_sql_select_all_query(sql_scheme_post_cont, what='COUNT(*)') res_count_dict = open_sql_all(sql_post_count, first=True, is_closing=False) res_count = res_count_dict['result'][0] db = res_count_dict['db'] crs = res_count_dict['cursor'] posts_count = 0 if res_count: posts_count = res_count['COUNT(*)'] """ TODO sql_scheme_thread_up = { 'columns_names': ['isDeleted', 'posts'], 'columns_values': [0, 'posts+'+str(posts_count)], 'condition': {'id': thread}, 'table': 'Thread' }""" sql1 = 'update Thread set posts=%s , isDeleted=0 where id = %s' % (posts_count, thread) exec_sql(sql1, first=False, cursor=crs, is_closing=False) sql_scheme_post_up = { 'columns_names': ['isDeleted'], 'columns_values': [0], 'condition': {'thread': thread}, 'table': 'Post' } sql2 = build_sql_update_query(sql_scheme_post_up) exec_sql(sql2, first=False, cursor=crs, is_closing=False) #if (exec_message1 == exec_message2) != 0: # code = 4 if not res: code = 1 db.close() keys = ['thread'] values = [thread] resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def get_forum_posts_list(data): code = 0 posts_list = [] forum_sh_name = data.get('forum')[0] since = data.get('since', [0, ])[0] limit = data.get('limit', [0, ])[0] sort_by = data.get('sort', 'flat') related = data.get('related', [0, ]) is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': ['short_name'], 'columns_values': [forum_sh_name], 'table': 'Forum' } # sql_check = build_sql_select_all_query(sql_scheme) #res = open_sql(sql_check) # check if exists res = True if not res: code = 5 else: sql_scheme = { 'columns_names': ['forum'], 'columns_values': [forum_sh_name], 'table': 'Post' } if since != 0: larger = {'date': since} sql = build_sql_select_all_query(sql_scheme, is_desc, limit, larger) else: sql = build_sql_select_all_query(sql_scheme, is_desc, limit) posts_list_dict = open_sql_all(sql, first=True, is_closing=False) posts_list = posts_list_dict['result'] db = posts_list_dict['db'] crs = posts_list_dict['cursor'] resp_keys = ['date', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'message', 'parent', 'thread', 'user', 'likes', 'dislikes', 'points'] resp_dict = [] final_resp = make_response(code=code) if code == 0 and posts_list: for res in posts_list: resp_values = [str(res['date']), res['forum'], res['id'], bool(res['isApproved']), bool(res['isDeleted']), bool(res['isEdited']), bool(res['isHighlighted']), bool(res['isSpam']), res['message'], res['parent'], res['thread'], res['user'], res['likes'], res['dislikes'], res['points']] resp_dict.append(make_response(resp_keys, resp_values, code)['response']) if 'thread' in related: for x in resp_dict: x['thread'] = get_details_thread({'thread': [x['thread'], ], 'is_closing': [False, ], 'cursor': [crs, ]})['response'] if 'forum' in related: for x in resp_dict: x['forum'] = get_details_forum({'forum': [x['forum'], ], 'is_closing': [False, ], 'cursor': [crs, ]})['response'] if 'user' in related: for x in resp_dict: x['user'] = get_details_user({'user': [x['user'], ], 'is_closing': [False, ], 'cursor': [crs, ]})['response'] final_resp = {'code': code, 'response': resp_dict} db.close() return final_resp
def get_forum_posts_list(data): code = 0 posts_list = [] forum_sh_name = data.get('forum')[0] since = data.get('since', [ 0, ])[0] limit = data.get('limit', [ 0, ])[0] sort_by = data.get('sort', 'flat') related = data.get('related', [ 0, ]) is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': ['short_name'], 'columns_values': [forum_sh_name], 'table': 'Forum' } # sql_check = build_sql_select_all_query(sql_scheme) #res = open_sql(sql_check) # check if exists res = True if not res: code = 5 else: sql_scheme = { 'columns_names': ['forum'], 'columns_values': [forum_sh_name], 'table': 'Post' } if since != 0: larger = {'date': since} sql = build_sql_select_all_query(sql_scheme, is_desc, limit, larger) else: sql = build_sql_select_all_query(sql_scheme, is_desc, limit) posts_list_dict = open_sql_all(sql, first=True, is_closing=False) posts_list = posts_list_dict['result'] db = posts_list_dict['db'] crs = posts_list_dict['cursor'] resp_keys = [ 'date', 'forum', 'id', 'isApproved', 'isDeleted', 'isEdited', 'isHighlighted', 'isSpam', 'message', 'parent', 'thread', 'user', 'likes', 'dislikes', 'points' ] resp_dict = [] final_resp = make_response(code=code) if code == 0 and posts_list: for res in posts_list: resp_values = [ str(res['date']), res['forum'], res['id'], bool(res['isApproved']), bool(res['isDeleted']), bool(res['isEdited']), bool(res['isHighlighted']), bool(res['isSpam']), res['message'], res['parent'], res['thread'], res['user'], res['likes'], res['dislikes'], res['points'] ] resp_dict.append( make_response(resp_keys, resp_values, code)['response']) if 'thread' in related: for x in resp_dict: x['thread'] = get_details_thread({ 'thread': [ x['thread'], ], 'is_closing': [ False, ], 'cursor': [ crs, ] })['response'] if 'forum' in related: for x in resp_dict: x['forum'] = get_details_forum({ 'forum': [ x['forum'], ], 'is_closing': [ False, ], 'cursor': [ crs, ] })['response'] if 'user' in related: for x in resp_dict: x['user'] = get_details_user({ 'user': [ x['user'], ], 'is_closing': [ False, ], 'cursor': [ crs, ] })['response'] final_resp = {'code': code, 'response': resp_dict} db.close() return final_resp
def follow_user(data): code = 0 keys = [ 'id', 'username', 'about', 'name', 'email', 'isAnonymous', 'followers', 'following', 'subscriptions' ] values = [1, 'noname', 'qwe', 'lal', '*****@*****.**', 0, 'a', 'b', 'c'] if not data: resp_dict = make_response(keys, values, code=0) return flask.jsonify(resp_dict) email_1 = data['follower'] email_2 = data['followee'] sql_scheme_usr1 = { 'columns_names': ['email'], 'columns_values': [email_1], 'table': 'User' } sql_scheme_usr2 = { 'columns_names': ['email'], 'columns_values': [email_2], 'table': 'User' } sql_scheme_follow = { 'columns_names': ['follower', 'followee'], 'columns_values': [email_1, email_2], 'table': 'Followers' } #sql_check_usr1 = build_sql_select_all_query(sql_scheme_usr1, what=' id ', limit=1) #sql_check_usr2 = build_sql_select_all_query(sql_scheme_usr2, what=' id ', limit=1) sql_check_follow = build_sql_select_all_query(sql_scheme_follow) #res_usr1 = open_sql(sql_check_usr1) # check if exists #res_usr2 = open_sql(sql_check_usr2) res_usr1 = True res_usr2 = True res_follow = open_sql(sql_check_follow) if res_usr1 and res_usr2 and not res_follow: sql = build_sql_insert_query(sql_scheme_follow) exec_message = exec_sql(sql) if exec_message < 0: #code = 4 a = 0 usr_details = get_details_user({'user': [ email_1, ]}) usr_details = usr_details['response'] if 'id' in usr_details: values = [ int(usr_details['id']), usr_details['username'], usr_details['about'], usr_details['name'], usr_details['email'], bool(usr_details['isAnonymous']), usr_details['followers'], usr_details['following'], usr_details['subscriptions'] ] resp_dict = make_response(keys, values, code) return flask.jsonify(resp_dict)
def get_forum_users_list(data): code = 0 posts_list = [] users_res = [] forum_sh_name = data.get('forum')[0] since = data.get('since_id', [ 0, ])[0] limit = data.get('limit', [ 1, ])[0] is_desc = 0 order_by = data.get('order', 'desc') if 'desc' in order_by: is_desc = 1 sql_scheme = { 'columns_names': ['short_name'], 'columns_values': [forum_sh_name], 'table': 'Forum' } #sql_check = build_sql_select_all_query(sql_scheme) # res = open_sql(sql_check) # check if exists res = True if not res: code = 2 else: sql_scheme = { 'columns_names': ['forum'], 'columns_values': [forum_sh_name], 'table': 'Post' } if since != 0: larger = {'id': since} sql = build_sql_select_all_query(sql_scheme, group='user', what='user', larger=larger, limit=limit) else: sql = build_sql_select_all_query(sql_scheme, group='user', what='user', limit=limit) posts_list_dict = open_sql_all(sql) posts_list = posts_list_dict if posts_list == -1 or not posts_list: return make_response([], [], code=4) #!db = posts_list_dict['db'] #!crs = posts_list_dict['cursor'] mails = [] for x in posts_list: mails.append(x['user']) if mails: sql_scheme = { 'columns_names': ['email'], 'columns_values': mails, 'table': 'User' } if since != 0: sql = build_sql_select_all_query(sql_scheme, larger={'id': since}, limit=limit, ord_by=' name ', is_desc=is_desc, in_set=True) else: sql = build_sql_select_all_query(sql_scheme, limit=limit, ord_by=' name ', is_desc=is_desc, in_set=True) users_res_dict = open_sql_all( sql) #!, first=False, is_closing=False, cursor=crs) users_res = users_res_dict #!['result'] if users_res == -1 and not users_res: return make_response([], [], code=4) resp_list = [] final_resp = make_response(code=code) if code == 0 and users_res: for res in users_res: #usr_details = get_details_user({'user': [res['email'], ]}) #usr_details = usr_details['response'] res['subscriptions'] = [] #usr_details['subscriptions'] res['followers'] = [] #usr_details['followers'] res['following'] = [] #usr_details['following'] resp_list.append(res) #!db.close() final_resp = {'code': code, 'response': resp_list} return final_resp
def get_details_user(data, by_id=False): code = 0 keys = [] values = [] followers = [] following = [] subscriptions = [] email = data['user'][0] col_name = 'email' col_val = email if by_id: col_name = 'id' col_val = data['user_id'][0] sql_scheme = { 'columns_names': [col_name], 'columns_values': [col_val], 'table': 'User' } is_closing = data.get('is_closing', [1, ])[0] cursor = data.get('cursor', [1, ])[0] sql_check = build_sql_select_all_query(sql_scheme) if is_closing == 1 and cursor == 1: res = open_sql(sql_check) # check if exists else: res = open_sql(sql_check, first=False, is_closing=False, cursor=cursor)['result'] if not res: code = 1 else: sql_scheme_get_followers = { 'columns_names': ['followee', 'isDeleted'], 'columns_values': [res['email'], 0], 'table': 'Followers' } sql_scheme_get_following = { 'columns_names': ['follower', 'isDeleted'], 'columns_values': [res['email'], 0], 'table': 'Followers' } if cursor == 1: sql_get_followers_usr1 = build_sql_select_all_query(sql_scheme_get_followers, what='follower') sql_get_following_usr1 = build_sql_select_all_query(sql_scheme_get_following, what='followee') followers = open_sql_all(sql_get_followers_usr1) following = open_sql_all(sql_get_following_usr1) sql_scheme_get_subscriptions = { 'columns_names': ['user', 'isDeleted'], 'columns_values': [res['email'], 0], 'table': 'Subscribe' } sql_get_subscriptions = build_sql_select_all_query(sql_scheme_get_subscriptions, what=' thread ') subscriptions = open_sql_all(sql_get_subscriptions) followers_list = [] following_list = [] subscriptions_list = [] if followers: for x in followers: followers_list.append(x['follower']) if following: for x in following: following_list.append(x['followee']) if subscriptions: for x in subscriptions: subscriptions_list.append(x['thread']) keys = ['about', 'email', 'followers', 'following', 'id', 'isAnonymous', 'name', 'subscriptions', 'username'] values = [res['about'], res['email'], followers_list, following_list, int(res['id']), bool(res['isAnonymous']), res['name'], subscriptions_list, res['username']] resp_dict = make_response(keys, values, code) return resp_dict
def create_thread(data): code = 0 keys = [ 'id', 'forum', 'title', 'isClosed', 'user', 'date', 'message', 'slug', 'isDeleted' ] values = [ 1, 'forum', 'title', 'isclosed', 'email', 'date', 'message', 'slug', 'isdeleted' ] if not data: resp_dict = make_response(keys, values, code=0) return flask.jsonify(resp_dict) forum = data.get('forum', None) title = data.get('title', None) isclosed = data.get('isClosed', None) date = data.get('date', None) message = data.get('message', None) slug = data.get('slug', None) email = data.get('user', None) isdeleted = data.get('isDeleted', False) sql_scheme = { 'columns_names': ['title'], 'columns_values': [title], 'table': 'Thread' } sql_check = build_sql_select_all_query(sql_scheme, what=' id ', limit=1) #res = open_sql(sql_check) # check if exists res = False if not res: sql_scheme = { 'columns_names': [ 'forum', 'title', 'isClosed', 'user', 'date', 'message', 'slug', 'isDeleted' ], 'columns_values': [ forum, title, int(isclosed), email, date, message, slug, int(isdeleted) ], 'table': 'Thread' } sql = build_sql_insert_query(sql_scheme) exec_message = exec_sql(sql) if exec_message >= 0: values = [ exec_message, forum, title, isclosed, email, date, message, slug, isdeleted ] else: #code = 4 a = 0 #!keys = ['id', 'date', 'forum', 'isClosed', 'isDeleted', 'message', 'slug', 'title', 'user'] #!values = [res['id'], str(res['date']), res['forum'], bool(res['isClosed']), bool(res['isDeleted']), # res['message'], # res['slug'], res['title'], res['user']] resp_dict = make_response(keys, values, code, sql_check) return flask.jsonify(resp_dict)