flask-rest-jsonapi icon indicating copy to clipboard operation
flask-rest-jsonapi copied to clipboard

cannot set properties on connection using pre-process methods

Open mdbr000 opened this issue 7 years ago • 0 comments
trafficstars

First of all, excellent work on this library. So much easier to use jsonapi with this lib than any other I've worked with.

I'm probably going about this wrong, but my issue is that I cannot set a property on the underlying connection object using a pre-process method. Specifically, I need to set the 'client_identifier' property on the cx_Oracle connection object that is being used to communicate with the db for every transaction.

What does work is if I set that property in a standalone transaction, where I create a connection, set my desired connection properties (client_identifier), create a cursor, and execute some arbitrary sql query using that cursor. However, I can't seem to figure out how to do the same while inside one of the preprocess methods. Seems like those methods are using a connection object that is unreachable from the preprocess method itself?

Here's what does work, that is, the client_identifier property is set successfully:

    raw_conn = db.session.connection().engine.raw_connection().connection
    cursor = raw_conn.cursor()
    cursor.execute("select username, client_identifier from v$session where username = 'MY_USERNAME'")
    v = cursor.fetchall()
    print(v)

And here's what I am attempting to do in the pre-process method, but it does not work, that is, when this resource is queried for, the pre-process method does get executed, but the client_identifier property is not set and remains null:

class MyObjectList(ResourceList):
    def before_get_collection(self, qs, view_kwargs):
        print(qs, view_kwargs)
        raw_conn = self.session.query().session.connection().engine.raw_connection().connection
        # always set the client_identifier
        raw_conn.client_identifier = 'MY_CUSTOM_CLIENT_ID'

    schema = MyObjectSchema
    data_layer = {'session': db.session,
                  'model': MyObject,
                  'methods': {'before_get_collection': before_get_collection}
                  }
    decorators = (jwt_required,)

mdbr000 avatar Mar 06 '18 15:03 mdbr000