def checkDeposit(t_user, t_coin): # 如果 t_user 是 uid # 则先获取相应的 model if type(t_user) == type(1): t_user, err = searchUser(t_user) if err: return err try: t_user = t_user.coins.all()[0] except Exception as e: return 'ForeignKey error' if t_user.Coin >= t_coin: return None return 'not enough coin'
def flow(request): # 检查登录状态 if 'login_id' not in request.session: return failMSG('no login') # 检查方法 if request.method != 'GET': return failMSG('method error') t_uid = request.session['login_id'] t_user, err = searchUser(t_uid) if err: return failMSG(err) try: t_flow = t_user.cf.all().order_by('-TimeStamp') except Exception as e: print(e) return failMSG('db error when get flow') else: response = {} response['flows'] = [] try: for t_f in t_flow: temp = {} temp['title'] = t_f.Title temp['type'] = t_f.Type temp['flow'] = t_f.Flow temp['timestamp'] = t_f.TimeStamp response['flows'].append(temp) except Exception as e: print(e) return failMSG('create flow fail') else: return okMSG(response) return failMSG('fail')
def payCoin(t_user, t_coin): # 如果 t_user 是 uid # 则先获取相应的 model if type(t_user) == type(1): t_user, err = searchUser(t_user) if err: return err # 获取 coin model # 进行支付 try: t_user = t_user.coins.all()[0] except Exception as e: return 'ForeignKey error' if t_user.Coin >= t_coin: t_user.Coin = t_user.Coin - t_coin t_user.save() return None return 'not enough coin'
def controller(request, t_aid): # GET 方法, 不用登录 # 获取问卷 if request.method == 'GET': response, err = getQuestionnaireResponse(t_aid) if err: return failMSG(err) return okMSG(response) # 检查登录状态 if 'login_id' not in request.session: return failMSG('no login') # 从 session 获取 uid t_uid = request.session['login_id'] if type(t_uid) != type(1): t_uid = int(t_uid) # delete 方法, 删除问卷 if request.method == 'DELETE': # 拿到 model t_asg, err = getAsg(t_aid) if err: return failMSG(err) # 检查 creator if t_asg.Creator.UserID == t_uid: t_asg.delete() return okMSG() else: return failMSG('not creator') # POST 方法, 提交问卷答案 if request.method == 'POST': try: rdata = json.loads(request.body) except Exception as e: return failMSG('get json data error') try: t_answers = rdata['answers'] except Exception as e: return failMSG('body parameter error') # 检查是否已经回答 err = alreadyAnswer(t_uid, t_aid) if err: return failMSG(err) # 取 user asg 的 model t_user, err = searchUser(t_uid) if err: return failMSG(err) t_asg, err = getAsg(t_aid) if err: return failMSG(err) # 创建 answer for ans in t_answers: print(ans) print(ans['qid']) t_que, err = getQuestion(ans['qid']) if err: delAns(t_uid, t_aid) return failMSG(err) t_opt, err = getOption(ans['oid']) if err: delAns(t_uid, t_aid) return failMSG(err) try: t_ans = Answer.objects.create(Oid=t_opt, Qid=t_que, Aid=t_asg, Uid=t_user, Value=ans['value'], TimeStamp=ans['timestamp']) except Exception as e: print(e) delAns(t_uid, t_aid) return failMSG('db error when create answer') # 获得t_one.Coin闲钱报酬 try: t_c = t_user.coins.all()[0] t_one = t_asg.qnncoin.all()[0] t_c.Coin += t_one.Coin t_asg.Coins -= t_one.Coin t_c.save() t_asg.save() # flow t_flow = CoinFlow.objects.create(Uid=t_user, Title=t_asg.Title, Type='post questionnaire', TimeStamp=time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), Flow=t_one.Coin) except Exception as e: return failMSG('only get coin fail') return okMSG() return failMSG('wrong method')
def publish(request): # 检查登录状态 if 'login_id' not in request.session: return failMSG('no login') # 检查 method if request.method != 'POST': return failMSG('wrong method') # 从 session 获取 uid t_uid = int(request.session['login_id']) try: rdata = json.loads(request.body) except Exception as e: return failMSG('get json data error') # 从 body 获取参数 try: t_title = rdata['title'] t_description = rdata['description'] t_type = 'questionnaire' t_creator = t_uid # 单份问卷的闲钱奖励 t_coin = int(rdata['coin']) # 问卷份数 t_copy = int(rdata['copy']) t_createTime = rdata['createTime'] t_startTime = rdata['startTime'] t_endTime = rdata['endTime'] t_questions = rdata['questions'] t_options = rdata['options'] except Exception as e: return failMSG('parameter error') # 检查用户和闲钱 t_creator, err = searchUser(t_creator) if err: return failMSG(err) err = checkDeposit(t_creator, t_coin * t_copy) if err: return failMSG(err) # 创建 assignment 并 扣除相应闲钱 try: t_asg = Assignment.objects.create(Title=t_title, Description=t_description, Type=t_type, Creator=t_creator, Coins=t_coin * t_copy, CreateTime=t_createTime, StartTime=t_startTime, EndTime=t_endTime) ctor_coin = t_creator.coins.all()[0] ctor_coin.Coin -= t_coin ctor_coin.save() # flow t_flow = CoinFlow.objects.create(Uid=t_creator, Title=t_title, Type='create questionnaire', TimeStamp=time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()), Flow=-t_coin * t_copy) except Exception as e: return failMSG('create asg fail') # 创建qnn coin try: t_qc = QnnCoin.objects.create(Aid=t_asg, Coin=t_coin, Copy=t_copy) except Exception as e: return failMSG('create qc fail') # 准备创建 question 和 option # q_length 有多少个 question # has_option 检查每个 question 是否有 option # opt_index 下一个创建的 option 序号 q_length = len(t_questions) has_option = False opt_index = 0 for index in range(q_length): # 创建 question try: temp = t_questions[index] t_que = Questions.objects.create(Aid=t_asg, Title=temp['title'], Type=temp['type']) except Exception as e: print(temp) print(e) return failMSG('create question fail') # 创建 option temp = t_options[opt_index] while temp['questionIndex'] == index: has_option = True try: t_opt = Options.objects.create(Qid=t_que, Aid=t_asg, Value=temp['value']) except Exception as e: return failMSG('create option fail') opt_index += 1 if opt_index < len(t_options): temp = t_options[opt_index] else: break # 检查是否每个 question 都有 option if has_option == False: err = deleteAsgByAid(t_asg) if err: return failMSG('some questions miss option and ' + err) return failMSG('some questions miss option') has_option = False return okMSG({'aid': t_asg.Aid})
def getMyAsg(request, t_class): # 检查 method if request.method != 'GET': return failMSG('wrong method') # 检查登录状态 if 'login_id' not in request.session: return failMSG('no login') # 从 session 获取 uid t_uid = int(request.session['login_id']) # 检查 get 参数 if t_class != 'all' and t_class != 'questionnaire' and t_class != 'qa' and t_class != 'answer': return failMSG('parameter error') if t_class == 'all': response = {} response['assignments'] = [] t_user, err = searchUser(t_uid) if err: return failMSG(err) # 数据库操作 try: t_asg = t_user.asg.all().order_by('-CreateTime') except Exception as e: return failMSG('db error when get my asg') else: for asg in t_asg: temp = {} temp['title'] = asg.Title temp['description'] = asg.Description temp['type'] = asg.Type temp['aid'] = asg.Aid temp['creator'] = asg.Creator.Nickname temp['coin'] = asg.Coins temp['createTime'] = asg.CreateTime temp['startTime'] = asg.StartTime temp['endTime'] = asg.EndTime temp['answerCount'] = 0 temp['bestCount'] = 0 temp['unit'] = 0 temp['copy'] = 0 if asg.Type == 'qa': temp['answerCount'] = asg.qas.all().count() temp['bestCount'] = asg.qab.all().count() else: temp['unit'] = asg.qnncoin.all()[0].Coin temp['copy'] = asg.qnncoin.all()[0].Copy response['assignments'].append(temp) response['asgCount'] = t_asg.count() return okMSG(response) return failMSG('fail') if t_class == 'questionnaire' or t_class == 'qa': response = {} response['assignments'] = [] t_user, err = searchUser(t_uid) if err: return failMSG(err) # 数据库操作 try: t_asg = t_user.asg.filter(Type=t_class).order_by('-CreateTime') except Exception as e: return failMSG('db error when get my asg') else: for asg in t_asg: temp = {} temp['title'] = asg.Title temp['description'] = asg.Description temp['type'] = asg.Type temp['aid'] = asg.Aid temp['creator'] = asg.Creator.Nickname temp['coin'] = asg.Coins temp['createTime'] = asg.CreateTime temp['startTime'] = asg.StartTime temp['endTime'] = asg.EndTime temp['answerCount'] = 0 temp['bestCount'] = 0 temp['unit'] = 0 temp['copy'] = 0 if asg.Type == 'qa': temp['answerCount'] = asg.qas.all().count() temp['bestCount'] = asg.qab.all().count() else: temp['unit'] = asg.qnncoin.all()[0].Coin temp['copy'] = asg.qnncoin.all()[0].Copy response['assignments'].append(temp) response['asgCount'] = t_asg.count() return okMSG(response) return failMSG('fail') if t_class == 'answer': response = {} response['assignments'] = [] t_user, err = searchUser(t_uid) if err: return failMSG(err) t_ans = t_user.qas.all().order_by('-TimeStamp') try: t_asg = set() for x in t_ans: t_asg.add(x.Aid) for asg in t_asg: temp = {} temp['title'] = asg.Title temp['description'] = asg.Description temp['type'] = asg.Type temp['aid'] = asg.Aid temp['creator'] = asg.Creator.Nickname temp['coin'] = asg.Coins temp['createTime'] = asg.CreateTime temp['startTime'] = asg.StartTime temp['endTime'] = asg.EndTime temp['answerCount'] = 0 temp['bestCount'] = 0 temp['unit'] = 0 temp['copy'] = 0 if asg.Type == 'qa': temp['answerCount'] = asg.qas.all().count() temp['bestCount'] = asg.qab.all().count() else: temp['unit'] = asg.qnncoin.all()[0].Coin temp['copy'] = asg.qnncoin.all()[0].Copy response['assignments'].append(temp) # response['asgCount'] = t_asg.count() return okMSG(response) except Exception as e: print(e) return failMSG('my answer fail') return failMSG('fail')