Exemplo n.º 1
0
    def get(self, request):
        player = UserScore.objects.filter(user=request.user)[0]
        active = config.quiz_active(config)
        curr_config = config.current_config(config)
        current_time = datetime.datetime.now().replace(tzinfo=utc)
        if current_time < curr_config.quiz_start.replace(tzinfo=utc):
            response = {"quiz_started": False}
            return Response(response)
        if active:
            day = curr_config.current_day
            curr_day = player.today
            curr_question = player.current_question
            if curr_day > curr_config.current_day:
                response = {"quiz_finished": True}
                return Response(response)
            if curr_day < day:  #IMP: this is done so that for users who haven't completed the last day's task completely
                player.today = day  #The ptr on current question is shifted to 1 and the day is shifted to whatever the present day is.
                player.current_question = 1  #EDGE CASE: ERROR if there are only 1 question in a round.
                curr_day = player.today
                curr_question = player.current_question
                player.save()

            ##question=Question.objects.filter(day=day,question_no=curr_question)[0]
            #serializer=QuestionSerializer(question)
            response = {'current_question': curr_question}
            return Response(response)
        else:
            response = {"error": "quiz has ended"}
            return Response(response)
Exemplo n.º 2
0
 def post(self,request):
     player=UserScore.objects.filter(user=request.user)[0]
     print(player.name)
     answer=request.data.get("answer",None)
     print(player)
     active=config.quiz_active(config)
     if active:
         day=config.objects.all()[0].current_day
         curr_question=player.current_question
         if curr_question>config.objects.all()[0].q_no:
             response={
               "quiz_finished": True
             }
             return Response(response)
         question=Question.objects.filter(day=day,question_no=curr_question)
         result=Question.check_ans(Question,answer,question)
         quiz_ended=False
         if result:
            player.new_score(player)
            if curr_question==config.objects.all()[0].q_no:
                quiz_ended=True
         response={
                 'status_code':status.HTTP_200_OK,
                 'result':result,
                 'quiz_finished':quiz_ended
             }
     else:
        response= {
             "error":"quiz has ended"
         }
     return Response(response)
Exemplo n.º 3
0
    def post(self, request):
        res = verifyGoogleToken(request.data.get('token'))
        if res['status'] == 404:
            return Response({"status": 404, "message": "Token expired."})
        try:
            user = User.objects.get(email=res['email'])
        except User.DoesNotExist:
            user = User()
            user.username = res['email']
            # provider random default password
            user.password = make_password(
                BaseUserManager().make_random_password())
            user.email = res['email']
            user.save()
            score = UserScore(
                user=user,
                name=res['first_name'],
                imgurl=res['image'],
                email=user.email,
                current_question=1,
                last_modified=datetime.datetime.now().replace(tzinfo=utc))
            score.save()
            z = UserScore.lboardSave(UserScore)

        token = RefreshToken.for_user(
            user)  # generate token without username & password
        response = {}
        response['username'] = user.username
        response['access_token'] = str(token.access_token)
        response['refresh_token'] = str(token)
        response['image'] = str(res['image'])
        #register on leaderboard

        #adding quiz_finished tag for users who have finished the level
        player = UserScore.objects.filter(user=user)[0]
        active = config.quiz_active(config)
        curr_config = config.current_config(config)
        if active:
            day = curr_config.current_day
            curr_day = player.today
            curr_question = player.current_question
            if curr_day > day:
                response['quiz_finished'] = True
            else:
                response['quiz_finished'] = False
        else:
            response['error'] = "No active quizes"
        #end
        return Response(response)
Exemplo n.º 4
0
 def validate(self,data): 
     answer=data.get("answer",None)
     player=self.context.get("player")
     print(player)
     #player=data.get("player")
     active=config.quiz_active(config)
     day=config.objects.all()[0].current_day
     curr_question=player[0].current_question
     question=Question.objects.filter(day=day,question_no=curr_question)
     result=Question.check_ans(Question,answer,question)
     if result:
        player[0].new_score(player)
     return {
             "result":result
         }
Exemplo n.º 5
0
 def validate(self,data): 
     answer=data.get("answer",None)
     player=self.context.get("player")
     print(player)
     #player=data.get("player")
     active=config.quiz_active(config)
     curr_config = config.current_config(config)
     day=curr_config.current_day
     
     curr_question=player[0].current_question
     question=Question.objects.filter(day=day,question_no=curr_question)
     result=Question.check_ans(Question,answer,question)                 #answer is being checked here. Resulting response is sent back
     if result:
        player[0].new_score(player)                                      #if ans is correct, new score is store. Result is returned
     return {
             "result":result
         }
Exemplo n.º 6
0
 def get(self,request):
     player=UserScore.objects.filter(user=request.user)[0]
     active=config.quiz_active(config)
     if active:
         day=config.objects.all()[0].current_day
         curr_question=player.current_question
         if curr_question>config.objects.all()[0].q_no:
             response={
               "quiz_finished": True
             }
             return Response(response)
         question=Question.objects.filter(day=day,question_no=curr_question)[0]
         serializer=QuestionSerializer(question)
         return Response(serializer.data)
     else:
         response= {
             "error":"quiz has ended"
         }
         return Response(response)
Exemplo n.º 7
0
    def post(self, request):
        print(request.data)
        accesstoken = request.data.get('accesstoken')
        expiration_time = request.data.get('expiration_time')
        print(expiration_time)
        userID = request.data.get('userID')
        if (int(expiration_time) < int(time.time())):
            content = {"status": 404}
            return Response(content)
        else:
            url = "https://graph.facebook.com/{}".format(userID)
            parameters = {
                'fields': 'name,email,picture',
                'access_token': accesstoken
            }
            idInfo = r.get(url=url, params=parameters).json()

            email = idInfo['email']
            name = idInfo['name']
            email = email
            name = name

            image = idInfo['picture']['data']['url']
            try:
                user = User.objects.get(email=email)
                player = UserScore.objects.filter(user=user)[
                    0]  #FB profile pic at higher priority than google.
                player.imgurl = image  #However this generates an issue: Anyone who swtiches to fb from google
                player.save(
                )  #have his last modified time, changed to now(). So he might lose a couple
            except User.DoesNotExist:  #places on the leaderboard. Solution: Make a custom save option/ Change auto_now= false
                user = User()
                user.username = email
                user.email = email
                # provider random default password
                user.password = make_password(
                    BaseUserManager().make_random_password())
                user.save()
                score = UserScore(
                    user=user,
                    name=name,
                    imgurl=image,
                    email=user.email,
                    current_question=1,
                    last_modified=datetime.datetime.now().replace(tzinfo=utc))
                score.save()
                z = UserScore.lboardSave(UserScore)

        token = RefreshToken.for_user(
            user)  # generate token without username & password
        response = {}
        response['username'] = user.username
        response['access_token'] = str(token.access_token)
        response['refresh_token'] = str(token)
        response['image'] = image[0]

        #adding quiz_finished tag for users who have finished the level
        player = UserScore.objects.filter(user=user)[0]
        active = config.quiz_active(config)
        curr_config = config.current_config(config)
        if active:
            day = curr_config.current_day
            curr_day = player.today
            curr_question = player.current_question
            if curr_day > day:
                response['quiz_finished'] = True
            else:
                response['quiz_finished'] = False
        else:
            response['error'] = "No active quizes"
        #end
        return Response(response)