def unfollow_user(data): code = 1 keys = [] values = [] resp_dict = {'code': code, 'response': []} 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) sql_check_usr2 = build_sql_select_all_query(sql_scheme_usr2) 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_follow = open_sql(sql_check_follow) if res_usr1 and res_usr2 and res_follow: sql_scheme_follow = { 'columns_names': ['isDeleted'], 'columns_values': [1], 'table': 'Followers', 'condition': {'id': res_follow['id']} } sql = build_sql_update_query(sql_scheme_follow) exec_message = exec_sql(sql) if exec_message != 0: code = 4 usr_details = get_details_user({'user': [res_usr1['email'], ]}) usr_details = usr_details['response'] resp_dict = {'code': code, 'response': usr_details} else: code = 2 #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 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 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 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 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 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 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 update_user(data): code = 0 keys = [] values = [] about = data['about'] user_email = data['user'] name = data['name'] 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) if res: sql_scheme = { 'columns_names': ['name', 'about'], 'columns_values': [name, about], 'condition': {'email': user_email}, 'table': 'User' } exec_sql(build_sql_update_query(sql_scheme)) user_data = {'user': [res['email'], ]} user_resp = get_details_user(user_data)['response'] else: code = 1 resp_dict = {'code': code, 'response': user_resp} 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 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 update_user(data): code = 0 keys = [] values = [] about = data['about'] user_email = data['user'] name = data['name'] 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) if res: sql_scheme = { 'columns_names': ['name', 'about'], 'columns_values': [name, about], 'condition': { 'email': user_email }, 'table': 'User' } exec_sql(build_sql_update_query(sql_scheme)) user_data = { 'user': [ res['email'], ] } user_resp = get_details_user(user_data)['response'] else: code = 1 resp_dict = {'code': code, 'response': user_resp} 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 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_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_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 unfollow_user(data): code = 1 keys = [] values = [] resp_dict = {'code': code, 'response': []} 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) sql_check_usr2 = build_sql_select_all_query(sql_scheme_usr2) 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_follow = open_sql(sql_check_follow) if res_usr1 and res_usr2 and res_follow: sql_scheme_follow = { 'columns_names': ['isDeleted'], 'columns_values': [1], 'table': 'Followers', 'condition': { 'id': res_follow['id'] } } sql = build_sql_update_query(sql_scheme_follow) exec_message = exec_sql(sql) if exec_message != 0: code = 4 usr_details = get_details_user({'user': [ res_usr1['email'], ]}) usr_details = usr_details['response'] resp_dict = {'code': code, 'response': usr_details} else: code = 2 #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 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_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 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)