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 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 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 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 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 onSave(self): if self.Check() == True: title = self.titleInput.text() votelimit = self.votelimitInput.text() votelimit = int(votelimit) primes = create_primes(self.rownum) self.tabledata = self.getTableData() data = [] for i in range(self.rownum): data.append([self.tabledata[i][0], primes[i]]) reply = QMessageBox.question(self, '询问', '确认保存?', QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) if reply == QMessageBox.Yes: s = self.usr + title + str(votelimit) for i in range(len(data)): s = s + data[i][0] + '-' + str(data[i][1]) captcha = md5(s) pubkey = openKey(self, 0) if pubkey == False: return C = pubkey.encrypt_int(1) C = str(C[0]) + ',' + str(C[1]) flag = insert_launch(self.usr, title, captcha, votelimit, C) if flag == 0: QMessageBox.information(self, 'sorry', '后台数据库出了点问题', QMessageBox.Yes) elif flag == -1: QMessageBox.warning(self, 'warning', '您已经保存过该活动', QMessageBox.Yes) else: for x in data: flag = insert_votedata(captcha, x[0], x[1]) if flag == 0: del_launch(captcha) QMessageBox.information(self, 'sorry', '后台数据库出了点问题', QMessageBox.Yes) return elif flag == -1: QMessageBox.warning(self, 'warning', '生成标记不是素数', QMessageBox.Yes) self.captchalbl.setText('投票邀请码: ' + captcha) QMessageBox.information(self, '恭喜', '保存成功', QMessageBox.Yes)