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 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 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 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_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 truncate_all(): code = 0 tables = ['Post', 'Thread', 'Forum', 'User', 'Subscribe', 'Followers'] tbl_list = [] for x in tables: sql = 'truncate %s ;' % x tbl_list.append(sql) exec_sql(tbl_list, multi=True) resp_dict = {'code': code, 'response': 'Ok'} 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 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 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 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 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 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 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 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_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 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 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 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 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 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 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 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 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 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 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)
__author__ = 'kic' from forum_app.api_packs.db_queries.queries import exec_sql if __name__ == '___main__': exec_sql( ' create table User(id int auto_increment PRIMARY KEY, name varchar(255), isAnonymous tinyint(1)' / ' not null default 0, email varchar(255) unique, about mediumtext, username varchar(255))' ) exec_sql( ' create table Thread(id int auto_increment PRIMARY KEY, date datetime, title varchar(255),' / ' isClosed tinyint(1) not null default 0, isDeleted tinyint(1) not null default 0, message mediumtext,' / ' slug varchar(255), likes int(11) not null default 0, dislikes int(11) not null default 0,' / ' points int(11) not null default 0, user varchar(255), forum varchar(255), posts int(11) not null default 0);' ) exec_sql( ' create table Subscribe(id int auto_increment PRIMARY KEY, thread int(11), user varchar(255),' / ' isDeleted tinyint(1) not null default 0);') exec_sql( ' create table Post(id int auto_increment PRIMARY KEY, date datetime,' / ' isApproved tinyint(1) not null default 0, isDeleted tinyint(1) not null default 0,' / ' isEdited tinyint(1) not null default 0, isHighlighted tinyint(1) not null default 0,'