def insert_launch(usr, title, captcha, votelimit, total): ''' 功能:往lauch表中插入一条活动记录 接受参数: usr: 创建者 title: 活动标题 capthca: 邀请码(唯一) votelimit(int): 个人投票有效票数 total(text) 返回参数: 1 插入成功 0 数据库端错误 -1 验证码不唯一, 已经创建过相同的活动 ''' flag = check_captcha(captcha) if flag == 0: return 0 # 数据库端错误 elif flag == -1: return -1 # 验证码不唯一, 已经创建过相同的活动 usr = md5(usr) sql = "insert into launch(usr, title, captcha, votelimit, total) values('{}', '{}', '{}', {}, '{}')".format( usr, title, captcha, votelimit, total) flag, e = op_mysql(sql) if flag == False: print(e) return 0 #数据库端错误 else: return 1 #插入成功
def getVotenum(captcha, usr, votelimit): ''' # 功能:在voterecords表中获取cpatcha和usr相对应的votenum # 接受参数:cpatcha, usr, votelimit(int) # 返回参数:votenum(int) -1 数据库端错误 or votenum ''' flag = checkUsrRecord(captcha, usr) if flag == 0: return -1 elif flag == -1: flag = insertUsrRecord(captcha, usr, votelimit) if flag == True: return votelimit else: return -1 usr = md5(usr) sql = "select votenum from voterecords where captcha='%s' and usr='******'" % ( captcha, usr) flag, res = op_mysql(sql) if flag == 0: print(res) return -1 for x in res: return x[0]
def updataUsrRecord(captcha, usr, votenum): ''' # 功能:更新voterecords表中的某活动用户投票数量 # 接受参数: # 返回参数:True / False ''' usr = md5(usr) sql = """update voterecords set votenum = {} where captcha = '{}' and usr = '******'""".format( votenum, captcha, usr) flag, res = op_mysql(sql) if flag == False: print(flag) return flag
def insertUsrRecord(captcha, usr, votenum): ''' # 功能:在voterecords表中插入一条记录 # 接受参数: # 返回参数:True / False ''' usr = md5(usr) sql = "insert into voterecords (captcha, usr, votenum) values ('{}', '{}', {})".format( captcha, usr, votenum) flag, res = op_mysql(sql) if flag == False: print(res) return flag
def delVoteActivity(captcha): ''' # 功能:在launch, votedata, voterecords表captcha的相关记录 # 接受参数:captcha # 返回参数:True / False ''' sql = "delete from votedata where captcha = '%s'" % captcha flag, e = op_mysql(sql) if flag == False: print(e) return False sql = "delete from voterecords where captcha = '%s'" % captcha flag, e = op_mysql(sql) if flag == False: print(e) return False sql = "delete from launch where captcha='%s'" % captcha flag, e = op_mysql(sql) if flag == False: print(e) return False return True
def updateTotal(captcha, pubkey, C2): ''' # 功能:更新记录captcha对应的投票总数 # 接受参数:captcha(邀请码), pubkey(公钥), C2(int) # 返回参数: ''' C1 = getTotal(captcha) C = pubkey.evaluate_int(C1, C2) C = str(C[0]) + ',' + str(C[1]) sql = "update launch set total = '{}' where captcha = '{}'".format( C, captcha) flag, res = op_mysql(sql) if flag == False: print(res) return flag
def getVoteData(captcha): ''' # 功能:在votedata表中返回captcha记录对应(choice, tag) # 接受参数:captcha # 返回参数:[[choice(str), tag(int)]...] ''' sql = "select choice, tag from votedata where captcha = '%s'" % (captcha) flag, res = op_mysql(sql) if flag == False: print(res) return [] data = [] for x in res: data.append([x[0], x[1]]) return data
def del_launch(captcha): ''' 功能:在lauch表中删除一条活动记录 接受参数: capthca: 邀请码(唯一) 返回参数: 1 删除成功 0 数据库端错误 ''' sql = "delete from launch where captcha='%s'" % (captcha) flag, e = op_mysql(sql) if flag == False: print(e) return 0 else: return 1
def getTotal(captcha): ''' # 功能:获取记录captcha的投票总数密文 # 接受参数:captcha # 返回参数:text(int, int) / False ''' sql = "select total from launch where captcha = '%s'" % captcha flag, res = op_mysql(sql) if flag == False: print(e) return False data = [] for x in res: for y in x: y = y.split(',') return (int(y[0]), int(y[1]))
def getVoteActivities(usr): ''' # 功能:从launch表中返回usr发起的投票记录 # 接受参数:usr # 返回参数:[(title, captcha)] ''' usr = md5(usr) sql = "select title, captcha from launch where usr = '******'" % usr flag, res = op_mysql(sql) if flag == False: print(False) return [] data = [] for x in res: data.append([x[0], x[1]]) return data
def check_captcha(captcha): ''' # 功能:检查captcha活动是否记录在launch之中 # 接受参数:captcha # 返回参数: 1 launch中不存在该记录 0 数据库端错误 -1 已经创建过相同的活动 ''' sql = "select captcha from launch where captcha = '%s'" % (captcha) flag, res = op_mysql(sql) if flag == False: print(res) return 0 elif len(res): return -1 return 1
def getVoteContent(captcha): ''' # 功能:寻找captcha活动记录对应的title, votelimit # 接受参数:captcha # 返回参数:[] / [title(str), votelimit(int)] ''' sql = "select title, votelimit from launch where captcha = '%s'" % ( captcha) flag, res = op_mysql(sql) if flag == False: print(res) return [] data = [] for x in res: data.append(x[0]) data.append(x[1]) break return data
def checkUsrRecord(captcha, usr): ''' # 功能:在voterecords表中检查(cpatcha, usr)对应记录是否存在 # 接受参数: # 返回参数: 1 记录存在 0 数据库端出错 -1 记录不存在 ''' usr = md5(usr) sql = "select votenum from voterecords where captcha='%s' and usr='******'" % ( captcha, usr) flag, res = op_mysql(sql) if flag == False: print(res) return 0 elif len(res) == 0: return -1 return 1
def insert_votedata(captcha, choice, tag): ''' # 功能:往votedata插入邀请码为captcha的候选数据 # 接受参数: capthca(str): 验证码 choice(str): 候选人 tag(int): 素数标记 # 返回参数: 1 成功插入 0 数据库端错误 -1 tag不是素数 ''' if isPrime(tag) == False: return -1 sql = "insert into votedata(captcha, choice, tag) values('{}', '{}', {})".format( captcha, choice, tag) flag, e = op_mysql(sql) if flag == False: print(e) return 0 else: return 1