Esempio n. 1
0
 def get(self, request):
     self.userAuth()
     form = QuestionForm(self.GET())
     if not form.is_valid():
         return RestResponse.frontFail("参数错误!", form.errorsDict())
     data = form.cleaned_data
     data['questionNum'] = 10 if not data['questionNum'] else data[
         'questionNum']
     dataList = Question.objects.filter(type=data['type']).values(
         "questionId").order_by('?')[:data['questionNum']]
     questionIdList = list(map(lambda x: x['questionId'], dataList))
     questionList = Data.getDataList(Question, questionIdList)
     if data['type'] in [2, 3]:  # 单选题和多选题
         optionIdList = Option.objects.filter(
             questionId__in=questionIdList).values('optionId')
         optionIdList = list(map(lambda x: x['optionId'], optionIdList))
         optionList = Data.getDataList(Option, optionIdList)
         questionOptionDict = {}
         for option in optionList:
             if option['questionId'] not in questionOptionDict:
                 questionOptionDict[option['questionId']] = []
             questionOptionDict[option['questionId']].append(option)
         for question in questionList:
             question['optionList'] = questionOptionDict[
                 question['questionId']]
     return RestResponse.success("获取题目成功!", {"questionList": questionList})
Esempio n. 2
0
 def get(self, request):
     self.userAuth()
     form = UserRankForm(self.GET())
     if not form.is_valid():
         return RestResponse.frontFail("参数错误!", form.errorsDict())
     data = form.cleaned_data
     offset = data['offset'] if data['offset'] else 0
     limit = data['limit'] if data['limit'] else 10
     userList = User.objects.values('userId').order_by(
         '-score')[offset:offset + limit]
     userIdList = list(map(lambda x: x['userId'], userList))
     userList = Data.getDataList(User, userIdList)
     return RestResponse.success("获取成功!", {"userList": userList})
Esempio n. 3
0
 def post(self, request):
     self.userAuth()
     checkinScore = 5  # 签到奖励积分
     try:
         Data.createData(Checkin, {"userId": self.userId})
         data = Data.getData(User, self.userId)
         user = {
             "userId": self.userId,
             "score": data['score'] + checkinScore
         }
         Data.updateData(User, user)
         return RestResponse.success("签到成功!", {"score": checkinScore})
     except:
         return RestResponse.userFail("你今天已签到,不能重复签到哟!")
Esempio n. 4
0
 def post(self, request):
     form = UserLoginForm(self.POST())
     if not form.is_valid():
         return RestResponse.frontFail("参数错误!", form.errorsDict())
     data = form.cleaned_data
     openId = wechatAuth.requestWechat(data["code"])
     if not openId:
         return RestResponse.userFail("登录失败!")
     data['openId'] = openId
     data['loginTime'] = datetime.datetime.now()
     del data['code']
     user = User.objects.filter(openId=openId).values('userId').first()
     if user:
         userId = data['userId'] = user['userId']
         Data.updateData(User, data)
     else:
         user = Data.createData(User, data)
         userId = user.userId
     sn = Token.setSn(userId)
     return RestResponse.success("登录成功!", {"userId": userId, "sn": sn})
Esempio n. 5
0
 def post(self, request, questionId):
     self.userAuth()
     form = QuestionByIdForm(self.POST())
     if not form.is_valid():
         return RestResponse.frontFail("参数错误!", form.errorsDict())
     data = form.cleaned_data
     answerList = json.loads(
         data['answerList']) if data['answerList'] else []
     question = Data.getData(Question, questionId)
     if question['type'] == 1:
         if len(answerList) != 1:
             return RestResponse.userFail("回答错误!")
         ques = Question.objects.filter(
             questionId=questionId).values('isCorrect').first()
         if not ques or ques['isCorrect'] != answerList[0]:
             return RestResponse.userFail("回答错误!")
     elif question['type'] == 2:
         if len(answerList) != 1:
             return RestResponse.userFail("回答错误!")
         option = Option.objects.filter(**{
             'optionId': answerList[0]
         }).values(*['questionId', 'isCorrect']).first()
         if option['questionId'] != int(
                 questionId) or option['isCorrect'] != 1:
             return RestResponse.userFail("回答错误!")
     elif question['type'] == 3:
         if len(answerList) < 2:
             return RestResponse.userFail("回答错误!")
         optionList = Option.objects.filter(**{
             'questionId': questionId,
             'isCorrect': 1
         }).values('optionId')
         if set(map(lambda x: x['optionId'],
                    optionList)) != set(answerList):
             return RestResponse.userFail("回答错误!")
     data = Data.getData(User, self.userId)
     Data.updateData(User, {
         "userId": self.userId,
         "score": data['score'] + question['type'] * 5
     })
     return RestResponse.success("回答正确!", {"score": question['type'] * 5})
Esempio n. 6
0
 def process_exception(self, request, exception):
     exceptionStr = str(exception)
     if exceptionStr == 'userAuthException':
         return RestResponse.authFail()
     print(request, exception)
     return RestResponse.serverFail()
Esempio n. 7
0
 def get(self, request):
     self.userAuth()
     data = Data.getData(User, self.userId)
     return RestResponse.success("获取自己的信息成功!", data)