示例#1
0
def record_gameevent(sessionid, token, events):
    '''
    Checks if the token is valid and records the game event(s) in the database.
    
    :param sessionid: Sessionid to which the gameevent is related
    :param token: the token used to authenticate the request
    :param events: JSON representation of the event
    :returns: Number of events inserted successfully in the database.
    :rtype: int
    '''

    client = Client.verify_auth_token(token)
    
   
    if client and ("sessionid" in client):
        if sessionid != client["sessionid"]:
            raise NotAcceptable("Requested sessionID and token sessionid do not match.")
        
        query_sessionid = db.session.query(Session).filter(Session.id == sessionid)
        try:
            res_sessionid = query_sessionid.one()
        except NoResultFound:
            # SessionID is not in the db.
            raise NotFound("This sessionid is not in the database. Did you request a token first?")
            
        #serialized_events = False
        decoded_events = False
        count_new_records = 0
        if (res_sessionid):
            #TODO: Validate the game event against XML schema or JSON-LD context?
            if (isinstance(events, str)):            
                try:
                    decoded_events = simplejson.loads(events)
                #serialized_events = simplejson.dumps(decoded_events)
                except JSONDecodeError:
                    raise BadRequest
            elif (isinstance(events, list)):
                decoded_events = events
            else:
                raise BadRequest
            
            results = []
            if decoded_events:
                for decoded_event in decoded_events:
                    new_gameevent = GameEvent(sessionid, simplejson.dumps(decoded_event))
                    db.session.add(new_gameevent)
                    results.append(new_gameevent)
                    try:
                        db.session.commit()
                        count_new_records = count_new_records + 1
                    except Exception as e:
                        LOG.warning(e)
                        db.session.rollback()
                        db.session.flush() # for resetting non-commited .add()
                        LOG.error(e, exc_info=True)
                        raise e
            return results        
    else:
        raise AuthenticationFailed('Unauthorized token. You need a client token to edit gaming sessions.') 
示例#2
0
def token_authenticate(token):
    '''
    Tries to authenticate a client checking the token.
    
    :param token: The token to be checked
    :raises: `AuthenticationFailed` if client does not exist or if token is not valid/has expired.
    :returns: the client that has been authenticated
    :rtype: :py:class:`gameevents_app.models.session.Client`
    '''
    """"""
    try:
        token_client = Client.verify_auth_token(token)
        clientid_from_token = token_client["clientid"]
        client = db.session.query(Client).filter_by(clientid = clientid_from_token).one()
        return client
    except NoResultFound:
        # Temporary fix to be able to create the first admin user
        if clientid_from_token == "masteroftheuniverse":
            client = Client(clientid_from_token, "easkdajskda")
            return client
        else:
            raise AuthenticationFailed("Clientid does not exist.")
def token_authenticate(token):
    '''
    Tries to authenticate a client checking the token.
    
    :param token: The token to be checked
    :raises: `AuthenticationFailed` if client does not exist or if token is not valid/has expired.
    :returns: the client that has been authenticated
    :rtype: :py:class:`gameevents_app.models.session.Client`
    '''
    """"""
    try:
        token_client = Client.verify_auth_token(token)
        clientid_from_token = token_client["clientid"]
        client = db.session.query(Client).filter_by(
            clientid=clientid_from_token).one()
        return client
    except NoResultFound:
        # Temporary fix to be able to create the first admin user
        if clientid_from_token == "masteroftheuniverse":
            client = Client(clientid_from_token, "easkdajskda")
            return client
        else:
            raise AuthenticationFailed("Clientid does not exist.")
def record_gameevent(sessionid, token, events):
    '''
    Checks if the token is valid and records the game event(s) in the database.
    
    :param sessionid: Sessionid to which the gameevent is related
    :param token: the token used to authenticate the request
    :param events: JSON representation of the event
    :returns: Number of events inserted successfully in the database.
    :rtype: int
    '''

    client = Client.verify_auth_token(token)

    if client and ("sessionid" in client):
        if sessionid != client["sessionid"]:
            raise NotAcceptable(
                "Requested sessionID and token sessionid do not match.")

        query_sessionid = db.session.query(Session).filter(
            Session.id == sessionid)
        try:
            res_sessionid = query_sessionid.one()
        except NoResultFound:
            # SessionID is not in the db.
            raise NotFound(
                "This sessionid is not in the database. Did you request a token first?"
            )

        #serialized_events = False
        decoded_events = False
        count_new_records = 0
        if (res_sessionid):
            #TODO: Validate the game event against XML schema or JSON-LD context?
            if (isinstance(events, str)):
                try:
                    decoded_events = simplejson.loads(events)
                #serialized_events = simplejson.dumps(decoded_events)
                except JSONDecodeError:
                    raise BadRequest
            elif (isinstance(events, list)):
                decoded_events = events
            else:
                raise BadRequest

            results = []
            if decoded_events:
                for decoded_event in decoded_events:
                    new_gameevent = GameEvent(sessionid,
                                              simplejson.dumps(decoded_event))
                    db.session.add(new_gameevent)
                    results.append(new_gameevent)
                    try:
                        db.session.commit()
                        count_new_records = count_new_records + 1
                    except Exception as e:
                        LOG.warning(e)
                        db.session.rollback()
                        db.session.flush()  # for resetting non-commited .add()
                        LOG.error(e, exc_info=True)
                        raise e
            return results
    else:
        raise AuthenticationFailed(
            'Unauthorized token. You need a client token to edit gaming sessions.'
        )