def select_partisan(datas): conn = getconn() cur = conn.cursor() ids = datas.get('id') try: partisan_all_select_sql = """SELECT `id`,`partisan`,`partisan_image` FROM `partisan` LIMIT %s,%s""" partisan_one_select_sql = """SELECT `id`,`partisan`,`partisan_image` FROM `partisan` WHERE `id` = %s""" if ids == None: page_number = datas.get('page_number') page_size = datas.get('page_size') start = (int(page_number) - 1) * int(page_size) re = cur.execute(partisan_all_select_sql, (start, int(page_size))) if re < 1: app.logger.error('党派所有信息') return 0 else: re = cur.execute(partisan_one_select_sql, (ids)) if re < 1: app.logger.error('无某一党派信息') return 0 result = cur.fetchall() lists = [] for item in result: dict = {} dict['id'] = item[0] dict['partisan'] = item[1] dict['partisan_image'] = item[2] lists.append(dict) return lists except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def insert_partisan(datas): conn = getconn() cur = conn.cursor() partisan = datas.get('partisan') insert_support_sql = """INSERT INTO `partisan` (`partisan`) VALUES (%s)""" select_support_sql = """SELECT `partisan` FROM `partisan` WHERE `partisan` = %s """ try: insert_re = '' select_re = cur.execute(select_support_sql, (partisan)) if select_re != 0: app.logger.error('党派数据已经存在') return 406 elif select_re == 0: insert_re = cur.execute(insert_support_sql, (partisan)) if insert_re < 1: app.logger.error('党派数据未写入') return 0 else: app.logger.error('党派数据写入成功') return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_message_info_page(page, count, user): conn = getconn() cur = conn.cursor() role_number = get_user_roles(user) try: if int(page) == 1 or int(page) == 0: page_t = 0 else: page_t = (int(page) - 1) * int(count) if role_number == '0' or role_number == '1': select_info = '''SELECT `id`,`user` ,`model_info` ,`message` ,`time` FROM `messages` LIMIT %s, %s;''' re = cur.execute(select_info, (int(page_t), int(count))) else: select_info = '''SELECT `id`,`user` ,`model_info` ,`message` ,`time` FROM `messages` WHERE `user`= %s LIMIT %s, %s;''' re = cur.execute(select_info, (user, int(page_t), int(count))) if re < 1: return 0 else: result = cur.fetchall() lists = [] for item in result: dict = {} dict['id'] = item[0] dict['user'] = item[1] dict['model_info'] = item[2] dict['message'] = item[3] dict['time'] = item[4] lists.append(dict) return lists except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_area_info_one(data): conn = getconn() cur = conn.cursor() info_id = data['info_id'] try: select_sql = """SELECT `info_id`,`administrative_id` ,`area_info` ,`governance_situation` ,`year` FROM `administrative_infos` WHERE `info_id` = %s """ re = cur.execute(select_sql, (info_id)) if re < 1: return 0 else: select_admini_name() item = cur.fetchone() # pro_name = select_pro_by_id(item[1]) dict = {} for k, v in admini_id_name.items(): if item[1] == k: dict['administrative_name'] = admini_id_name[item[1]] dict['info_id'] = item[0] dict['administrative_id'] = item[1] # dict['administrative_name'] = pro_name dict['area_info'] = item[2] dict['governance_situation'] = item[3] dict['year'] = item[4] return dict except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_user_info_page(page, count): conn = getconn() cur = conn.cursor() try: select_info = '''SELECT `id`,`user`,`position` FROM `users` LIMIT %s, %s;''' count = count if int(page) == 1 or int(page) == 0: page_t = 0 else: page_t = (int(page) - 1) * int(count) re = cur.execute(select_info, (int(page_t), int(count))) if re < 1: return 0 else: result = cur.fetchall() lists = [] for item in result: dict = {} dict['id'] = item[0] dict['user'] = item[1] dict['position'] = item[2] lists.append(dict) return lists except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def get_google_trend(start_time, end_time, dict_name): conn = getconn() cur = conn.cursor() list_data = get_date(start_time, end_time) start_time = get_date_item(start_time) end_time = get_date_item(end_time) series_dict = {} try: dict = {} for id in dict_name.keys(): series = [] sql = "select trend,date_time from google_trend where DATE(date_time) >= %s and DATE(date_time)<= %s and candidate_id=%s" count = cur.execute(sql, (start_time, end_time, id)) result = cur.fetchmany(count) dict_item = {} # Can be optimized # --------------------------------------------------------------------------------------- for item in result: dict_item[item[1].strftime("%Y-%m-%d")] = item[0] for item in list_data: if item not in dict_item: dict_item[item] = 0 dict[dict_name[id]] = dict_item for item in list_data: series.append(dict[dict_name[id]][item]) series_dict[dict_name[id]] = series result = {} result["xAxis"] = list_data result["series"] = series_dict # ----------------------------------------------------------------------------------------- return result except: return 0 finally: closeAll(conn, cur)
def insert_election_info(administrative_id, elector, election_score, election_parties, period, year): conn = getconn() cur = conn.cursor() insert_sql = """INSERT INTO `previous_elections` (`administrative_id`,`elector`,`election_score`,`election_parties`,`period`,`year`) VALUES (%s,%s,%s,%s,%s,%s)""" select_sql = """SELECT `id` FROM `previous_elections` WHERE `administrative_id` = %s AND `elector`= %s AND `election_score`= %s AND `election_parties`= %s AND `period`= %s AND `year`= %s""" try: insert_re = '' select_re = cur.execute(select_sql, (administrative_id, elector, election_score, election_parties, period, year)) if select_re != 0: return 406 elif select_re == 0: insert_re = cur.execute( insert_sql, (administrative_id, elector, election_score, election_parties, period, year)) if insert_re < 1: return 0 else: return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_users_all(): conn = getconn() cur = conn.cursor() try: select_sql = """SELECT `id`,`user` ,`position` FROM `users` """ re = cur.execute(select_sql) if re < 1: return 0, 0 else: count = re result = cur.fetchall() lists = [] for item in result: dict = {} dict['id'] = item[0] dict['user'] = item[1] dict['position'] = item[2] lists.append(dict) return lists, count except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_election_all(): conn = getconn() cur = conn.cursor() try: select_sql = """SELECT `id`,`administrative_id` ,`elector` ,`election_score` ,`election_parties` ,`period` ,`year` FROM `previous_elections` """ re = cur.execute(select_sql) if re < 1: return 0, 0 else: count = re # select_admini_name() result = cur.fetchall() lists = [] for item in result: dict = {} for k, v in admini_id_name.items(): if item[1] == k: dict['administrative_name'] = admini_id_name[item[1]] dict['id'] = item[0] dict['administrative_id'] = item[1] dict['elector'] = item[2] dict['election_score'] = item[3] dict['election_parties'] = item[4] dict['period'] = item[5] dict['year'] = item[6] lists.append(dict) return lists, count except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_reply_info(user): conn = getconn() cur = conn.cursor() role_number = get_user_roles(user) # print(user, role_number) try: if role_number == '0' or role_number == '1': select_sql = """SELECT `id`,`message_id` ,`reply_message` ,`replyer` ,`time` FROM `reply_messages` """ re = cur.execute(select_sql) else: select_sql = """SELECT `id`,`message_id` ,`reply_message` ,`replyer` ,`time` FROM `reply_messages` WHERE `message_author` = %s""" re = cur.execute(select_sql, user) if re < 1: return 0, 0 else: count = re result = cur.fetchall() lists = [] for item in result: dict = {} dict['id'] = item[0] dict['message_id'] = item[1] dict['reply_message'] = item[2] dict['replyer'] = item[3] dict['time'] = item[4] lists.append(dict) return lists, count except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def save_new_file(file_root, file_name, user_name, is_private, file_label): conn = getconn() cur = conn.cursor() file_root = file_root file_name = file_name user_name = user_name is_private = is_private insert_sql = """INSERT INTO `word_files` (`user`,`word_name`,`is_private`,`file_label`) VALUES (%s,%s,%s,%s)""" select_sql = """SELECT `id` FROM `word_files` WHERE `word_name` = %s """ try: select_re = cur.execute(select_sql, (file_name)) if select_re != 0: return 2 insert_re = cur.execute(insert_sql, (user_name, file_name, is_private, file_label)) if insert_re < 1: return 0 else: document = Document(r'{}'.format(file_root)) document_new = Document() for paragraph in document.paragraphs: line = paragraph.text new_line = cht_to_chs(line) document_new.add_paragraph('{}'.format(new_line)) document_new.save(r'{}'.format(file_root)) return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_area_info(): conn = getconn() cur = conn.cursor() try: select_sql = """SELECT `info_id`,`administrative_id` ,`area_info` ,`governance_situation` ,`year` FROM `administrative_infos` """ re = cur.execute(select_sql) if re < 1: return 0, 0 else: count = re select_admini_name() result = cur.fetchall() lists = [] for item in result: dict = {} for k, v in admini_id_name.items(): if item[1] == k: dict['administrative_name'] = admini_id_name[item[1]] dict['info_id'] = item[0] dict['administrative_id'] = item[1] # dict['administrative_name'] = pro_name dict['area_info'] = item[2] dict['governance_situation'] = item[3] dict['year'] = item[4] lists.append(dict) return lists, count except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def delete_messages(user_name, message_id): conn = getconn() cur = conn.cursor() try: # 首先验证要删除的消息的用户是否是创建消息的用户 select_sql = """SELECT `user` FROM `messages` WHERE `id`= %s""" delete_sql = """DELETE FROM `messages` WHERE id = %s """ re = cur.execute(select_sql, message_id) if re < 1: return 0 else: result = cur.fetchone() original_name = result[0] if original_name != user_name: return "您没有权限删除该消息" else: re = cur.execute(delete_sql, message_id) if re < 1: return 0 return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_election_info_one(data): conn = getconn() cur = conn.cursor() id = data['id'] try: select_sql = """SELECT `id`,`administrative_id` ,`elector` ,`election_score` ,`election_parties` ,`period` ,`year` FROM `previous_elections` WHERE `id` = %s """ re = cur.execute(select_sql, (id)) if re < 1: return 0 else: select_admini_name() item = cur.fetchone() dict = {} for k, v in admini_id_name.items(): if item[1] == k: dict['administrative_name'] = admini_id_name[item[1]] dict['id'] = item[0] dict['administrative_id'] = item[1] # dict['administrative_name'] = pro_name dict['elector'] = item[2] dict['election_score'] = item[3] dict['election_parties'] = item[4] dict['period'] = item[5] dict['year'] = item[6] return dict except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def get_pages(datas): conn = getconn() cur = conn.cursor() info_type = datas.get('info_type') try: if info_type == '6': #民调数据所有数据 candidate_id = datas.get('candidate_id') suopport_all_select_sql = """SELECT `id` FROM `poll_support` WHERE `candidate_id` = %s""" suopport_all_count = cur.execute(suopport_all_select_sql, (candidate_id)) if suopport_all_count < 1: app.logger.error('无某一侯选人所有民调信息') return 0 else: return suopport_all_count elif info_type == '7': #党派数据所有数据 partisan_all_select_sql = """SELECT `id` FROM `partisan`""" partisan_all_count = cur.execute(partisan_all_select_sql) if partisan_all_count < 1: app.logger.error('无所有党派信息') return 0 else: return partisan_all_count except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def insert_support(datas): conn = getconn() cur = conn.cursor() times = datas.get('support_time') support_time = get_date_item(times) candidate_id = datas.get('candidate_id') if candidate_id == None or support_time == None: app.logger.error('侯选人id或者民调月份未写入') return 0 twenty = datas.get('20s-30s') if twenty == None: twenty = '0.00' thirty = datas.get('30s-40s') if thirty == None: thirty = '0.00' forty = datas.get('40s-50s') if forty == None: forty = '0.00' fifty = datas.get('50s-60s') if fifty == None: fifty = '0.00' sixty = datas.get('60s-70s') if sixty == None: sixty = '0.00' seventy = datas.get('70s') if seventy == None: seventy = '0.00' network_support = datas.get('network_support') if network_support == None: network_support = '0.00' total_support = datas.get('total_support') if total_support == None: total_support = '0.00' insert_support_sql = """INSERT INTO `poll_support` (`candidate_id`,`20s-30s`,`30s-40s`,`40s-50s`,`50s-60s`,`60s-70s`,`70s`,`network_support`,`total_support`,`support_time`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""" select_support_sql = """SELECT `support_time` FROM `poll_support` WHERE `support_time` = %s AND `candidate_id`= %s """ try: insert_re = '' select_re = cur.execute(select_support_sql, (support_time, candidate_id)) if select_re != 0: app.logger.error('该月份数据已经存在') return 406 elif select_re == 0: insert_re = cur.execute( insert_support_sql, (candidate_id, twenty, thirty, forty, fifty, sixty, seventy, network_support, total_support, support_time)) if insert_re < 1: app.logger.error('民调数据未写入') return 0 else: app.logger.error('民调数据写入成功') return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def get_region_dict(data): name = data.get('name') id = data.get('id') every = data.get('every') conn = getconn() cur = conn.cursor() if len(data) == 1 and name != None: select_sql = """SELECT `administrative_id` FROM `administrative_area` WHERE `administrative_name` = %s""" try: cur.execute(select_sql, (name)) name_re = cur.fetchone() except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur) if name_re != '' or name_re != None: return name_re[0] else: return 0 elif len(data) == 1 and id != None: select_sql = """SELECT `administrative_name` FROM `administrative_area` WHERE `administrative_id` = %s""" try: cur.execute(select_sql, (id)) id_re = cur.fetchone() except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur) if id_re != '' or id_re != None: return id_re[0] else: return 0 elif len(data) == 1 and every != None: select_sql = """SELECT `administrative_id`,`administrative_name` FROM `administrative_area` """ try: cur.execute(select_sql) all_re = cur.fetchall() except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur) result_list = [] if all_re != '' or all_re != None: for item in all_re: result_dict = {} result_dict['administrative_id'] = item[0] result_dict['administrative_name'] = item[1] result_list.append(result_dict) return result_list else: return 0
def process_delete(file_name, user_name): # 判断是否是docx结尾,避免删掉系统py文件 conn = getconn() cur = conn.cursor() word_name = file_name user = user_name try: file_name_list = file_name.split('.') if file_name_list[1] != "docx": app.logger.error("传入文件格式不对") return 0 # 查询该文件是否属于该用户 select_sql = """SELECT `id` FROM `word_files` WHERE `word_name` = %s AND `user`= %s """ select_re = cur.execute(select_sql, (word_name, user)) cur.close() conn.close() if select_re == 0: app.logger.error("该用户没有该文件") return 3 elif select_re != 0: delete_sql = """DELETE FROM `word_files` WHERE `word_name` = %s """ conn = getconn() cur = conn.cursor() re = cur.execute(delete_sql, (word_name)) if re < 1: conn.rollback() cur.close() conn.close() return 0 else: try: os.remove(os.path.join(UPLOAD_FILES, file_name)) except Exception as e: return 0 return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def login(name, password): conn = getconn() cur = conn.cursor() try: password = md5(password) sql = "select * from users where user=%s and passwd=%s" result = cur.execute(sql, (name, password)) return result except (Exception) as e: app.logger.error(e) return "0" finally: closeAll(conn, cur)
def delete_reply_info(reply_id): conn = getconn() cur = conn.cursor() try: delete_sql = """DELETE FROM `reply_messages` WHERE id = %s """ re = cur.execute(delete_sql, reply_id) if re < 1: return 0 return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def get_egional_images(id): conn = getconn() cur = conn.cursor() try: sql = "select image_name from candidate_personnel_information where candidate_id =%s" cur.execute(sql, id) result = cur.fetchone() if result[0] == "": return 0 return result[0] except (Exception) as e: return 0 finally: closeAll(conn, cur)
def get_all_years(info_type, data): conn = getconn() cur = conn.cursor() if info_type == 'years': try: administrative_id = data.get('administrative_id') years_select_sql = """SELECT `year` FROM `candidate` WHERE `administrative_id` = %s GROUP BY `year`""" re = cur.execute(years_select_sql, (administrative_id)) if re < 1: return [] else: result = cur.fetchall() result_lists = [] mid_list = [] dict = {} for item in result: mid_list.append(item[0]) dict['years'] = mid_list result_lists.append(dict) return result_lists except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur) elif info_type == 'candidates': try: year = data.get('year') administrative_id = data.get('administrative_id') candidates_select_sql = """SELECT `candidate_id`,`username` FROM `candidate` WHERE `year` = %s AND `administrative_id` = %s""" re = cur.execute(candidates_select_sql, (year, administrative_id)) if re < 1: return 0 else: result = cur.fetchall() lists = [] for item in result: dict = {} dict['candidate_id'] = item[0] dict['name'] = item[1] lists.append(dict) return lists except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def delete_area_info(data): conn = getconn() cur = conn.cursor() info_id = data.get('info_id') try: delete_sql = """DELETE FROM `administrative_infos` WHERE info_id = %s """ re = cur.execute(delete_sql, (info_id)) if re < 1: return 0 else: return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def get_user_roles(user): conn = getconn() cur = conn.cursor() try: select_info = '''SELECT `id`,`user` ,`position` FROM `users` WHERE `user` = %s;''' re = cur.execute(select_info, user) if re < 1: return 0 else: result = cur.fetchone() return result[2] except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_admini_name(): conn = getconn() cur = conn.cursor() try: select_sql = """SELECT `administrative_id`, `administrative_name` FROM `administrative_area` """ re = cur.execute(select_sql) if re: items = cur.fetchall() for item in items: admini_id_name[item[0]] = item[1] except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def delete_election_info(data): conn = getconn() cur = conn.cursor() id = data.get('id') try: delete_sql = """DELETE FROM `previous_elections` WHERE id = %s """ re = cur.execute(delete_sql, (id)) if re < 1: return 0 else: return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def get_partisan(id): conn = getconn() cur = conn.cursor() try: sql = "select partisan from candidate_personnel_information where candidate_id =%s" cur.execute(sql, id) result = cur.fetchone() if result[0] == "": return "无党" if result[0] == None: return "无党" return result[0] except (Exception) as e: return "无党" finally: closeAll(conn, cur)
def select_election_info_page(page, count): conn = getconn() cur = conn.cursor() try: select_info = '''SELECT `id`,`administrative_id`,`elector`,`election_score`,`election_parties`,`period`,`year` FROM `previous_elections` LIMIT %s, %s;''' # select_sql2 = """SELECT `*` FROM `previous_elections` """ # re2 = cur.execute(select_sql2) # all_pages = re2 / int(count) # if all_pages < 1: # all_pages = 1 # else: # all_pages = math.ceil(all_pages) count = count if int(page) == 1 or int(page) == 0: page_t = 0 else: page_t = (int(page) - 1) * int(count) re = cur.execute(select_info, (int(page_t), int(count))) if re < 1: return 0 else: select_admini_name() result = cur.fetchall() lists = [] for item in result: # pro_name = select_pro_by_id(item[1]) dict = {} for k, v in admini_id_name.items(): if item[1] == k: dict['administrative_name'] = admini_id_name[item[1]] dict['id'] = item[0] dict['administrative_id'] = item[1] # dict['administrative_name'] = pro_name dict['elector'] = item[2] dict['election_score'] = item[3] dict['election_parties'] = item[4] dict['period'] = item[5] dict['year'] = item[6] # dict['all_pages'] = all_pages lists.append(dict) return lists except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def update_users_info(datas): where_dict = datas.get('where_dict') up_dict = datas.get('up_dict') if len(where_dict) == 0 or len(up_dict) == 0: if len(where_dict) == 0: return "更新条件不能为空" if len(up_dict) == 0: return "更新内容不能为空" else: conn = getconn() cur = conn.cursor() try: where_sql = '' up_sql = '' where_num = 0 up_num = 0 for k, v in where_dict.items(): ency_password = select_user_secret(v) print(ency_password) where_num += 1 if where_num < len(where_dict): where_sql += "{} = '{}' AND ".format(k, v) else: where_sql += "{} = '{}'".format(k, v) for k, v in up_dict.items(): if k == "passwd": if v: v = md5(v) else: v = ency_password up_num += 1 if up_num < len(up_dict): up_sql += "{} = '{}', ".format(k, v) else: up_sql += "{} = '{}' ".format(k, v) update_sql = """UPDATE `users` SET """ + up_sql + ' WHERE ' + where_sql re = cur.execute(update_sql) if re < 1: return 0 else: return 1 except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)
def select_support(datas): conn = getconn() cur = conn.cursor() candidate_id = datas.get('candidate_id') ids = datas.get('id') try: suopport_all_select_sql = """SELECT `id`,`candidate_id`,`20s-30s`,`30s-40s`,`40s-50s`,`50s-60s`,`60s-70s`,`70s`,`network_support`,`total_support`,`support_time` FROM `poll_support` WHERE `candidate_id` = %s LIMIT %s,%s""" suopport_one_select_sql = """SELECT `id`,`candidate_id`,`20s-30s`,`30s-40s`,`40s-50s`,`50s-60s`,`60s-70s`,`70s`,`network_support`,`total_support`,`support_time` FROM `poll_support` WHERE `id` = %s""" if candidate_id != None: page_number = datas.get('page_number') page_size = datas.get('page_size') start = (int(page_number) - 1) * int(page_size) re = cur.execute(suopport_all_select_sql, (candidate_id, start, int(page_size))) if re < 1: app.logger.error('无某一候选人民调所有信息') return 0 else: re = cur.execute(suopport_one_select_sql, (ids)) if re < 1: app.logger.error('无某一民调信息') return 0 result = cur.fetchall() lists = [] for item in result: dict = {} dict['id'] = item[0] dict['candidate_id'] = item[1] dict['20s-30s'] = item[2] dict['30s-40s'] = item[3] dict['40s-50s'] = item[4] dict['50s-60s'] = item[5] dict['60s-70s'] = item[6] dict['70s'] = item[7] dict['network_support'] = item[8] dict['total_support'] = item[9] dict['support_time'] = item[10] lists.append(dict) return lists except Exception as erro: app.logger.error(erro) return 0 finally: closeAll(conn, cur)