marshmallow-code / marshmallow-code/flask-marshmallow
conditionally return URLs
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 887
- Forks
- 65
- Avg merge
- 7h 25m
- Merged PRs (30d)
- 3
Description
I'm looking to selectively return some urls based on the current state of the object and am having a heck of a time solving how to expose the state property in the Schema, do some logic and determine which URLs to return based on the object state:
The Model:
class Car(Model):
model = Column(String)
year = Column(String)
running = Column(Boolean) #'0 = not running', '1 = running'
and the 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 like to do is have the start url only returned when the 'running' property is 0, and the stop url returned when it's 1, but i'm unclear on how to accomplish this.
Marshmallow seems to have a few decorators that would seem to but how would I leverage them with flask-marshmallow?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the CarSchema definition and the Marshmallow decorators mentioned in the issue. Check how flask-marshmallow exposes object state during serialization, then verify serialized Car output for running=0 and running=1: each should contain only the applicable start or stop URL, alongside self.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- flask, python
- Domain
- api
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100