def post(self, action): role = self.get_secure_cookie("role") if role != b"admin": dump_err(self, "无权访问此接口") if action == "list": cursor = con.cursor() cursor.execute("select * from {}".format(self.table_name)) print("list:", cursor.statement) records = [] for data in cursor: data = list(data) data[3] = "软件学院" data_dict = {} for ind in range(len(self.col_names)): data_dict[self.col_names[ind]] = str(data[ind]) records.append(data_dict) cursor.close() print("list records", records) result = {} result["Result"] = "OK" result["Records"] = records print(json.dumps(result)) self.write(json.dumps(result)) return
def post(self, action): if action == "tuition": studentid = self.get_argument("studentid") amount = self.get_argument("amount") message = self.get_argument("message") amount = Decimal(amount) cursor = util.con.cursor() sql = "select frozen from finance where studentid = %s" cursor.execute(sql, (studentid,)) print(cursor.statement) frozen = list(cursor)[0][0] print("{} 的冻结金额是 {}".format(studentid, frozen)) if frozen < amount: dump_err(self, "账户冻结金额 {} 小于扣费金额 {}".format(frozen, amount)) cursor.close() return """扣钱""" frozen -= amount sql = "update finance set frozen = %s where studentid = %s" cursor.execute(sql, (frozen, studentid)) print(cursor.statement) """添加交易记录""" sql = "insert into transaction (studentid, amount, comment) values (%s, %s, %s)" cursor.execute(sql, (studentid, amount, message)) print(cursor.statement) util.con.commit() cursor.close() self.write(json.dumps({"status": "success", "message": "学费扣费成功"}, ensure_ascii=False)) return if action == "freeze": studentid = self.get_argument("studentid") amount = Decimal(self.get_argument("amount")) """检查余额""" cursor = util.con.cursor() sql = "select balance,frozen from finance where studentid = %s" cursor.execute(sql, (studentid,)) print(cursor.statement) balance, frozen = list(cursor)[0] print("用户的余额和冻结金额是", balance, frozen) if balance < amount: dump_err(self, "余额不足,请充值后重试冻结") cursor.close() return balance -= amount frozen += amount sql = "update finance set balance = %s, frozen = %s where studentid = %s" cursor.execute(sql, (balance, frozen, studentid)) util.con.commit() cursor.close() self.write(json.dumps({"status": "success", "message": "冻结余额成功"}, ensure_ascii=False)) return if action == "unfreeze": studentid = self.get_argument("studentid") amount = Decimal(self.get_argument("amount")) """检查冻结金额""" cursor = util.con.cursor() sql = "select balance,frozen from finance where studentid = %s" cursor.execute(sql, (studentid,)) print(cursor.statement) balance, frozen = list(cursor)[0] print("用户的余额和冻结金额是", balance, frozen) if frozen < amount: dump_err(self, "冻结金额不足,不能解冻这么多") cursor.close() return frozen -= amount balance += amount sql = "update finance set balance = %s, frozen = %s where studentid = %s" cursor.execute(sql, (balance, frozen, studentid)) util.con.commit() cursor.close() self.write(json.dumps({"status": "success", "message": "解冻余额成功"}, ensure_ascii=False)) return
async def post(self, action): if action == "get_class": # 1. 获取所有的课程 classes = self.get_classes() sid = self.get_argument("sid") print("所有的课程为", classes) # 2. 对class按照学生设定的顺序进行排序 cursor = util.con.cursor() sql = "select clsids from student where studentid = %s " cursor.execute(sql, (sid,)) clsids = list(cursor)[0][0] if clsids is not None: # 如果是none是学生没设过排序 # 检查排序的clsid和当前计划的clsid是否完全相同 clsids = json.loads(clsids) curr_plan_clas = set([cls[0] for cls in classes]) print(curr_plan_clas, clsids, set(clsids)) if curr_plan_clas != set(clsids): # 有出入,这个排序无效,清空 sql = "update student set clsids = null where studentid = %s" cursor.execute(sql, (sid,)) util.con.commit() else: # 合法,按照排序修改课程顺序 sorted_class = [] for clsid in clsids: for clas in classes: if clsid == clas[0]: sorted_class.append(clas) break print("sorted_class", sorted_class) classes = sorted_class # 3. 补全class中的老师,课程名称,是否选择信息 for ind in range(len(classes)): classes[ind] = list(classes[ind]) print("curr clas info", classes[ind]) sql = "select name from course where courseid = %s" cursor.execute(sql, (classes[ind][1],)) print(cursor.statement) course_name = list(cursor)[0][0] classes[ind].append(str(course_name)) sql = "select name from teacher where teacherid = %s" cursor.execute(sql, (classes[ind][3],)) print(cursor.statement) teacher_name = list(cursor)[0][0] classes[ind].append(teacher_name) classes[ind].append(util.map_class_time(classes[ind][5])) sql = "select is_1st from choice_temp where studentid = %s and classid = %s" cursor.execute(sql, (sid, classes[ind][0])) print("+_+_+", cursor.statement) is_1st = list(cursor) print("is_1st", is_1st) if len(is_1st) == 0: classes[ind].append(-1) else: classes[ind].append(is_1st[0][0]) print("补全后课程信息", classes[ind]) # 4. 获取选课计划信息 sql = "select * from plan where planid = %s" cursor.execute(sql, (self.get_argument("planid"),)) planinfo = list(list(cursor)[0]) planinfo[5] = planinfo[5].isoformat().replace("T", " ") planinfo[6] = planinfo[6].isoformat().replace("T", " ") print(planinfo) cursor.close() self.write( json.dumps( {"status": "success", "classes": classes, "planinfo": planinfo,}, ensure_ascii=False, ) ) return if action == "set_order": # 设置课程展示顺序, clsids = json.loads(self.get_argument("classids")) sid = self.get_argument("sid") clsids = [int(id) for id in clsids] clsids = json.dumps(clsids) sql = "update student set clsids = %s where studentid = %s " cursor = util.con.cursor() cursor.execute(sql, (clsids, sid)) cursor.close() util.con.commit() planid = self.get_argument("pid") studentid = self.get_argument("sid") order_2nd = self.get_argument("order_2nd") order_2nd = util.purge_list(order_2nd.split(",")) order_2nd = [int(x) for x in order_2nd] unified = [] for id in order_2nd: if id not in unified: unified.append(id) order_2nd = unified order_2nd = [int(x) for x in order_2nd] print("备选的顺序是", order_2nd) cursor = util.con.cursor() sql = "select classid from choice_temp where planid = %s and studentid = %s and is_1st = 0" cursor.execute(sql, (planid, studentid)) sec_ids = list(cursor) sec_ids = [int(x[0]) for x in sec_ids] print("数据库中的备选id", sec_ids) if set(sec_ids) != set(order_2nd): dump_err(self, "保存过程中备选顺序和数据库中备选课程不匹配") cursor.close() return sql = "update choice_temp set priority = %s where classid = %s and planid = %s and studentid = %s" for ind, id in enumerate(sec_ids): cursor.execute(sql, (ind + 5, id, planid, studentid)) print(cursor.statement) util.con.commit() cursor.close() self.write(json.dumps({"status": "success", "message": "选课计划保存成功"})) # 设置备选课程顺序 return if action == "unchoose": planid = self.get_argument("pid") studentid = self.get_argument("sid") classid = self.get_argument("cid") courseid = self.get_argument("courseid") # 1. 获取要退的课的id print(studentid, "正在尝试退", classid) cursor = util.con.cursor() sql = "select choiceid, is_1st from choice_temp where studentid = %s and classid = %s and planid = %s" cursor.execute(sql, (studentid, classid, planid)) print(cursor.statement) choiceid = list(cursor) if len(choiceid) == 0: dump_err(self, "无法退选自己没选的课") cursor.close() return choiceid, is_1st = choiceid[0] print("这个选课记录的id是", choiceid) # 2. 如果是首选要求已经没有备选 if is_1st == 1: print("退的是首选的课程") sql = "select count(choiceid) from choice_temp where studentid = %s and planid = %s and is_1st = 0" cursor.execute(sql, (studentid, planid)) class_count = list(cursor)[0][0] print("备选总数", class_count) if class_count != 0: dump_err(self, "有备选的课程时不能退选首选") cursor.close() return # 3. 解冻账户冻结金额 # # 3.1 看看这门课多少钱 # sql = "select price from course where courseid = %s" # cursor.execute(sql, (courseid,)) # print(cursor.statement) # price = list(cursor)[0][0] # print("课程费用", price) # # # 3.2 解冻金额 # url = util.base_url + "/finance/unfreeze" # body = urllib.parse.urlencode({"studentid": studentid, "amount": price}) # req = tornado.httpclient.HTTPRequest(url, "POST", body=body) # res = await tornado.httpclient.AsyncHTTPClient().fetch(req) # print("freeze", res.body) # 4. 删除课程 sql = "delete from choice_temp where choiceid = %s" cursor.execute(sql, (choiceid,)) util.con.commit() self.write(json.dumps({"status": "success"})) if action == "choose": # 只有学生能访问 util.check_role(self, "student") # 实际进行选课,谁选什么课程 sid = self.get_argument("sid") cid = self.get_argument("cid") is_1st = int(self.get_argument("is_1st")) planid = self.get_argument("pid") courseid = self.get_argument("courseid") print("学生 {} 尝试选 {} 作为 {}".format(sid, cid, is_1st)) """ 所有的这些检查都要分开做,能切换顺序。提升性能的时候把便宜刷的多的放前面 1. 判断选课时间是否合法 2. 检查这个教学班是不是已经选满 3. 检查这个学生这门课是不是选了其他班级 4. 检查是不是选了超过4门首选 5. 检查是不是选了超过2门备选 6. 首选满了才备选 7. 检查是不是选了先修课 扣钱 添加记录 """ """1. 判断选课时间是否合法""" cursor = util.con.cursor() sql = "select start_time, end_time from plan where planid = %s" cursor.execute(sql, (planid,)) time_range = list(cursor) if len(time_range) != 1: print("选课计划不合法", time_range) dump_err(self, "提供的选课计划不合法") cursor.close() return time_range = list(time_range[0]) time_range = [x.isoformat().replace("T", " ") for x in time_range] print("选课计划时间", time_range) curr_time = util.curr_time() print("当前时间", curr_time) if curr_time <= time_range[0]: dump_err(self, "还未到选课开始时间") cursor.close() return if curr_time > time_range[1]: dump_err(self, "选课已经结束") cursor.close() return """2. 检查首选选这个教学班的是不是已经满了""" sql = "select count(choiceid) from choice_temp where is_1st = %s and classid = %s" cursor.execute(sql, (is_1st, cid)) count = list(cursor)[0][0] print("当前首选这门课的人数", count) if count >= 10: dump_err(self, "当前选课人数已经超过10个") cursor.close() return """3. 检查这个学生这门课是不是已经选了这个或者其他班级 """ sql = "select classid from class where planid = %s and courseid = %s" cursor.execute(sql, (planid, courseid)) classes = list(cursor) classes = [x[0] for x in classes] print("这门课的教学班id共有", classes) sql = "select count(choiceid) from choice_temp where classid in (" sql += "%s, " * len(classes) sql = sql[:-2] + ") and planid = %s and studentid = %s" classes.append(planid) classes.append(sid) print(sql) cursor.execute(sql, classes) class_count = list(cursor)[0][0] print("当前学生选了上述班级 {} 次".format(class_count)) if class_count != 0: dump_err(self, "已经选择了这个教学班或者这门课的其他教学班") cursor.close() return """4. 如果首选,检查是不是选了超过4门首选""" sql = "select count(choiceid) from choice_temp where studentid = %s and is_1st = 1" cursor.execute(sql, (sid,)) pri_class_count = list(cursor)[0][0] print("学生当前共选了 {} 门首选".format(pri_class_count)) if is_1st == 1: if pri_class_count >= 4: dump_err(self, "不能选超过4门首选") cursor.close() return """5. 如果备选, 检查是否选了超过2门备选 """ if is_1st == 0: print("choosing 2nd, now chosen", pri_class_count) if pri_class_count != 4: dump_err(self, "首选没选满4门课不能选择备选") cursor.close() return sql = "select count(choiceid) from choice_temp where studentid = %s and is_1st = 0" cursor.execute(sql, (sid,)) class_count = list(cursor)[0][0] print("学生当前共选了 {} 门备选") if class_count >= 2: dump_err(self, "不能选超过2门备选") cursor.close() return """6. 检查是否选了先修课 """ """7. 尝试冻结金额""" # sql = "select price from course where courseid = %s" # cursor.execute(sql, (courseid,)) # print(cursor.statement) # price = list(cursor)[0][0] # print("课程费用", price) # # url = util.base_url + "/finance/freeze" # body = urllib.parse.urlencode({"studentid": sid, "amount": price}) # req = tornado.httpclient.HTTPRequest(url, "POST", body=body) # res = await tornado.httpclient.AsyncHTTPClient().fetch(req) # print("freeze", res.body) """ 在choicetemp表中添加选课记录 """ print("is 1st", is_1st) if is_1st == 1: priority = 1 else: priority = 0 sql = "INSERT INTO `choice_temp`(`studentid`,`classid`,`planid`,`is_1st`, `priority`)VALUES(%s, %s, %s, %s, %s)" cursor.execute(sql, (sid, cid, planid, is_1st, priority)) cursor.close() util.con.commit() self.write(json.dumps({"status": "success", "message": "选课成功"}))
def post(self, action): """删除选课计划""" if action == "delete": planid = self.get_argument("planid") # 1. 如果不存在,不能删除 sql = "select public from plan where planid = %s" cursor = util.con.cursor() cursor.execute(sql, (planid,)) print(cursor.statement) ispublic = list(cursor) print("ispublic", ispublic) if len(ispublic) == 0: dump_err(self, "不能删除不存在的选课计划") cursor.close() return # 2. 如果已公开,不能删除 ispublic = ispublic[0][0] if ispublic == 1: dump_err(self, "不能删除已公开的选课计划") cursor.close() return # 3. 删除教学班 sql = "delete from class where planid = %s" cursor.execute(sql, (planid,)) util.con.commit() # 4. 删除计划记录 sql = "delete from plan where planid = %s" cursor.execute(sql, (planid,)) print("delete plan: ", cursor.statement) util.con.commit() cursor.close() self.write(json.dumps({"status": "success", "message": "选课计划和计划中的教学班删除成功"})) """公开选课计划""" if action == "publish": planid = self.get_argument("planid") cursor = util.con.cursor() # 1. 删除没老师的课程 sql = "select classid from class where planid = %s and teacherid is null " cursor.execute(sql, (planid,)) classes_without_teacher = list(cursor) classes_without_teacher = [x[0] for x in classes_without_teacher] print("没老师的课", classes_without_teacher) if len(classes_without_teacher) != 0: sql = "delete from class where classid in (" + "%s, " * len(classes_without_teacher) sql = sql[:-2] sql = sql + ")" cursor.execute(sql, classes_without_teacher) print(cursor.statement) # 2. 选课计划设置为公开 sql = "update plan set public = 1 where planid = %s" cursor.execute(sql, (planid,)) cursor.close() util.con.commit() msg = "选课计划公开成功" if len(classes_without_teacher) != 0: msg += ",删除了{}".format(classes_without_teacher) self.write(json.dumps({"status": "success", "message": msg,}, ensure_ascii=False,)) return """编辑选课计划""" if action == "save": planid = self.get_argument("planid") curr_time = util.curr_time() start_daytime = ( self.get_argument("start_day") + " " + self.get_argument("start_time") + ":00" ) if start_daytime <= curr_time: dump_err(self, "选课计划开始时间需要晚于当前时间") return if planid == "None": planid = self.insert_plan() else: self.update_plan(planid) # PLAN: 这里判断如果有选课计划(是修改),那么当前选课计划必须还没有发布 self.insert_class(planid) self.update_class(planid) self.del_class(planid) self.write( json.dumps( {"status": "success", "message": "成功添加选课计划", "planid": planid}, ensure_ascii=False, ) )
async def charge(self): cursor = con.cursor() # 尝试扣费finance chargequery = "select * from charge_queue" cursor.execute(chargequery) chargelist = list(cursor) index = 0 # print("hhhhhheheheheheh") while len(chargelist) > 0 and index < len(chargelist): # # 获取该学生当前余额 # getbalance = ("select balance from finance where studentid = {}").format(chargelist[index][1]) # cursor.execute(getbalance) # balance = list(cursor) # # 尝试插入finance # insfinance = ("update finance set balance={},frozen=0)").format(balance-chargelist[index][2]) # cursor.execute(insfinance) # 尝试插入finance url = util.base_url + "/finance/tuition" data = { "studentid": chargelist[index][1], "amount": chargelist[index][2], "message": chargelist[index][3], } body = urllib.parse.urlencode(data) try: # 发起一个post请求去请财务的接口 print("发起一个post请求去请财务的接口") req = tornado.httpclient.HTTPRequest(url, "POST", body=body) res = await tornado.httpclient.AsyncHTTPClient().fetch(req) res = json.loads(res.body) # print("res: ",res) except: # 网络链接错误 dump_err(self, "网络连接错误") else: if res["status"] != "success": # 成功 return delcharge = ("delete from charge_queue where chargeid = {}").format( chargelist[index][0] ) cursor.execute(delcharge) index += -1 print(cursor.statement) # 判断扣费是否成功,如果成功,则在charge_queue删除记录 index += 1 # 更新一下chargelist chargequery = "select * from charge_queue" cursor.execute(chargequery) chargelist = list(cursor) print("index:", index, " new chargelist: ", chargelist) # 这里未完成需要有个重置index # url = util.base_url + "/finance/tuition" # # 获取异步客户端,fetch(url, callback回调) # clicent = tornado.httpclient.AsyncHTTPClient() # print('异步请求开始') # clicent.fetch(url, callback="POST") # self.write('请求成功') con.commit() cursor.close()
def end_registration(self): planid = self.get_argument("planid") # 需要删除的课程列表: cls_to_del = set() choicelist = [] registration_temp = {} # 选课情况dict # 检查选课计划是不是已经结束 cursor = con.cursor() sql = "select end_time from plan where planid = %s" cursor.execute(sql, (planid,)) end_time = list(cursor)[0][0].isoformat().replace("T", " ") print("选课计划结束时间", end_time) if util.curr_time() < end_time: dump_err(self, "未到选课结束时间不能结束选课") return # 从choice_temp中选出结束的planid里所有的选课记录 choicequery = "select * from choice_temp where planid = " + planid cursor.execute(choicequery) choicelist = list(cursor) print("1 ", choicelist) if len(choicelist) > 0: # 对所有选课记录进行投票统计,如果一个班级首选加备选人数都不超过3个人,这个教学班删除。写入一个set cls_to_del clsidsquery = "select distinct classid from choice_temp where planid = " + str(planid) cursor.execute(clsidsquery) clsids = list(cursor) print("2 ", clsids) remainnum_per_class = {} # 初始化remainnum_perclass for i in range(0, len(clsids)): remainnum_per_class[clsids[i][0]] = max_student_per_class print("3 ", remainnum_per_class) for eachchoice in choicelist: theclassid = eachchoice[2] if remainnum_per_class[theclassid] > 0: remainnum_per_class[theclassid] += -1 print("4 ", remainnum_per_class) for key in remainnum_per_class.keys(): # key=classid if remainnum_per_class[key] > max_student_per_class - 3: cls_to_del.add(key) print("5 ", cls_to_del) # choicelist按照cls_to_del删除记录 # 第一轮筛选之后的choicelist: index = 0 while index < len(choicelist) and index >= 0: # print(index," " ,choicelist) if choicelist[index][2] in cls_to_del: # print("hhh",choicelist[index][2]) choicelist.remove(choicelist[index]) index -= 1 index += 1 print("6 ", choicelist) if len(choicelist) > 0: # 按照首选给每个人选课,生成一个选课情况dict。 # registration_temp = {} # 选课情况dict # 初始化选课情况字典 for eachchoice in choicelist: registration_temp[eachchoice[1]] = [] print("7 ", registration_temp) # 重新初始化remainnum_per_class (重复) for key in remainnum_per_class.keys(): remainnum_per_class[key] = max_student_per_class # 按照首选课选到选课情况字典registration_temp里 for eachchoice in choicelist: if eachchoice[-1] == 1: # 是首选课 this_stu_id = eachchoice[1] this_cls_id = eachchoice[2] registration_temp[this_stu_id].append(this_cls_id) remainnum_per_class[this_cls_id] += -1 print("8 ", registration_temp) print("9 ", remainnum_per_class) # #在dict中统计所有班级的人数,如果不够3个人这个班级删除,dict中的记录删除 # for key in remainnum_per_class.keys():# key=classid # if remainnum_per_class[key] > max_student_per_class-3: # cls_to_del.add(key) # 增加要删除的课id,更新了cls_to_del!!!!!!!!!!!!!!!! # print("10 ",cls_to_del) # # # 在registration_temp里学生的课程列表中,删掉更新后的cls_to_del里面的课id # for key in registration_temp.keys():# key=studentid # clsids = registration_temp[key] # 每个学生选的一堆课id 的list # index = 0 # while index <len(clsids) and index >= 0 :# 每个学生选的一堆课id中的一个 # if clsids[index] in cls_to_del: # print("clsids[index]:",clsids[index]) # clsids.remove(clsids[index]) # index-=1 # index+=1 # # # 上一步操作后registration_temp里学生的课程列表可能是空的了(学生的至多四个首选课全都没选上),需要删除他 # for key in list(registration_temp.keys()):# key=studentid # if len(registration_temp[key]) == 0: # 这个学生没有选上课,一堆课id删减到为空了(太惨了) # registration_temp.pop(key) # print("11 ",registration_temp) # # # # choicelist按照cls_to_del删除记录 # # 第二轮筛选之后的choicelist: # while index < len(choicelist) and index >= 0: # # print(index," " ,choicelist) # if choicelist[index][2] in cls_to_del: # # print("hhh",choicelist[index][2]) # choicelist.remove(choicelist[index]) # index-=1 # index+=1 # print("12 ",choicelist) # 用第一备选给学生补齐选课 if len(choicelist) > 0: # 找dict中不满4门课的学生。给他选上第一备选。 for key in list(registration_temp.keys()): # key = studentid if len(registration_temp[key]) < 4: # 找到dict中不满4门课的学生id: key for eachchoice in choicelist: if ( eachchoice[1] == key and eachchoice[-3] == 5 ): # 找到这个学生的第一备选课id:eachchoice[2] registration_temp[key].append(eachchoice[2]) # 给他选上第一备选 remainnum_per_class[eachchoice[2]] += -1 print("_+_+_+_+_+_+", eachchoice[2]) print("$$$$$$$$", registration_temp, "$$$$$$$$$$", remainnum_per_class) # 找dict中还是不满4门课的学生。给他选上第二备选。 for key in list(registration_temp.keys()): # key = studentid if len(registration_temp[key]) < 4: # 找到dict中不满4门课的学生id: key for eachchoice in choicelist: if ( eachchoice[1] == key and eachchoice[-3] == 6 ): # 找到这个学生的第二备选课id:eachchoice[2] registration_temp[key].append(eachchoice[2]) # 给他选上第二备选 remainnum_per_class[eachchoice[2]] += -1 # 在dict中统计所有班级的人数,如果不够3个人这个班级删除,dict中的记录删除 for key in remainnum_per_class.keys(): # key=classid if remainnum_per_class[key] > max_student_per_class - 3: cls_to_del.add(key) # 增加要删除的课id,更新了cls_to_del!!!!!!!!!!!!!!!! print("10 ", cls_to_del) # 在registration_temp里学生的课程列表中,删掉更新后的cls_to_del里面的课id for key in registration_temp.keys(): # key=studentid clsids = registration_temp[key] # 每个学生选的一堆课id 的list index = 0 while index < len(clsids) and index >= 0: # 每个学生选的一堆课id中的一个 if clsids[index] in cls_to_del: print("clsids[index]:", clsids[index]) clsids.remove(clsids[index]) index -= 1 index += 1 # 上一步操作后registration_temp里学生的课程列表可能是空的了(学生的至多四个首选课全都没选上),需要删除他 for key in list(registration_temp.keys()): # key=studentid if len(registration_temp[key]) == 0: # 这个学生没有选上课,一堆课id删减到为空了(太惨了) registration_temp.pop(key) print("11 ", registration_temp) # choicelist按照cls_to_del删除记录 # 第三轮筛选之后的choicelist: while index < len(choicelist) and index >= 0: # print(index," " ,choicelist) if choicelist[index][2] in cls_to_del: # print("hhh",choicelist[index][2]) choicelist.remove(choicelist[index]) index -= 1 index += 1 print("12 ", choicelist) # # 用第二备选给学生补齐选课(重复) # if len(choicelist) > 0: # # 找dict中不满4门课的学生。给他选上第二备选。 # for key in list(registration_temp.keys()): # key = studentid # if len(registration_temp[key]) < 4: # 找到dict中不满4门课的学生id: key # for eachchoice in choicelist: # if ( # eachchoice[1] == key and eachchoice[-3] == 6 # ): # 找到这个学生的第二备选课id:eachchoice[2] # registration_temp[key].append(eachchoice[2]) # 给他选上第二备选 # remainnum_per_class[eachchoice[2]] += -1 # # # 在dict中统计所有班级的人数,如果不够3个人这个班级删除,dict中的记录删除 # for key in remainnum_per_class.keys(): # key=classid # if remainnum_per_class[key] > max_student_per_class - 3: # cls_to_del.add(key) # 增加要删除的课id,更新了cls_to_del!!!!!!!!!!!!!!!! # print("13 ", cls_to_del) # # # 在registration_temp里学生的课程列表中,删掉更新后的cls_to_del里面的课id # for key in registration_temp.keys(): # key=studentid # clsids = registration_temp[key] # 每个学生选的一堆课id 的list # index = 0 # while index < len(clsids) and index >= 0: # 每个学生选的一堆课id中的一个 # if clsids[index] in cls_to_del: # print("clsids[index]:", clsids[index]) # clsids.remove(clsids[index]) # index -= 1 # index += 1 # # # 上一步操作后registration_temp里学生的课程列表可能是空的了(学生的至多四个首选课全都没选上),需要删除他 # for key in list(registration_temp.keys()): # key=studentid # if len(registration_temp[key]) == 0: # 这个学生没有选上课,一堆课id删减到为空了(太惨了) # registration_temp.pop(key) # print("14 ", registration_temp) # # # choicelist按照cls_to_del删除记录 # # 第四轮筛选之后的choicelist: # while index < len(choicelist) and index >= 0: # # print(index," " ,choicelist) # if choicelist[index][2] in cls_to_del: # # print("hhh",choicelist[index][2]) # choicelist.remove(choicelist[index]) # index -= 1 # index += 1 # print("15 ", choicelist) # 给主任显示要删除的教学班:需要render现在的cls_to_del print("set", cls_to_del) cursor.close() self.write( json.dumps( { "status": "success", "planid": planid, "cls_to_del": list(cls_to_del), "choicelist": choicelist, "registration_temp": registration_temp, }, ensure_ascii=False, ), )
def post(self, action): role = self.get_secure_cookie("role") if role != b"admin": dump_err(self, "无权访问此接口") if action == "list": cursor = con.cursor() cursor.execute("select * from {}".format(self.table_name)) print("list:", cursor.statement) records = [] for data in cursor: data_dict = {} for ind in range(len(self.col_names)): data_dict[self.col_names[ind]] = str(data[ind]) records.append(data_dict) cursor.close() print("list records", records) result = {} result["Result"] = "OK" result["Records"] = records print(json.dumps(result)) self.write(json.dumps(result)) return if action == "create": data = [self.get_argument(col_name) for col_name in self.col_names[1:]] print("create paramaters", data) cursor = con.cursor() names = "" for name in self.col_names[1:-1]: names += name + "," names += self.col_names[-1] query = ( "insert into {} (" + names + ") values (" + "%s," * (len(self.col_names) - 2) + "%s" + ")" ).format(self.table_name) cursor.execute(query, data) print(cursor.statement) new_id = cursor.lastrowid """ 添加一个默认密码 """ sql = "insert into passwd values (%s, %s, %s, %s)" if self.table_name == "teacher": username = util.get_pinyin(data[0]) + data[9].split("-")[0][-2:] else: username = util.get_pinyin(data[0]) + data[6].split("-")[0][-2:] cursor.execute( sql, (username, util.get_md5(util.default_password), self.user_type, new_id), ) print("insert passwd:", cursor.statement) con.commit() """ 如果学生,添加财务记录 """ if self.user_type == 2: sql = "insert into finance values (%s, %s, %s)" cursor.execute(sql, (new_id, 10000, 0)) con.commit() print(list(cursor)) cursor.close() result = {} result["Result"] = "OK" data.insert(0, new_id) temp = {} for ind in range(len(self.col_names)): temp[self.col_names[ind]] = data[ind] result["Record"] = temp print(json.dumps(result)) self.write(json.dumps(result)) if action == "update": data = [self.get_argument(col_name) for col_name in self.col_names] print("update paramaters", data) cursor = con.cursor() query = "update {} set ".format(self.table_name) for ind, col in enumerate(self.col_names[1:-1]): query += col + "= %s," query += self.col_names[-1] + "= %s" query += " where " + self.col_names[0] + "= '{}'".format(data[0]) cursor.execute(query, data[1:]) print("update: ", cursor.statement) con.commit() cursor.close() result = {} result["Result"] = "OK" self.write(json.dumps(result)) if action == "delete": key_value = self.get_argument(self.col_names[0]) print("delete param", key_value) cursor = con.cursor() # 从密码表中删除记录 sql = "delete from passwd where userid = %s and user_type = %s" cursor.execute(sql, (key_value, self.user_type)) print(cursor.statement) # 从财务表中删除记录 if self.user_type == 2: sql = "delete from finance where studentid = %s" cursor.execute(sql, (key_value,)) print(cursor.statement) # 删除学生记录 query = ("delete from {} where " + self.col_names[0] + " = %s").format(self.table_name) cursor.execute(query, [key_value]) print(cursor.statement) con.commit() cursor.close() result = {} result["Result"] = "OK" self.write(json.dumps(result))