示例#1
0
文件: views.py 项目: noQ/TriviaOnline
    def post(self, request):
        ''' check user token '''
        token = TokenMiddleware.get_token(request)
        if token is None:
            raise NotAuthenticated("Token invalid or expired!")

        try:
            data = JSONParser().parse(request)
            data = data.get(PARAMETER_DATA)
        except JSONDecodeError:
            raise ParseError(detail="No data found on the request")
        game_type = WebRequestParameter.get_data(PARAMETER_TYPE, data)

        ''' extract user from token '''
        if not token.has_key(PARAMETER_USER):
            raise NotAuthenticated()
        ''' get user object from request '''
        user = token.get(PARAMETER_USER)
        if user is None:
            raise NotAuthenticated("User not exists!")

        score = WebRequestParameter.get_data(PARAMETER_SCORE, data)
        if not isinstance(score, list):
            raise APIException("Object must be an instance of list")
        
        ''' update user score '''
        user, score_detail = self.update_score(user, score, game_type)
        if score_detail.has_key("levelup"):
            token = TokenMiddleware.get_token(request)
            if token:
                from engine.util.const import ten_minutes_in_seconds
                cache_data = dict(uid=user.id, name=user.name, user=user)
                cache.set(user.auth_token, cache_data, ten_minutes_in_seconds)
        return Response(score_detail)
示例#2
0
文件: views.py 项目: noQ/TriviaOnline
 def get(self, request, limit=20):
     token = TokenMiddleware.get_token(request)
     if token is None:
         raise NotAuthenticated("Token invalid or expired!")
     uid = str(token.get(PARAMETER_UID))
     
     leader_table = ObjectMemoryCache.get_key(Leaderboard)
     if leader_table:
         leader_table = leader_table.values()
     else:
         ''' retrieve X leaders from leaderboard table '''
         leader_table = Leaderboard.objects[:limit]
         if len(leader_table) == 0:
             return []
         memo = ObjectMemoryCache(Leaderboard, PARAMETER_ID)
         memo.queryset(leader_table)
         memo.load(serialize=True, serializer_class=Leader)
         leader_table = memo.get_key(Leaderboard)
     return Response(leader_table)