python - flask-marshmallow - how to conditionally generate HATEOAS URLS -


i'm looking selectively return urls based on current state of object , having heck of time solving how expose state property in schema, logic , determine urls return based on object state:

the model:

class car(model):     model = column(string)     year = column(string)     running = column(boolean)   #'0 = not running', '1 = running' 

and schema:

class carschema(ma.schema):     class meta:         fields = ('model', 'year', 'running',  '_links')      _links = ma.hyperlinks({         'self': ma.urlfor('car_detail', id='<id>'),         'start': ma.urlfor('car_start', id='<id>')         'stop': ma.urlfor('car_start', id='<id>')     }) 

what i'd have start url returned when 'running' property 0, , stop url returned when it's 1, i'm unclear on how accomplish this.

marshmallow seems have few decorators seem how leverage them flask-marshmallow?

you can post_dump post-processing: check running status , remove inappropriate fields. that'll easier generating them conditionally.

class carschema(ma.schema):     class meta:         fields = ('model', 'year', 'running', '_links')      _links = ma.hyperlinks({         'self': ma.urlfor('car_detail', id='<id>'),         'start': ma.urlfor('car_start', id='<id>')         'stop': ma.urlfor('car_start', id='<id>')     })      @ma.post_dump     def postprocess(self, data):         if data['running']:             del data['_links']['start']         else:             del data['_links']['stop'] 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -