Generate links at pre_dump time
This commit is contained in:
parent
d13f8d8b8c
commit
4793de1283
4 changed files with 547 additions and 101 deletions
|
|
@ -1,4 +1,4 @@
|
|||
from flask import abort
|
||||
from flask import abort, url_for
|
||||
|
||||
from openflexure_microscope.common.flask_labthings.schema import Schema
|
||||
from openflexure_microscope.common.flask_labthings import fields
|
||||
|
|
@ -6,6 +6,8 @@ from openflexure_microscope.common.labthings_core import tasks
|
|||
from openflexure_microscope.common.flask_labthings.resource import Resource
|
||||
from openflexure_microscope.common.flask_labthings.utilities import description_from_view
|
||||
|
||||
from marshmallow import pre_dump
|
||||
|
||||
|
||||
class TaskList(Resource):
|
||||
def get(self):
|
||||
|
|
@ -43,16 +45,17 @@ class TaskSchema(Schema):
|
|||
_start_time = fields.String(data_key="start_time")
|
||||
_end_time = fields.String(data_key="end_time")
|
||||
|
||||
# TODO: Add HTTP methods
|
||||
links = fields.Hyperlinks(
|
||||
{
|
||||
# TODO: Automate this somewhat
|
||||
@pre_dump
|
||||
def generate_links(self, data, **kwargs):
|
||||
data.links = {
|
||||
"self": {
|
||||
"href": fields.AbsoluteUrlFor(TaskResource, id="<id>"),
|
||||
"href": url_for(TaskResource.endpoint, id=data.id, _external=True),
|
||||
"mimetype": "application/json",
|
||||
**description_from_view(TaskResource)
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
return data
|
||||
|
||||
|
||||
task_schema = TaskSchema()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue