Open graysonhead opened 5 years ago
In my opinion you should:
use after_update_object
function instead of after_patch, by defining a "methods" dictionnary in your data layer. after_update_object
has the obj you have changed as input argument, so no need to query it by yourself.
Remove session.add(segment)
. It is not necessary as the object is aldready created and you only update it. Just modify it and commit.
Moreover, the way you recover the session seems strange to me.
Why don't you use the db.session
comming from your db
object (the one you use to declare your Segment
class)?
Thanks @etiennecaldo, It now looks like the follwing:
class SegmentDetail(ResourceDetail):
def after_update_object(self, object):
object.calc_distance()
self.session.add(object)
self.session.commit()
schema = SegmentSchema
decorators = (login_required_api, )
data_layer = {
'session': db.session,
'model': models.Segment,
'methods': {'after_update_object': after_update_object}
}
Which is much cleaner indeed. However, I seems like I can still get outdated information after I get a 200 from the PATCH request.
I have a resource called Segments which contains a geoJSON path and some statistical attributes (Only distance is included in this example for simplicity.)
The issue I'm running into is that when I use after_patch to re-calculate statistical info (distance of the selected path in this case), if I make a GET immediately after the PATCH the first GET has the old distance value, not the distance value that gets set in my after_patch function. If I wait long enough, the GET has the correct value, but for longer paths updating the distance/average speed/etc can be long and un-predictable. I would much rather have the request time out than get outdated information back.
I feel that I'm doing this the wrong way, but I wasn't able to figure out any cleaner way to do it, help appreciated!
Model:
Schema:
ResourceDetail: